Merge pull request #2524 from jmichelp/lua54

Update LUA from v5.2 to v5.4
This commit is contained in:
Iceman 2024-09-22 10:26:51 +03:00 committed by GitHub
commit 2b1a71b391
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
78 changed files with 27062 additions and 17022 deletions

View file

@ -41,7 +41,7 @@ jobs:
run: sudo apt-get update run: sudo apt-get update
- name: Install dependencies - name: Install dependencies
run: sudo apt-get install -yqq make autoconf build-essential ca-certificates pkg-config libreadline-dev gcc-arm-none-eabi libnewlib-dev qtbase5-dev libbz2-dev liblz4-dev libbluetooth-dev libpython3-dev python3 python3-dev libpython3-all-dev liblua5.2-dev liblua5.2-0 lua5.2 sed libssl-dev run: sudo apt-get install -yqq make autoconf build-essential ca-certificates pkg-config libreadline-dev gcc-arm-none-eabi libnewlib-dev qtbase5-dev libbz2-dev liblz4-dev libbluetooth-dev libpython3-dev python3 python3-dev libpython3-all-dev liblua5.4-dev liblua5.4-0 lua5.4 sed libssl-dev
- name: Install Python dependencies - name: Install Python dependencies
run: | run: |

View file

@ -30,7 +30,7 @@ jobs:
run: sudo apt-get update run: sudo apt-get update
- name: Install dependencies - name: Install dependencies
run: sudo apt-get install -yqq make autoconf build-essential ca-certificates pkg-config libreadline-dev gcc-arm-none-eabi libnewlib-dev qtbase5-dev libbz2-dev liblz4-dev libbluetooth-dev libpython3-dev python3 python3-dev libpython3-all-dev liblua5.2-dev liblua5.2-0 lua5.2 sed libssl-dev libgd-dev run: sudo apt-get install -yqq make autoconf build-essential ca-certificates pkg-config libreadline-dev gcc-arm-none-eabi libnewlib-dev qtbase5-dev libbz2-dev liblz4-dev libbluetooth-dev libpython3-dev python3 python3-dev libpython3-all-dev liblua5.4-dev liblua5.4-0 lua5.4 sed libssl-dev libgd-dev
- name: Install Python dependencies - name: Install Python dependencies
run: pip install -r tools/requirements.txt run: pip install -r tools/requirements.txt
@ -60,7 +60,7 @@ jobs:
run: sudo apt-get update run: sudo apt-get update
- name: Install dependencies - name: Install dependencies
run: sudo apt-get install -yqq make autoconf build-essential ca-certificates pkg-config libreadline-dev gcc-arm-none-eabi libnewlib-dev qtbase5-dev libbz2-dev liblz4-dev libbluetooth-dev libpython3-dev python3 python3-dev libpython3-all-dev liblua5.2-dev liblua5.2-0 lua5.2 sed libssl-dev libgd-dev run: sudo apt-get install -yqq make autoconf build-essential ca-certificates pkg-config libreadline-dev gcc-arm-none-eabi libnewlib-dev qtbase5-dev libbz2-dev liblz4-dev libbluetooth-dev libpython3-dev python3 python3-dev libpython3-all-dev liblua5.4-dev liblua5.4-0 lua5.4 sed libssl-dev libgd-dev
- name: Install Python dependencies - name: Install Python dependencies
run: pip install -r tools/requirements.txt run: pip install -r tools/requirements.txt
@ -91,7 +91,7 @@ jobs:
run: sudo apt-get update run: sudo apt-get update
- name: Install dependencies - name: Install dependencies
run: sudo apt-get install -yqq make autoconf build-essential ca-certificates pkg-config libreadline-dev gcc-arm-none-eabi libnewlib-dev qtbase5-dev libbz2-dev liblz4-dev libbluetooth-dev libpython3-dev python3 python3-dev libpython3-all-dev liblua5.2-dev liblua5.2-0 lua5.2 sed libssl-dev libgd-dev run: sudo apt-get install -yqq make autoconf build-essential ca-certificates pkg-config libreadline-dev gcc-arm-none-eabi libnewlib-dev qtbase5-dev libbz2-dev liblz4-dev libbluetooth-dev libpython3-dev python3 python3-dev libpython3-all-dev liblua5.4-dev liblua5.4-0 lua5.4 sed libssl-dev libgd-dev
- name: Install Python dependencies - name: Install Python dependencies
run: pip install -r tools/requirements.txt run: pip install -r tools/requirements.txt

View file

@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...
## [unreleased][unreleased] ## [unreleased][unreleased]
- Updated LUA to v5.4.7 which adds utf-8 support (@jmichelp)
- Changed `lf search` - it now tries to read and decode paxton id (@iceman1001) - Changed `lf search` - it now tries to read and decode paxton id (@iceman1001)
- Changed `lf search` - to identify hitag2/s/82xx in chipset detection to preserve their EM4100 or other outputs (@iceman1001) - Changed `lf search` - to identify hitag2/s/82xx in chipset detection to preserve their EM4100 or other outputs (@iceman1001)
- Added `lf hitag hts reader` - to act as a HitagS / 82xx reader (@iceman1001) - Added `lf hitag hts reader` - to act as a HitagS / 82xx reader (@iceman1001)

View file

@ -411,6 +411,7 @@ set (TARGET_SOURCES
${PM3_ROOT}/client/src/graph.c ${PM3_ROOT}/client/src/graph.c
${PM3_ROOT}/client/src/iso4217.c ${PM3_ROOT}/client/src/iso4217.c
${PM3_ROOT}/client/src/jansson_path.c ${PM3_ROOT}/client/src/jansson_path.c
${PM3_ROOT}/client/src/lua_bitlib.c
${PM3_ROOT}/client/src/preferences.c ${PM3_ROOT}/client/src/preferences.c
${PM3_ROOT}/client/src/pm3.c ${PM3_ROOT}/client/src/pm3.c
${PM3_ROOT}/client/src/pm3_binlib.c ${PM3_ROOT}/client/src/pm3_binlib.c

View file

@ -175,11 +175,11 @@ PM3INCLUDES += $(ID48LIBINC)
## Lua ## Lua
ifneq ($(SKIPLUASYSTEM),1) ifneq ($(SKIPLUASYSTEM),1)
ifdef MACPORTS_PREFIX ifdef MACPORTS_PREFIX
LUAINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags lua-5.2 2>/dev/null) LUAINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags lua-5.4 2>/dev/null)
LUALDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs lua-5.2 2>/dev/null) LUALDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs lua-5.4 2>/dev/null)
else else
LUAINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags lua5.2 2>/dev/null) LUAINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags lua5.4 2>/dev/null)
LUALDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs lua5.2 2>/dev/null) LUALDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs lua5.4 2>/dev/null)
endif endif
ifneq ($(LUALDLIBS),) ifneq ($(LUALDLIBS),)
LUALIB = LUALIB =
@ -730,11 +730,12 @@ SRCS = mifare/aiddesfire.c \
loclass/cipherutils.c \ loclass/cipherutils.c \
loclass/elite_crack.c \ loclass/elite_crack.c \
loclass/ikeys.c \ loclass/ikeys.c \
lua_bitlib.c \
mifare/lrpcrypto.c \ mifare/lrpcrypto.c \
mifare/desfirecrypto.c \ mifare/desfirecrypto.c \
mifare/desfirecore.c \ mifare/desfirecore.c \
mifare/desfiresecurechan.c \ mifare/desfiresecurechan.c \
mifare/desfiretest.c \ mifare/desfiretest.c \
mifare/gallaghercore.c \ mifare/gallaghercore.c \
mifare/mad.c \ mifare/mad.c \
mifare/mfkey.c \ mifare/mfkey.c \

View file

@ -3,11 +3,10 @@ MYINCLUDES = -I.
# Lua lib requires GNU extensions (implicit declarations of functions): -std=gnu99 or -std=gnu11 # Lua lib requires GNU extensions (implicit declarations of functions): -std=gnu99 or -std=gnu11
MYCFLAGS = -Wno-cast-align -Wno-bad-function-cast -Wno-switch-enum MYCFLAGS = -Wno-cast-align -Wno-bad-function-cast -Wno-switch-enum
MYDEFS = -DLUA_COMPAT_ALL $(SYSCFLAGS) MYDEFS = -DLUA_COMPAT_ALL $(SYSCFLAGS)
MYSRCS = lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c \ MYSRCS = lapi.c lauxlib.c lbaselib.c lcode.c lcorolib.c lctype.c ldblib.c ldebug.c ldo.c ldump.c \
lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c \ lfunc.c lgc.c linit.c liolib.c llex.c lmathlib.c lmem.c loadlib.c lobject.c lopcodes.c \
ltm.c lundump.c lvm.c lzio.c \ loslib.c lparser.c lstate.c lstring.c lstrlib.c ltable.c ltablib.c ltm.c lundump.c \
lauxlib.c lbaselib.c lbitlib.c lcorolib.c ldblib.c liolib.c \ lutf8lib.c lvm.c lzio.c
lmathlib.c loslib.c lstrlib.c ltablib.c loadlib.c linit.c
SYSCFLAGS= SYSCFLAGS=
@ -59,6 +58,6 @@ posix:
$(Q)$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" $(Q)$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX"
solaris: solaris:
$(Q)$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" $(Q)$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT"
.PHONY: all $(PLATS) default clean depend none .PHONY: all $(PLATS) default clean depend none

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.h,v 2.7 2009/11/27 15:37:59 roberto Exp $ ** $Id: lapi.h $
** Auxiliary functions from Lua API ** Auxiliary functions from Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,14 +11,42 @@
#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");}
/* 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");}
/*
** 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 < L->top) L->ci->top = L->top; } { if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
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")
/*
** To reduce the overhead of returning from C functions, the presence of
** to-be-closed variables in these functions is coded in the CallInfo's
** field 'nresults', in a way that functions with no to-be-closed variables
** with zero, one, or "all" wanted results have no overhead. Functions
** with other number of wanted results, as well as functions with
** variables to be closed, have an extra check.
*/
#define hastocloseCfunc(n) ((n) < LUA_MULTRET)
/* Map [-1, inf) (range of 'nresults') into (-inf, -2] */
#define codeNresults(n) (-(n) - 3)
#define decodeNresults(n) (-(n) - 3)
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.h,v 1.120 2011/11/29 15:55:08 roberto Exp $ ** $Id: lauxlib.h $
** 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,91 +12,110 @@
#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"
/* extra error code for `luaL_load' */
typedef struct luaL_Buffer luaL_Buffer;
/* extra error code for 'luaL_loadfilex' */
#define LUA_ERRFILE (LUA_ERRERR+1) #define LUA_ERRFILE (LUA_ERRERR+1)
/* key, in the registry, for table of loaded modules */
#define LUA_LOADED_TABLE "_LOADED"
/* key, in the registry, for table of preloaded loaders */
#define LUA_PRELOAD_TABLE "_PRELOAD"
typedef struct luaL_Reg { typedef struct luaL_Reg {
const char *name; const char *name;
lua_CFunction func; lua_CFunction func;
} luaL_Reg; } luaL_Reg;
LUALIB_API void (luaL_checkversion_)(lua_State *L, lua_Number ver); #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
#define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM)
LUALIB_API int (luaL_getmetafield)(lua_State *L, int obj, const char *e); LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz);
LUALIB_API int (luaL_callmeta)(lua_State *L, int obj, const char *e); #define luaL_checkversion(L) \
LUALIB_API const char *(luaL_tolstring)(lua_State *L, int idx, size_t *len); luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)
LUALIB_API int (luaL_argerror)(lua_State *L, int numarg, const char *extramsg);
LUALIB_API const char *(luaL_checklstring)(lua_State *L, int numArg,
size_t *l);
LUALIB_API const char *(luaL_optlstring)(lua_State *L, int numArg,
const char *def, size_t *l);
LUALIB_API lua_Number(luaL_checknumber)(lua_State *L, int numArg);
LUALIB_API lua_Number(luaL_optnumber)(lua_State *L, int nArg, lua_Number def);
LUALIB_API lua_Integer(luaL_checkinteger)(lua_State *L, int numArg); LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
LUALIB_API lua_Integer(luaL_optinteger)(lua_State *L, int nArg, LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
lua_Integer def); LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len);
LUALIB_API lua_Unsigned(luaL_checkunsigned)(lua_State *L, int numArg); LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg);
LUALIB_API lua_Unsigned(luaL_optunsigned)(lua_State *L, int numArg, LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname);
lua_Unsigned def); LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg,
size_t *l);
LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg,
const char *def, size_t *l);
LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg);
LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def);
LUALIB_API void (luaL_checkstack)(lua_State *L, int sz, const char *msg); LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg);
LUALIB_API void (luaL_checktype)(lua_State *L, int narg, int t); LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg,
LUALIB_API void (luaL_checkany)(lua_State *L, int narg); lua_Integer def);
LUALIB_API int (luaL_newmetatable)(lua_State *L, const char *tname); LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
LUALIB_API void (luaL_setmetatable)(lua_State *L, const char *tname); LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t);
LUALIB_API void *(luaL_testudata)(lua_State *L, int ud, const char *tname); LUALIB_API void (luaL_checkany) (lua_State *L, int arg);
LUALIB_API void *(luaL_checkudata)(lua_State *L, int ud, const char *tname);
LUALIB_API void (luaL_where)(lua_State *L, int lvl); LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
LUALIB_API int (luaL_error)(lua_State *L, const char *fmt, ...); LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname);
LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname);
LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
LUALIB_API int (luaL_checkoption)(lua_State *L, int narg, const char *def, LUALIB_API void (luaL_where) (lua_State *L, int lvl);
const char *const lst[]); LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
LUALIB_API int (luaL_fileresult)(lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
LUALIB_API int (luaL_execresult)(lua_State *L, int stat); const char *const lst[]);
/* pre-defined references */ LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
/* predefined references */
#define LUA_NOREF (-2) #define LUA_NOREF (-2)
#define LUA_REFNIL (-1) #define LUA_REFNIL (-1)
LUALIB_API int (luaL_ref)(lua_State *L, int t); LUALIB_API int (luaL_ref) (lua_State *L, int t);
LUALIB_API void (luaL_unref)(lua_State *L, int t, int ref); LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
LUALIB_API int (luaL_loadfilex)(lua_State *L, const char *filename, LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename,
const char *mode); const char *mode);
#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
LUALIB_API int (luaL_loadbufferx)(lua_State *L, const char *buff, size_t sz, LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
const char *name, const char *mode); const char *name, const char *mode);
LUALIB_API int (luaL_loadstring)(lua_State *L, const char *s); 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 int (luaL_len)(lua_State *L, int idx); LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
LUALIB_API const char *(luaL_gsub)(lua_State *L, const char *s, const char *p, LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
const char *r); const char *p, 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);
LUALIB_API int (luaL_getsubtable)(lua_State *L, int idx, const char *fname); LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname);
LUALIB_API void (luaL_traceback)(lua_State *L, lua_State *L1, LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
const char *msg, int level); const char *msg, int level);
LUALIB_API void (luaL_requiref)(lua_State *L, const char *modname, LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
lua_CFunction openf, int glb); lua_CFunction openf, int glb);
/* /*
** =============================================================== ** ===============================================================
@ -105,33 +124,62 @@ LUALIB_API void (luaL_requiref)(lua_State *L, const char *modname,
*/ */
#define luaL_newlibtable(L,l) \ #define luaL_newlibtable(L,l) \
lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
#define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) #define luaL_newlib(L,l) \
(luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
#define luaL_argcheck(L, cond,numarg,extramsg) \ #define luaL_argcheck(L, cond,arg,extramsg) \
((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg))))
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) #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_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
#define luaL_dofile(L, fn) \ #define luaL_dofile(L, fn) \
(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
#define luaL_dostring(L, s) \ #define luaL_dostring(L, s) \
(luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
#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 */
#define luaL_pushfail(L) lua_pushnil(L)
/*
** Internal assertions for in-house debugging
*/
#if !defined(lua_assert)
#if defined LUAI_ASSERT
#include <assert.h>
#define lua_assert(c) assert(c)
#else
#define lua_assert(c) ((void)0)
#endif
#endif
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
/* /*
@ -140,31 +188,40 @@ LUALIB_API void (luaL_requiref)(lua_State *L, const char *modname,
** ======================================================= ** =======================================================
*/ */
typedef struct luaL_Buffer { 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;
char initb[LUAL_BUFFERSIZE]; /* initial buffer */ union {
} luaL_Buffer; LUAI_MAXALIGN; /* ensure maximum alignment for 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) \
((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \
((B)->b[(B)->n++] = (c))) ((B)->b[(B)->n++] = (c)))
#define luaL_addsize(B,s) ((B)->n += (s)) #define luaL_addsize(B,s) ((B)->n += (s))
LUALIB_API void (luaL_buffinit)(lua_State *L, luaL_Buffer *B); #define luaL_buffsub(B,s) ((B)->n -= (s))
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_addstring)(luaL_Buffer *B, const char *s);
LUALIB_API void (luaL_addvalue)(luaL_Buffer *B);
LUALIB_API void (luaL_pushresult)(luaL_Buffer *B);
LUALIB_API void (luaL_pushresultsize)(luaL_Buffer *B, size_t sz);
LUALIB_API char *(luaL_buffinitsize)(lua_State *L, luaL_Buffer *B, size_t sz);
#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
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_addstring) (luaL_Buffer *B, const char *s);
LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz);
LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz);
#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
/* }====================================================== */ /* }====================================================== */
@ -186,25 +243,57 @@ LUALIB_API char *(luaL_buffinitsize)(lua_State *L, luaL_Buffer *B, size_t sz);
typedef struct luaL_Stream { typedef struct luaL_Stream {
FILE *f; /* stream (NULL for incompletely created streams) */ FILE *f; /* stream (NULL for incompletely created streams) */
lua_CFunction closef; /* to close stream (NULL for closed streams) */ lua_CFunction closef; /* to close stream (NULL for closed streams) */
} luaL_Stream; } luaL_Stream;
/* }====================================================== */ /* }====================================================== */
/*
** {==================================================================
** "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 old module system */ /*
#if defined(LUA_COMPAT_MODULE) ** {============================================================
** Compatibility with deprecated conversions
** =============================================================
*/
#if defined(LUA_COMPAT_APIINTCASTS)
LUALIB_API void (luaL_pushmodule)(lua_State *L, const char *modname, #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a))
int sizehint); #define luaL_optunsigned(L,a,d) \
LUALIB_API void (luaL_openlib)(lua_State *L, const char *libname, ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d)))
const luaL_Reg *l, int nup);
#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
#endif #endif
/* }============================================================ */
#endif #endif

View file

@ -1,9 +1,13 @@
/* /*
** $Id: lbaselib.c,v 1.276 2013/02/21 13:44:53 roberto Exp $ ** $Id: lbaselib.c $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#define lbaselib_c
#define LUA_LIB
#include "lprefix.h"
#include <ctype.h> #include <ctype.h>
@ -11,260 +15,334 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define lbaselib_c
#define LUA_LIB
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lualib.h" #include "lualib.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;
lua_getglobal(L, "tostring"); for (i = 1; i <= n; i++) { /* for each argument */
for (i = 1; i <= n; i++) { size_t l;
const char *s; const char *s = luaL_tolstring(L, i, &l); /* convert it to string */
size_t l; if (i > 1) /* not the first element? */
lua_pushvalue(L, -1); /* function to be called */ lua_writestring("\t", 1); /* add a tab before it */
lua_pushvalue(L, i); /* value to print */ lua_writestring(s, l); /* print it */
lua_call(L, 1, 1); lua_pop(L, 1); /* pop result */
s = lua_tolstring(L, -1, &l); /* get result */ }
if (s == NULL) lua_writeline();
return luaL_error(L, return 0;
LUA_QL("tostring") " must return a string to " LUA_QL("print")); }
if (i > 1) luai_writestring("\t", 1);
luai_writestring(s, l);
lua_pop(L, 1); /* pop result */ /*
** 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"
static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
lua_Unsigned n = 0;
int neg = 0;
s += strspn(s, SPACECHARS); /* skip initial spaces */
if (*s == '-') { s++; neg = 1; } /* handle sign */
else if (*s == '+') s++;
if (!isalnum((unsigned char)*s)) /* no digit? */
return NULL;
do {
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
: (toupper((unsigned char)*s) - 'A') + 10;
if (digit >= base) return NULL; /* invalid numeral */
n = n * base + digit;
s++;
} while (isalnum((unsigned char)*s));
s += strspn(s, SPACECHARS); /* skip trailing spaces */
*pn = (lua_Integer)((neg) ? (0u - n) : n);
return s;
}
static int luaB_tonumber (lua_State *L) {
if (lua_isnoneornil(L, 2)) { /* standard conversion? */
if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
lua_settop(L, 1); /* yes; return it */
return 1;
} }
luai_writeline();
return 0;
}
#define SPACECHARS " \f\n\r\t\v"
static int luaB_tonumber(lua_State *L) {
if (lua_isnoneornil(L, 2)) { /* standard conversion */
int isnum;
lua_Number n = lua_tonumberx(L, 1, &isnum);
if (isnum) {
lua_pushnumber(L, n);
return 1;
} /* else not a number; must be something */
luaL_checkany(L, 1);
} else {
size_t l;
const char *s = luaL_checklstring(L, 1, &l);
const char *e = s + l; /* end point for 's' */
int base = luaL_checkint(L, 2);
int neg = 0;
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
s += strspn(s, SPACECHARS); /* skip initial spaces */
if (*s == '-') { s++; neg = 1; } /* handle signal */
else if (*s == '+') s++;
if (isalnum((unsigned char)*s)) {
lua_Number n = 0;
do {
int digit = (isdigit((unsigned char) * s)) ? *s - '0'
: toupper((unsigned char) * s) - 'A' + 10;
if (digit >= base) break; /* invalid numeral; force a fail */
n = n * (lua_Number)base + (lua_Number)digit;
s++;
} while (isalnum((unsigned char)*s));
s += strspn(s, SPACECHARS); /* skip trailing spaces */
if (s == e) { /* no invalid trailing characters? */
lua_pushnumber(L, (neg) ? -n : n);
return 1;
} /* else not a number */
} /* else not a number */
}
lua_pushnil(L); /* not a number */
return 1;
}
static int luaB_error(lua_State *L) {
int level = luaL_optint(L, 2, 1);
lua_settop(L, 1);
if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
luaL_where(L, level);
lua_pushvalue(L, 1);
lua_concat(L, 2);
}
return lua_error(L);
}
static int luaB_getmetatable(lua_State *L) {
luaL_checkany(L, 1);
if (!lua_getmetatable(L, 1)) {
lua_pushnil(L);
return 1; /* no metatable */
}
luaL_getmetafield(L, 1, "__metatable");
return 1; /* returns either __metatable field (if present) or metatable */
}
static int luaB_setmetatable(lua_State *L) {
int t = lua_type(L, 2);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
"nil or table expected");
if (luaL_getmetafield(L, 1, "__metatable"))
return luaL_error(L, "cannot change a protected metatable");
lua_settop(L, 2);
lua_setmetatable(L, 1);
return 1;
}
static int luaB_rawequal(lua_State *L) {
luaL_checkany(L, 1);
luaL_checkany(L, 2);
lua_pushboolean(L, lua_rawequal(L, 1, 2));
return 1;
}
static int luaB_rawlen(lua_State *L) {
int t = lua_type(L, 1);
luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
"table or string expected");
lua_pushinteger(L, lua_rawlen(L, 1));
return 1;
}
static int luaB_rawget(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
lua_settop(L, 2);
lua_rawget(L, 1);
return 1;
}
static int luaB_rawset(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
luaL_checkany(L, 3);
lua_settop(L, 3);
lua_rawset(L, 1);
return 1;
}
static int luaB_collectgarbage(lua_State *L) {
static const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "setpause", "setstepmul",
"setmajorinc", "isrunning", "generational", "incremental", NULL
};
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC
};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
int ex = luaL_optint(L, 2, 0);
int res = lua_gc(L, o, ex);
switch (o) {
case LUA_GCCOUNT: {
int b = lua_gc(L, LUA_GCCOUNTB, 0);
lua_pushnumber(L, res + ((lua_Number)b / 1024));
lua_pushinteger(L, b);
return 2;
}
case LUA_GCSTEP:
case LUA_GCISRUNNING: {
lua_pushboolean(L, res);
return 1;
}
default: {
lua_pushinteger(L, res);
return 1;
}
}
}
static int luaB_type(lua_State *L) {
luaL_checkany(L, 1);
lua_pushstring(L, luaL_typename(L, 1));
return 1;
}
static int pairsmeta(lua_State *L, const char *method, int iszero,
lua_CFunction iter) {
if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */
luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
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) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
if (lua_next(L, 1))
return 2;
else { else {
lua_pushnil(L); size_t l;
return 1; const char *s = lua_tolstring(L, 1, &l);
if (s != NULL && lua_stringtonumber(L, s) == l + 1)
return 1; /* successful conversion to number */
/* else not a number */
luaL_checkany(L, 1); /* (but there must be some parameter) */
} }
}
else {
size_t l;
const char *s;
lua_Integer n = 0; /* to avoid warnings */
lua_Integer base = luaL_checkinteger(L, 2);
luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
s = lua_tolstring(L, 1, &l);
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
if (b_str2int(s, (int)base, &n) == s + l) {
lua_pushinteger(L, n);
return 1;
} /* else not a number */
} /* else not a number */
luaL_pushfail(L); /* not a number */
return 1;
} }
static int luaB_pairs(lua_State *L) { static int luaB_error (lua_State *L) {
return pairsmeta(L, "__pairs", 0, luaB_next); int level = (int)luaL_optinteger(L, 2, 1);
lua_settop(L, 1);
if (lua_type(L, 1) == LUA_TSTRING && level > 0) {
luaL_where(L, level); /* add extra information */
lua_pushvalue(L, 1);
lua_concat(L, 2);
}
return lua_error(L);
} }
static int ipairsaux(lua_State *L) { static int luaB_getmetatable (lua_State *L) {
int i = luaL_checkint(L, 2); luaL_checkany(L, 1);
luaL_checktype(L, 1, LUA_TTABLE); if (!lua_getmetatable(L, 1)) {
i++; /* next value */ lua_pushnil(L);
lua_pushinteger(L, i); return 1; /* no metatable */
lua_rawgeti(L, 1, i); }
return (lua_isnil(L, -1)) ? 1 : 2; luaL_getmetafield(L, 1, "__metatable");
return 1; /* returns either __metatable field (if present) or metatable */
} }
static int luaB_ipairs(lua_State *L) { static int luaB_setmetatable (lua_State *L) {
return pairsmeta(L, "__ipairs", 1, ipairsaux); int t = lua_type(L, 2);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL))
return luaL_error(L, "cannot change a protected metatable");
lua_settop(L, 2);
lua_setmetatable(L, 1);
return 1;
} }
static int load_aux(lua_State *L, int status, int envidx) { static int luaB_rawequal (lua_State *L) {
if (status == LUA_OK) { luaL_checkany(L, 1);
if (envidx != 0) { /* 'env' parameter? */ luaL_checkany(L, 2);
lua_pushvalue(L, envidx); /* environment for loaded function */ lua_pushboolean(L, lua_rawequal(L, 1, 2));
if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ return 1;
lua_pop(L, 1); /* remove 'env' if not used by previous call */ }
}
return 1;
} else { /* error (message is on top of the stack) */ static int luaB_rawlen (lua_State *L) {
lua_pushnil(L); int t = lua_type(L, 1);
lua_insert(L, -2); /* put before error message */ luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
return 2; /* return nil plus error message */ "table or string");
lua_pushinteger(L, lua_rawlen(L, 1));
return 1;
}
static int luaB_rawget (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
lua_settop(L, 2);
lua_rawget(L, 1);
return 1;
}
static int luaB_rawset (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
luaL_checkany(L, 3);
lua_settop(L, 3);
lua_rawset(L, 1);
return 1;
}
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 const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "setpause", "setstepmul",
"isrunning", "generational", "incremental", NULL};
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
switch (o) {
case LUA_GCCOUNT: {
int k = lua_gc(L, o);
int b = lua_gc(L, LUA_GCCOUNTB);
checkvalres(k);
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
return 1;
} }
case LUA_GCSTEP: {
int step = (int)luaL_optinteger(L, 2, 0);
int res = lua_gc(L, o, step);
checkvalres(res);
lua_pushboolean(L, res);
return 1;
}
case LUA_GCSETPAUSE:
case LUA_GCSETSTEPMUL: {
int p = (int)luaL_optinteger(L, 2, 0);
int previous = lua_gc(L, o, p);
checkvalres(previous);
lua_pushinteger(L, previous);
return 1;
}
case LUA_GCISRUNNING: {
int res = lua_gc(L, o);
checkvalres(res);
lua_pushboolean(L, res);
return 1;
}
case LUA_GCGEN: {
int minormul = (int)luaL_optinteger(L, 2, 0);
int majormul = (int)luaL_optinteger(L, 3, 0);
return pushmode(L, lua_gc(L, o, minormul, majormul));
}
case LUA_GCINC: {
int pause = (int)luaL_optinteger(L, 2, 0);
int stepmul = (int)luaL_optinteger(L, 3, 0);
int stepsize = (int)luaL_optinteger(L, 4, 0);
return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize));
}
default: {
int res = lua_gc(L, o);
checkvalres(res);
lua_pushinteger(L, res);
return 1;
}
}
luaL_pushfail(L); /* invalid call (inside a finalizer) */
return 1;
} }
static int luaB_loadfile(lua_State *L) { static int luaB_type (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL); int t = lua_type(L, 1);
const char *mode = luaL_optstring(L, 2, NULL); luaL_argcheck(L, t != LUA_TNONE, 1, "value expected");
int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ lua_pushstring(L, lua_typename(L, t));
int status = luaL_loadfilex(L, fname, mode); return 1;
return load_aux(L, status, env); }
static int luaB_next (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
if (lua_next(L, 1))
return 2;
else {
lua_pushnil(L);
return 1;
}
}
static int pairscont (lua_State *L, int status, lua_KContext k) {
(void)L; (void)status; (void)k; /* unused */
return 3;
}
static int luaB_pairs (lua_State *L) {
luaL_checkany(L, 1);
if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
lua_pushcfunction(L, luaB_next); /* will return generator, */
lua_pushvalue(L, 1); /* state, */
lua_pushnil(L); /* and initial value */
}
else {
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */
}
return 3;
}
/*
** Traversal function for 'ipairs'
*/
static int ipairsaux (lua_State *L) {
lua_Integer i = luaL_checkinteger(L, 2);
i = luaL_intop(+, i, 1);
lua_pushinteger(L, i);
return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
}
/*
** 'ipairs' function. Returns 'ipairsaux', given "table", 0.
** (The given "table" may not be a table.)
*/
static int luaB_ipairs (lua_State *L) {
luaL_checkany(L, 1);
lua_pushcfunction(L, ipairsaux); /* iteration function */
lua_pushvalue(L, 1); /* state */
lua_pushinteger(L, 0); /* initial value */
return 3;
}
static int load_aux (lua_State *L, int status, int envidx) {
if (l_likely(status == LUA_OK)) {
if (envidx != 0) { /* 'env' parameter? */
lua_pushvalue(L, envidx); /* environment for loaded function */
if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
lua_pop(L, 1); /* remove 'env' if not used by previous call */
}
return 1;
}
else { /* error (message is on top of the stack) */
luaL_pushfail(L);
lua_insert(L, -2); /* put before error message */
return 2; /* return fail plus error message */
}
}
static int luaB_loadfile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
const char *mode = luaL_optstring(L, 2, NULL);
int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
int status = luaL_loadfilex(L, fname, mode);
return load_aux(L, status, env);
} }
@ -280,176 +358,192 @@ static int luaB_loadfile(lua_State *L) {
** string to avoid it being collected while parsed. 'load' has four ** string to avoid it being collected while parsed. 'load' has four
** optional arguments (chunk, source name, mode, and environment). ** optional arguments (chunk, source name, mode, and environment).
*/ */
#define RESERVEDSLOT 5 #define RESERVEDSLOT 5
/* /*
** Reader for generic `load' function: `lua_load' uses the ** Reader for generic 'load' function: 'lua_load' uses the
** stack for internal stuff, so the reader cannot change the ** stack for internal stuff, so the reader cannot change the
** stack top. Instead, it keeps its resulting string in a ** stack top. Instead, it keeps its resulting string in a
** reserved slot inside the stack. ** reserved slot inside the stack.
*/ */
static const char *generic_reader(lua_State *L, void *ud, size_t *size) { static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
(void)(ud); /* not used */ (void)(ud); /* not used */
luaL_checkstack(L, 2, "too many nested functions"); luaL_checkstack(L, 2, "too many nested functions");
lua_pushvalue(L, 1); /* get function */ lua_pushvalue(L, 1); /* get function */
lua_call(L, 0, 1); /* call it */ lua_call(L, 0, 1); /* call it */
if (lua_isnil(L, -1)) { if (lua_isnil(L, -1)) {
lua_pop(L, 1); /* pop result */ lua_pop(L, 1); /* pop result */
*size = 0; *size = 0;
return NULL; return NULL;
} else if (!lua_isstring(L, -1)) }
luaL_error(L, "reader function must return a string"); else if (l_unlikely(!lua_isstring(L, -1)))
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ luaL_error(L, "reader function must return a string");
return lua_tolstring(L, RESERVEDSLOT, size); lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
return lua_tolstring(L, RESERVEDSLOT, size);
} }
static int luaB_load(lua_State *L) { static int luaB_load (lua_State *L) {
int status; int status;
size_t l; size_t l;
const char *s = lua_tolstring(L, 1, &l); const char *s = lua_tolstring(L, 1, &l);
const char *mode = luaL_optstring(L, 3, "bt"); const char *mode = luaL_optstring(L, 3, "bt");
int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
if (s != NULL) { /* loading a string? */ if (s != NULL) { /* loading a string? */
const char *chunkname = luaL_optstring(L, 2, s); const char *chunkname = luaL_optstring(L, 2, s);
status = luaL_loadbufferx(L, s, l, chunkname, mode); status = luaL_loadbufferx(L, s, l, chunkname, mode);
} else { /* loading from a reader function */ }
const char *chunkname = luaL_optstring(L, 2, "=(load)"); else { /* loading from a reader function */
luaL_checktype(L, 1, LUA_TFUNCTION); const char *chunkname = luaL_optstring(L, 2, "=(load)");
lua_settop(L, RESERVEDSLOT); /* create reserved slot */ luaL_checktype(L, 1, LUA_TFUNCTION);
status = lua_load(L, generic_reader, NULL, chunkname, mode); lua_settop(L, RESERVEDSLOT); /* create reserved slot */
} status = lua_load(L, generic_reader, NULL, chunkname, mode);
return load_aux(L, status, env); }
return load_aux(L, status, env);
} }
/* }====================================================== */ /* }====================================================== */
static int dofilecont(lua_State *L) { static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
return lua_gettop(L) - 1; (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
return lua_gettop(L) - 1;
} }
static int luaB_dofile(lua_State *L) { static int luaB_dofile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL); const char *fname = luaL_optstring(L, 1, NULL);
lua_settop(L, 1); lua_settop(L, 1);
if (luaL_loadfile(L, fname) != LUA_OK) if (l_unlikely(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); return dofilecont(L, 0, 0);
} }
static int luaB_assert(lua_State *L) { static int luaB_assert (lua_State *L) {
if (!lua_toboolean(L, 1)) if (l_likely(lua_toboolean(L, 1))) /* condition is true? */
return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); return lua_gettop(L); /* return all arguments */
return lua_gettop(L); else { /* error */
luaL_checkany(L, 1); /* there must be a condition */
lua_remove(L, 1); /* remove it */
lua_pushliteral(L, "assertion failed!"); /* default message */
lua_settop(L, 1); /* leave only message (default if no other one) */
return luaB_error(L); /* call 'error' */
}
} }
static int luaB_select(lua_State *L) { static int luaB_select (lua_State *L) {
int n = lua_gettop(L); int n = lua_gettop(L);
if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
lua_pushinteger(L, n - 1); lua_pushinteger(L, n-1);
return 1;
} else {
int i = luaL_checkint(L, 1);
if (i < 0) i = n + i;
else if (i > n) i = n;
luaL_argcheck(L, 1 <= i, 1, "index out of range");
return n - i;
}
}
static int finishpcall(lua_State *L, int status) {
if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */
lua_settop(L, 0); /* create space for return values */
lua_pushboolean(L, 0);
lua_pushstring(L, "stack overflow");
return 2; /* return false, msg */
}
lua_pushboolean(L, status); /* first result (status) */
lua_replace(L, 1); /* put first result in first slot */
return lua_gettop(L);
}
static int pcallcont(lua_State *L) {
int status = lua_getctx(L, NULL);
return finishpcall(L, (status == LUA_YIELD));
}
static int luaB_pcall(lua_State *L) {
int status;
luaL_checkany(L, 1);
lua_pushnil(L);
lua_insert(L, 1); /* create space for status result */
status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont);
return finishpcall(L, (status == LUA_OK));
}
static int luaB_xpcall(lua_State *L) {
int status;
int n = lua_gettop(L);
luaL_argcheck(L, n >= 2, 2, "value expected");
lua_pushvalue(L, 1); /* exchange function... */
lua_copy(L, 2, 1); /* ...and error handler */
lua_replace(L, 2);
status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont);
return finishpcall(L, (status == LUA_OK));
}
static int luaB_tostring(lua_State *L) {
luaL_checkany(L, 1);
luaL_tolstring(L, 1, NULL);
return 1; return 1;
}
else {
lua_Integer i = luaL_checkinteger(L, 1);
if (i < 0) i = n + i;
else if (i > n) i = n;
luaL_argcheck(L, 1 <= i, 1, "index out of range");
return n - (int)i;
}
}
/*
** Continuation function for 'pcall' and 'xpcall'. Both functions
** already pushed a 'true' before doing the call, so in case of success
** 'finishpcall' only has to return everything in the stack minus
** 'extra' values (where 'extra' is exactly the number of items to be
** ignored).
*/
static int finishpcall (lua_State *L, int status, lua_KContext extra) {
if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */
lua_pushboolean(L, 0); /* first result (false) */
lua_pushvalue(L, -2); /* error message */
return 2; /* return false, msg */
}
else
return lua_gettop(L) - (int)extra; /* return all results */
}
static int luaB_pcall (lua_State *L) {
int status;
luaL_checkany(L, 1);
lua_pushboolean(L, 1); /* first result if no errors */
lua_insert(L, 1); /* put it in place */
status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
return finishpcall(L, status, 0);
}
/*
** Do a protected call with error handling. After 'lua_rotate', the
** stack will have <f, err, true, f, [args...]>; so, the function passes
** 2 to 'finishpcall' to skip the 2 first values when returning results.
*/
static int luaB_xpcall (lua_State *L) {
int status;
int n = lua_gettop(L);
luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */
lua_pushboolean(L, 1); /* first result */
lua_pushvalue(L, 1); /* function */
lua_rotate(L, 3, 2); /* move them below function's arguments */
status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
return finishpcall(L, status, 2);
}
static int luaB_tostring (lua_State *L) {
luaL_checkany(L, 1);
luaL_tolstring(L, 1, NULL);
return 1;
} }
static const luaL_Reg base_funcs[] = { static const luaL_Reg base_funcs[] = {
{"assert", luaB_assert}, {"assert", luaB_assert},
{"collectgarbage", luaB_collectgarbage}, {"collectgarbage", luaB_collectgarbage},
{"dofile", luaB_dofile}, {"dofile", luaB_dofile},
{"error", luaB_error}, {"error", luaB_error},
{"getmetatable", luaB_getmetatable}, {"getmetatable", luaB_getmetatable},
{"ipairs", luaB_ipairs}, {"ipairs", luaB_ipairs},
{"loadfile", luaB_loadfile}, {"loadfile", luaB_loadfile},
{"load", luaB_load}, {"load", luaB_load},
#if defined(LUA_COMPAT_LOADSTRING) {"next", luaB_next},
{"loadstring", luaB_load}, {"pairs", luaB_pairs},
#endif {"pcall", luaB_pcall},
{"next", luaB_next}, {"print", luaB_print},
{"pairs", luaB_pairs}, {"warn", luaB_warn},
{"pcall", luaB_pcall}, {"rawequal", luaB_rawequal},
{"print", luaB_print}, {"rawlen", luaB_rawlen},
{"rawequal", luaB_rawequal}, {"rawget", luaB_rawget},
{"rawlen", luaB_rawlen}, {"rawset", luaB_rawset},
{"rawget", luaB_rawget}, {"select", luaB_select},
{"rawset", luaB_rawset}, {"setmetatable", luaB_setmetatable},
{"select", luaB_select}, {"tonumber", luaB_tonumber},
{"setmetatable", luaB_setmetatable}, {"tostring", luaB_tostring},
{"tonumber", luaB_tonumber}, {"type", luaB_type},
{"tostring", luaB_tostring}, {"xpcall", luaB_xpcall},
{"type", luaB_type}, /* placeholders */
{"xpcall", luaB_xpcall}, {LUA_GNAME, NULL},
{NULL, NULL} {"_VERSION", NULL},
{NULL, NULL}
}; };
LUAMOD_API int luaopen_base(lua_State *L) { LUAMOD_API int luaopen_base (lua_State *L) {
/* set global _G */ /* open lib into global table */
lua_pushglobaltable(L); lua_pushglobaltable(L);
lua_pushglobaltable(L); luaL_setfuncs(L, base_funcs, 0);
lua_setfield(L, -2, "_G"); /* set global _G */
/* open lib into global table */ lua_pushvalue(L, -1);
luaL_setfuncs(L, base_funcs, 0); lua_setfield(L, -2, LUA_GNAME);
lua_pushliteral(L, LUA_VERSION); /* set global _VERSION */
lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ lua_pushliteral(L, LUA_VERSION);
return 1; lua_setfield(L, -2, "_VERSION");
return 1;
} }

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.h,v 1.58 2011/08/30 16:26:41 roberto Exp $ ** $Id: lcode.h $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -24,60 +24,78 @@
** 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 {
OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, /* arithmetic operators */
OPR_CONCAT, OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
OPR_EQ, OPR_LT, OPR_LE, OPR_DIV, OPR_IDIV,
OPR_NE, OPR_GT, OPR_GE, /* bitwise operators */
OPR_AND, OPR_OR, OPR_BAND, OPR_BOR, OPR_BXOR,
OPR_NOBINOPR OPR_SHL, OPR_SHR,
/* string operator */
OPR_CONCAT,
/* comparison operators */
OPR_EQ, OPR_LT, OPR_LE,
OPR_NE, OPR_GT, OPR_GE,
/* logical operators */
OPR_AND, OPR_OR,
OPR_NOBINOPR
} BinOpr; } BinOpr;
typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; /* true if operation is foldable (that is, it is arithmetic or bitwise) */
#define foldbinop(op) ((op) <= OPR_SHR)
#define getcode(fs,e) ((fs)->f->code[(e)->u.info]) #define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0)
#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) typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
LUAI_FUNC int luaK_codeABx(FuncState *fs, OpCode o, int A, unsigned int Bx); /* get (pointer to) instruction of given 'expdesc' */
LUAI_FUNC int luaK_codeABC(FuncState *fs, OpCode o, int A, int B, int C); #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info])
LUAI_FUNC int luaK_codek(FuncState *fs, int reg, int k);
LUAI_FUNC void luaK_fixline(FuncState *fs, int line);
LUAI_FUNC void luaK_nil(FuncState *fs, int from, int n); #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET)
LUAI_FUNC void luaK_reserveregs(FuncState *fs, int n);
LUAI_FUNC void luaK_checkstack(FuncState *fs, int n); #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
LUAI_FUNC int luaK_stringK(FuncState *fs, TString *s);
LUAI_FUNC int luaK_numberK(FuncState *fs, lua_Number r); LUAI_FUNC int luaK_code (FuncState *fs, Instruction i);
LUAI_FUNC void luaK_dischargevars(FuncState *fs, expdesc *e); LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
LUAI_FUNC int luaK_exp2anyreg(FuncState *fs, expdesc *e); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A,
LUAI_FUNC void luaK_exp2anyregup(FuncState *fs, expdesc *e); int B, int C, int k);
LUAI_FUNC void luaK_exp2nextreg(FuncState *fs, expdesc *e); LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v);
LUAI_FUNC void luaK_exp2val(FuncState *fs, expdesc *e); LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
LUAI_FUNC int luaK_exp2RK(FuncState *fs, expdesc *e); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
LUAI_FUNC void luaK_self(FuncState *fs, expdesc *e, expdesc *key); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
LUAI_FUNC void luaK_indexed(FuncState *fs, expdesc *t, expdesc *k); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
LUAI_FUNC void luaK_goiftrue(FuncState *fs, expdesc *e); LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n);
LUAI_FUNC void luaK_goiffalse(FuncState *fs, expdesc *e); LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_storevar(FuncState *fs, expdesc *var, expdesc *e); LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_setreturns(FuncState *fs, expdesc *e, int nresults); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_setoneret(FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
LUAI_FUNC int luaK_jump(FuncState *fs); LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_ret(FuncState *fs, int first, int nret); LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
LUAI_FUNC void luaK_patchlist(FuncState *fs, int list, int target); LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
LUAI_FUNC void luaK_patchtohere(FuncState *fs, int list); LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_patchclose(FuncState *fs, int list, int level); LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_concat(FuncState *fs, int *l1, int l2); LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
LUAI_FUNC int luaK_getlabel(FuncState *fs); LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
LUAI_FUNC void luaK_prefix(FuncState *fs, UnOpr op, expdesc *v, int line); LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_infix(FuncState *fs, BinOpr op, expdesc *v); LUAI_FUNC int luaK_jump (FuncState *fs);
LUAI_FUNC void luaK_posfix(FuncState *fs, BinOpr op, expdesc *v1, LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
expdesc *v2, int line); LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
LUAI_FUNC void luaK_setlist(FuncState *fs, int base, int nelems, int tostore); LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
LUAI_FUNC int luaK_getlabel (FuncState *fs);
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_posfix (FuncState *fs, BinOpr op, expdesc *v1,
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_finish (FuncState *fs);
LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg);
#endif #endif

View file

@ -1,153 +1,210 @@
/* /*
** $Id: lcorolib.c,v 1.5 2013/02/21 13:44:53 roberto Exp $ ** $Id: lcorolib.c $
** Coroutine Library ** Coroutine Library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <stdlib.h>
#define lcorolib_c #define lcorolib_c
#define LUA_LIB #define LUA_LIB
#include "lprefix.h"
#include <stdlib.h>
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lualib.h" #include "lualib.h"
static int auxresume(lua_State *L, lua_State *co, int narg) { static lua_State *getco (lua_State *L) {
int status; lua_State *co = lua_tothread(L, 1);
if (!lua_checkstack(co, narg)) { luaL_argexpected(L, co, 1, "thread");
lua_pushliteral(L, "too many arguments to resume"); return co;
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);
status = lua_resume(co, L, narg);
if (status == LUA_OK || status == LUA_YIELD) {
int nres = lua_gettop(co);
if (!lua_checkstack(L, nres + 1)) {
lua_pop(co, nres); /* remove results anyway */
lua_pushliteral(L, "too many results to resume");
return -1; /* error flag */
}
lua_xmove(co, L, nres); /* move yielded values */
return nres;
} else {
lua_xmove(co, L, 1); /* move error message */
return -1; /* error flag */
}
} }
static int luaB_coresume(lua_State *L) { /*
lua_State *co = lua_tothread(L, 1); ** Resumes a coroutine. Returns the number of results for non-error
int r; ** cases or -1 for errors.
luaL_argcheck(L, co, 1, "coroutine expected"); */
r = auxresume(L, co, lua_gettop(L) - 1); static int auxresume (lua_State *L, lua_State *co, int narg) {
if (r < 0) { int status, nres;
lua_pushboolean(L, 0); if (l_unlikely(!lua_checkstack(co, narg))) {
lua_insert(L, -2); lua_pushliteral(L, "too many arguments to resume");
return 2; /* return false + error message */ return -1; /* error flag */
} else { }
lua_xmove(L, co, narg);
status = lua_resume(co, L, narg, &nres);
if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
if (l_unlikely(!lua_checkstack(L, nres + 1))) {
lua_pop(co, nres); /* remove results anyway */
lua_pushliteral(L, "too many results to resume");
return -1; /* error flag */
}
lua_xmove(co, L, nres); /* move yielded values */
return nres;
}
else {
lua_xmove(co, L, 1); /* move error message */
return -1; /* error flag */
}
}
static int luaB_coresume (lua_State *L) {
lua_State *co = getco(L);
int r;
r = auxresume(L, co, lua_gettop(L) - 1);
if (l_unlikely(r < 0)) {
lua_pushboolean(L, 0);
lua_insert(L, -2);
return 2; /* return false + error message */
}
else {
lua_pushboolean(L, 1);
lua_insert(L, -(r + 1));
return r + 1; /* return true + 'resume' returns */
}
}
static int luaB_auxwrap (lua_State *L) {
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
int r = auxresume(L, co, lua_gettop(L));
if (l_unlikely(r < 0)) { /* error? */
int stat = lua_status(co);
if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
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_concat(L, 2);
}
return lua_error(L); /* propagate error */
}
return r;
}
static int luaB_cocreate (lua_State *L) {
lua_State *NL;
luaL_checktype(L, 1, LUA_TFUNCTION);
NL = lua_newthread(L);
lua_pushvalue(L, 1); /* move function to top */
lua_xmove(L, NL, 1); /* move function from L to NL */
return 1;
}
static int luaB_cowrap (lua_State *L) {
luaB_cocreate(L);
lua_pushcclosure(L, luaB_auxwrap, 1);
return 1;
}
static int luaB_yield (lua_State *L) {
return lua_yield(L, lua_gettop(L));
}
#define COS_RUN 0
#define COS_DEAD 1
#define COS_YIELD 2
#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 {
switch (lua_status(co)) {
case LUA_YIELD:
return COS_YIELD;
case LUA_OK: {
lua_Debug ar;
if (lua_getstack(co, 0, &ar)) /* does it have frames? */
return COS_NORM; /* it is running */
else if (lua_gettop(co) == 0)
return COS_DEAD;
else
return COS_YIELD; /* initial state */
}
default: /* some error occurred */
return COS_DEAD;
}
}
}
static int luaB_costatus (lua_State *L) {
lua_State *co = getco(L);
lua_pushstring(L, statname[auxstatus(L, co)]);
return 1;
}
static int luaB_yieldable (lua_State *L) {
lua_State *co = lua_isnone(L, 1) ? L : getco(L);
lua_pushboolean(L, lua_isyieldable(co));
return 1;
}
static int luaB_corunning (lua_State *L) {
int ismain = lua_pushthread(L);
lua_pushboolean(L, ismain);
return 2;
}
static int luaB_close (lua_State *L) {
lua_State *co = getco(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); lua_pushboolean(L, 1);
lua_insert(L, -(r + 1)); return 1;
return r + 1; /* return true + `resume' returns */ }
else {
lua_pushboolean(L, 0);
lua_xmove(co, L, 1); /* move error message */
return 2;
}
} }
} default: /* normal or running coroutine */
return luaL_error(L, "cannot close a %s coroutine", statname[status]);
}
static int luaB_auxwrap(lua_State *L) {
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
int r = auxresume(L, co, lua_gettop(L));
if (r < 0) {
if (lua_isstring(L, -1)) { /* error object is a string? */
luaL_where(L, 1); /* add extra info */
lua_insert(L, -2);
lua_concat(L, 2);
}
return lua_error(L); /* propagate error */
}
return r;
}
static int luaB_cocreate(lua_State *L) {
lua_State *NL;
luaL_checktype(L, 1, LUA_TFUNCTION);
NL = lua_newthread(L);
lua_pushvalue(L, 1); /* move function to top */
lua_xmove(L, NL, 1); /* move function from L to NL */
return 1;
}
static int luaB_cowrap(lua_State *L) {
luaB_cocreate(L);
lua_pushcclosure(L, luaB_auxwrap, 1);
return 1;
}
static int luaB_yield(lua_State *L) {
return lua_yield(L, lua_gettop(L));
}
static int luaB_costatus(lua_State *L) {
lua_State *co = lua_tothread(L, 1);
luaL_argcheck(L, co, 1, "coroutine expected");
if (L == co) lua_pushliteral(L, "running");
else {
switch (lua_status(co)) {
case LUA_YIELD:
lua_pushliteral(L, "suspended");
break;
case LUA_OK: {
lua_Debug ar;
if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
lua_pushliteral(L, "normal"); /* it is running */
else if (lua_gettop(co) == 0)
lua_pushliteral(L, "dead");
else
lua_pushliteral(L, "suspended"); /* initial state */
break;
}
default: /* some error occurred */
lua_pushliteral(L, "dead");
break;
}
}
return 1;
}
static int luaB_corunning(lua_State *L) {
int ismain = lua_pushthread(L);
lua_pushboolean(L, ismain);
return 2;
} }
static const luaL_Reg co_funcs[] = { static const luaL_Reg co_funcs[] = {
{"create", luaB_cocreate}, {"create", luaB_cocreate},
{"resume", luaB_coresume}, {"resume", luaB_coresume},
{"running", luaB_corunning}, {"running", luaB_corunning},
{"status", luaB_costatus}, {"status", luaB_costatus},
{"wrap", luaB_cowrap}, {"wrap", luaB_cowrap},
{"yield", luaB_yield}, {"yield", luaB_yield},
{NULL, NULL} {"isyieldable", luaB_yieldable},
{"close", luaB_close},
{NULL, NULL}
}; };
LUAMOD_API int luaopen_coroutine(lua_State *L) { LUAMOD_API int luaopen_coroutine (lua_State *L) {
luaL_newlib(L, co_funcs); luaL_newlib(L, co_funcs);
return 1; return 1;
} }

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lctype.c,v 1.11 2011/10/03 16:19:23 roberto Exp $ ** $Id: lctype.c $
** 'ctype' functions for Lua ** 'ctype' functions for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -7,46 +7,58 @@
#define lctype_c #define lctype_c
#define LUA_CORE #define LUA_CORE
#include "lprefix.h"
#include "lctype.h" #include "lctype.h"
#if !LUA_USE_CTYPE /* { */ #if !LUA_USE_CTYPE /* { */
#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. */
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */
0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05,
0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */
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,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; };
#endif /* } */ #endif /* } */

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ ** $Id: lctype.h $
** 'ctype' functions for Lua ** 'ctype' functions for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,65 +13,71 @@
/* /*
** 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)
#if 'A' == 65 && '0' == 48 #if 'A' == 65 && '0' == 48
/* ASCII case: can use its own tables; faster and fixed */ /* ASCII case: can use its own tables; faster and fixed */
#define LUA_USE_CTYPE 0 #define LUA_USE_CTYPE 0
#else #else
/* must use standard C ctype */ /* must use standard C ctype */
#define LUA_USE_CTYPE 1 #define LUA_USE_CTYPE 1
#endif #endif
#endif #endif
#if !LUA_USE_CTYPE /* { */ #if !LUA_USE_CTYPE /* { */
#include <limits.h> #include <limits.h>
#include "llimits.h" #include "llimits.h"
#define ALPHABIT 0 #define ALPHABIT 0
#define DIGITBIT 1 #define DIGITBIT 1
#define PRINTBIT 2 #define PRINTBIT 2
#define SPACEBIT 3 #define SPACEBIT 3
#define XDIGITBIT 4 #define XDIGITBIT 4
#define MASK(B) (1 << (B)) #define MASK(B) (1 << (B))
/* /*
** add 1 to char to allow index -1 (EOZ) ** add 1 to char to allow index -1 (EOZ)
*/ */
#define testprop(c,p) (luai_ctype_[(c)+1] & (p)) #define testprop(c,p) (luai_ctype_[(c)+1] & (p))
/* /*
** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_'
*/ */
#define lislalpha(c) testprop(c, MASK(ALPHABIT)) #define lislalpha(c) testprop(c, MASK(ALPHABIT))
#define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
#define lisdigit(c) testprop(c, MASK(DIGITBIT)) #define lisdigit(c) testprop(c, MASK(DIGITBIT))
#define lisspace(c) testprop(c, MASK(SPACEBIT)) #define lisspace(c) testprop(c, MASK(SPACEBIT))
#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))
/* /*
** this 'ltolower' only works for alphabetic characters ** In ASCII, this 'ltolower' is correct for alphabetic characters and
** 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) ((c) | ('A' ^ 'a')) #define ltolower(c) \
check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \
(c) | ('A' ^ 'a'))
/* two more entries for 0 and -1 (EOZ) */ /* one entry for each character and for -1 (EOZ) */
LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];)
#else /* }{ */ #else /* }{ */
/* /*
** use standard C ctypes ** use standard C ctypes
@ -80,16 +86,16 @@ LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];
#include <ctype.h> #include <ctype.h>
#define lislalpha(c) (isalpha(c) || (c) == '_') #define lislalpha(c) (isalpha(c) || (c) == '_')
#define lislalnum(c) (isalnum(c) || (c) == '_') #define lislalnum(c) (isalnum(c) || (c) == '_')
#define lisdigit(c) (isdigit(c)) #define lisdigit(c) (isdigit(c))
#define lisspace(c) (isspace(c)) #define lisspace(c) (isspace(c))
#define lisprint(c) (isprint(c)) #define lisprint(c) (isprint(c))
#define lisxdigit(c) (isxdigit(c)) #define lisxdigit(c) (isxdigit(c))
#define ltolower(c) (tolower(c)) #define ltolower(c) (tolower(c))
#endif /* } */ #endif /* } */
#endif #endif

View file

@ -1,405 +1,483 @@
/* /*
** $Id: ldblib.c,v 1.132.1.1 2013/04/12 18:48:47 roberto Exp roberto $ ** $Id: ldblib.c $
** 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
*/ */
#define ldblib_c
#define LUA_LIB
#include "lprefix.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define ldblib_c
#define LUA_LIB
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lualib.h" #include "lualib.h"
#define HOOKKEY "_HKEY" /*
** The hook table at registry[HOOKKEY] maps threads to their current
** hook function.
*/
static const char *const HOOKKEY = "_HOOKKEY";
static void checkstack(lua_State *L, lua_State *L1, int n) {
if (L != L1 && !lua_checkstack(L1, n)) /*
luaL_error(L, "stack overflow"); ** If L1 != L, L1 can be in any state, and therefore there are no
** guarantees about its stack space; any push in L1 must be
** checked.
*/
static void checkstack (lua_State *L, lua_State *L1, int n) {
if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
luaL_error(L, "stack overflow");
} }
static int db_getregistry(lua_State *L) { static int db_getregistry (lua_State *L) {
lua_pushvalue(L, LUA_REGISTRYINDEX); lua_pushvalue(L, LUA_REGISTRYINDEX);
return 1; return 1;
} }
static int db_getmetatable(lua_State *L) { static int db_getmetatable (lua_State *L) {
luaL_checkany(L, 1); luaL_checkany(L, 1);
if (!lua_getmetatable(L, 1)) { if (!lua_getmetatable(L, 1)) {
lua_pushnil(L); /* no metatable */ lua_pushnil(L); /* no metatable */
} }
return 1; return 1;
} }
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_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
"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 */
} }
static int db_getuservalue(lua_State *L) { static int db_getuservalue (lua_State *L) {
if (lua_type(L, 1) != LUA_TUSERDATA) int n = (int)luaL_optinteger(L, 2, 1);
lua_pushnil(L); if (lua_type(L, 1) != LUA_TUSERDATA)
else luaL_pushfail(L);
lua_getuservalue(L, 1); else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
return 1; lua_pushboolean(L, 1);
return 2;
}
return 1;
} }
static int db_setuservalue(lua_State *L) { static int db_setuservalue (lua_State *L) {
if (lua_type(L, 1) == LUA_TLIGHTUSERDATA) int n = (int)luaL_optinteger(L, 3, 1);
luaL_argerror(L, 1, "full userdata expected, got light userdata"); luaL_checktype(L, 1, LUA_TUSERDATA);
luaL_checktype(L, 1, LUA_TUSERDATA); luaL_checkany(L, 2);
if (!lua_isnoneornil(L, 2)) lua_settop(L, 2);
luaL_checktype(L, 2, LUA_TTABLE); if (!lua_setiuservalue(L, 1, n))
lua_settop(L, 2); luaL_pushfail(L);
lua_setuservalue(L, 1); return 1;
return 1;
} }
static void settabss(lua_State *L, const char *i, const char *v) { /*
lua_pushstring(L, v); ** Auxiliary function used by several library functions: check for
lua_setfield(L, -2, i); ** an optional thread as function's first argument and set 'arg' with
** 1 if this argument is present (so that functions can skip it to
** access their other arguments)
*/
static lua_State *getthread (lua_State *L, int *arg) {
if (lua_isthread(L, 1)) {
*arg = 1;
return lua_tothread(L, 1);
}
else {
*arg = 0;
return L; /* function will operate over current thread */
}
} }
static void settabsi(lua_State *L, const char *i, int v) { /*
lua_pushinteger(L, v); ** Variations of 'lua_settable', used by 'db_getinfo' to put results
lua_setfield(L, -2, i); ** from 'lua_getinfo' into result table. Key is always a string;
** value can be a string, an int, or a boolean.
*/
static void settabss (lua_State *L, const char *k, const char *v) {
lua_pushstring(L, v);
lua_setfield(L, -2, k);
}
static void settabsi (lua_State *L, const char *k, int v) {
lua_pushinteger(L, v);
lua_setfield(L, -2, k);
}
static void settabsb (lua_State *L, const char *k, int v) {
lua_pushboolean(L, v);
lua_setfield(L, -2, k);
} }
static void settabsb(lua_State *L, const char *i, int v) { /*
lua_pushboolean(L, v); ** In function 'db_getinfo', the call to 'lua_getinfo' may push
lua_setfield(L, -2, i); ** results on the stack; later it creates the result table to put
** these objects. Function 'treatstackoption' puts the result from
** 'lua_getinfo' on top of the result table so that it can call
** 'lua_setfield'.
*/
static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
if (L == L1)
lua_rotate(L, -2, 1); /* exchange object and table */
else
lua_xmove(L1, L, 1); /* move object to the "main" stack */
lua_setfield(L, -2, fname); /* put object into table */
} }
static lua_State *getthread(lua_State *L, int *arg) { /*
if (lua_isthread(L, 1)) { ** Calls 'lua_getinfo' and collects all results in a new table.
*arg = 1; ** L1 needs stack space for an optional input (function) plus
return lua_tothread(L, 1); ** two optional outputs (function and line table) from function
} else { ** 'lua_getinfo'.
*arg = 0; */
return L; static int db_getinfo (lua_State *L) {
} lua_Debug ar;
} int arg;
lua_State *L1 = getthread(L, &arg);
const char *options = luaL_optstring(L, arg+2, "flnSrtu");
static void treatstackoption(lua_State *L, lua_State *L1, const char *fname) { checkstack(L, L1, 3);
if (L == L1) { luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
lua_pushvalue(L, -2); if (lua_isfunction(L, arg + 1)) { /* info about a function? */
lua_remove(L, -3); options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
} else lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
lua_xmove(L1, L, 1);
lua_setfield(L, -2, fname);
}
static int db_getinfo(lua_State *L) {
lua_Debug ar;
int arg;
lua_State *L1 = getthread(L, &arg);
const char *options = luaL_optstring(L, arg + 2, "flnStu");
checkstack(L, L1, 3);
if (lua_isnumber(L, arg + 1)) {
if (!lua_getstack(L1, (int)lua_tointeger(L, arg + 1), &ar)) {
lua_pushnil(L); /* level out of range */
return 1;
}
} else if (lua_isfunction(L, arg + 1)) {
lua_pushfstring(L, ">%s", options);
options = lua_tostring(L, -1);
lua_pushvalue(L, arg + 1);
lua_xmove(L, L1, 1);
} else
return luaL_argerror(L, arg + 1, "function or level expected");
if (!lua_getinfo(L1, options, &ar))
return luaL_argerror(L, arg + 2, "invalid option");
lua_createtable(L, 0, 2);
if (strchr(options, 'S')) {
settabss(L, "source", ar.source);
settabss(L, "short_src", ar.short_src);
settabsi(L, "linedefined", ar.linedefined);
settabsi(L, "lastlinedefined", ar.lastlinedefined);
settabss(L, "what", ar.what);
}
if (strchr(options, 'l'))
settabsi(L, "currentline", ar.currentline);
if (strchr(options, 'u')) {
settabsi(L, "nups", ar.nups);
settabsi(L, "nparams", ar.nparams);
settabsb(L, "isvararg", ar.isvararg);
}
if (strchr(options, 'n')) {
settabss(L, "name", ar.name);
settabss(L, "namewhat", ar.namewhat);
}
if (strchr(options, 't'))
settabsb(L, "istailcall", ar.istailcall);
if (strchr(options, 'L'))
treatstackoption(L, L1, "activelines");
if (strchr(options, 'f'))
treatstackoption(L, L1, "func");
return 1; /* return table */
}
static int db_getlocal(lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
lua_Debug ar;
const char *name;
int nvar = luaL_checkint(L, arg + 2); /* local-variable index */
if (lua_isfunction(L, arg + 1)) { /* function argument? */
lua_pushvalue(L, arg + 1); /* push function */
lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
return 1;
} else { /* stack-level argument */
if (!lua_getstack(L1, luaL_checkint(L, arg + 1), &ar)) /* out of range? */
return luaL_argerror(L, arg + 1, "level out of range");
checkstack(L, L1, 1);
name = lua_getlocal(L1, &ar, nvar);
if (name) {
lua_xmove(L1, L, 1); /* push local value */
lua_pushstring(L, name); /* push name */
lua_pushvalue(L, -2); /* re-order */
return 2;
} else {
lua_pushnil(L); /* no name (nor value) */
return 1;
}
}
}
static int db_setlocal(lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
lua_Debug ar;
if (!lua_getstack(L1, luaL_checkint(L, arg + 1), &ar)) /* out of range? */
return luaL_argerror(L, arg + 1, "level out of range");
luaL_checkany(L, arg + 3);
lua_settop(L, arg + 3);
checkstack(L, L1, 1);
lua_xmove(L, L1, 1); lua_xmove(L, L1, 1);
lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg + 2))); }
return 1; else { /* stack level */
if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
luaL_pushfail(L); /* level out of range */
return 1;
}
}
if (!lua_getinfo(L1, options, &ar))
return luaL_argerror(L, arg+2, "invalid option");
lua_newtable(L); /* table to collect results */
if (strchr(options, 'S')) {
lua_pushlstring(L, ar.source, ar.srclen);
lua_setfield(L, -2, "source");
settabss(L, "short_src", ar.short_src);
settabsi(L, "linedefined", ar.linedefined);
settabsi(L, "lastlinedefined", ar.lastlinedefined);
settabss(L, "what", ar.what);
}
if (strchr(options, 'l'))
settabsi(L, "currentline", ar.currentline);
if (strchr(options, 'u')) {
settabsi(L, "nups", ar.nups);
settabsi(L, "nparams", ar.nparams);
settabsb(L, "isvararg", ar.isvararg);
}
if (strchr(options, 'n')) {
settabss(L, "name", ar.name);
settabss(L, "namewhat", ar.namewhat);
}
if (strchr(options, 'r')) {
settabsi(L, "ftransfer", ar.ftransfer);
settabsi(L, "ntransfer", ar.ntransfer);
}
if (strchr(options, 't'))
settabsb(L, "istailcall", ar.istailcall);
if (strchr(options, 'L'))
treatstackoption(L, L1, "activelines");
if (strchr(options, 'f'))
treatstackoption(L, L1, "func");
return 1; /* return table */
} }
static int auxupvalue(lua_State *L, int get) { static int db_getlocal (lua_State *L) {
const char *name; int arg;
int n = luaL_checkint(L, 2); lua_State *L1 = getthread(L, &arg);
luaL_checktype(L, 1, LUA_TFUNCTION); int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); if (lua_isfunction(L, arg + 1)) { /* function argument? */
if (name == NULL) return 0; lua_pushvalue(L, arg + 1); /* push function */
lua_pushstring(L, name); lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
lua_insert(L, -(get + 1)); return 1; /* return only name (there is no value) */
return get + 1; }
} else { /* stack-level argument */
static int db_getupvalue(lua_State *L) {
return auxupvalue(L, 1);
}
static int db_setupvalue(lua_State *L) {
luaL_checkany(L, 3);
return auxupvalue(L, 0);
}
static int checkupval(lua_State *L, int argf, int argnup) {
lua_Debug ar; lua_Debug ar;
int nup = luaL_checkint(L, argnup); const char *name;
luaL_checktype(L, argf, LUA_TFUNCTION); int level = (int)luaL_checkinteger(L, arg + 1);
lua_pushvalue(L, argf); if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
lua_getinfo(L, ">u", &ar); return luaL_argerror(L, arg+1, "level out of range");
luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
return nup;
}
static int db_upvalueid(lua_State *L) {
int n = checkupval(L, 1, 2);
lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
return 1;
}
static int db_upvaluejoin(lua_State *L) {
int n1 = checkupval(L, 1, 2);
int n2 = checkupval(L, 3, 4);
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
lua_upvaluejoin(L, 1, n1, 3, n2);
return 0;
}
#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
static void hookf(lua_State *L, lua_Debug *ar) {
static const char *const hooknames[] =
{"call", "return", "line", "count", "tail call"};
gethooktable(L);
lua_pushthread(L);
lua_rawget(L, -2);
if (lua_isfunction(L, -1)) {
lua_pushstring(L, hooknames[(int)ar->event]);
if (ar->currentline >= 0)
lua_pushinteger(L, ar->currentline);
else lua_pushnil(L);
lua_assert(lua_getinfo(L, "lS", ar));
lua_call(L, 2, 0);
}
}
static int makemask(const char *smask, int count) {
int mask = 0;
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
if (count > 0) mask |= LUA_MASKCOUNT;
return mask;
}
static char *unmakemask(int mask, char *smask) {
int i = 0;
if (mask & LUA_MASKCALL) smask[i++] = 'c';
if (mask & LUA_MASKRET) smask[i++] = 'r';
if (mask & LUA_MASKLINE) smask[i++] = 'l';
smask[i] = '\0';
return smask;
}
static int db_sethook(lua_State *L) {
int arg, mask, count;
lua_Hook func;
lua_State *L1 = getthread(L, &arg);
if (lua_isnoneornil(L, arg + 1)) {
lua_settop(L, arg + 1);
func = NULL;
mask = 0;
count = 0; /* turn off hooks */
} else {
const char *smask = luaL_checkstring(L, arg + 2);
luaL_checktype(L, arg + 1, LUA_TFUNCTION);
count = luaL_optint(L, arg + 3, 0);
func = hookf;
mask = makemask(smask, count);
}
if (gethooktable(L) == 0) { /* creating hook table? */
lua_pushstring(L, "k");
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
lua_pushvalue(L, -1);
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
}
checkstack(L, L1, 1); checkstack(L, L1, 1);
lua_pushthread(L1); name = lua_getlocal(L1, &ar, nvar);
lua_xmove(L1, L, 1); if (name) {
lua_pushvalue(L, arg + 1); lua_xmove(L1, L, 1); /* move local value */
lua_rawset(L, -3); /* set new hook */ lua_pushstring(L, name); /* push name */
lua_sethook(L1, func, mask, count); /* set hooks */ lua_rotate(L, -2, 1); /* re-order */
return 0; return 2;
} }
static int db_gethook(lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
char buff[5];
int mask = lua_gethookmask(L1);
lua_Hook hook = lua_gethook(L1);
if (hook != NULL && hook != hookf) /* external hook? */
lua_pushliteral(L, "external hook");
else { else {
gethooktable(L); luaL_pushfail(L); /* no name (nor value) */
checkstack(L, L1, 1); return 1;
lua_pushthread(L1);
lua_xmove(L1, L, 1);
lua_rawget(L, -2); /* get hook */
lua_remove(L, -2); /* remove hook table */
} }
lua_pushstring(L, unmakemask(mask, buff)); }
lua_pushinteger(L, lua_gethookcount(L1));
return 3;
} }
static int db_debug(lua_State *L) { static int db_setlocal (lua_State *L) {
for (;;) { int arg;
char buffer[250]; const char *name;
luai_writestringerror("%s", "lua_debug> "); lua_State *L1 = getthread(L, &arg);
if (fgets(buffer, sizeof(buffer), stdin) == 0 || lua_Debug ar;
strcmp(buffer, "cont\n") == 0) int level = (int)luaL_checkinteger(L, arg + 1);
return 0; int nvar = (int)luaL_checkinteger(L, arg + 2);
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
lua_pcall(L, 0, 0, 0)) return luaL_argerror(L, arg+1, "level out of range");
luai_writestringerror("%s\n", lua_tostring(L, -1)); luaL_checkany(L, arg+3);
lua_settop(L, 0); /* remove eventual returns */ lua_settop(L, arg+3);
} checkstack(L, L1, 1);
lua_xmove(L, L1, 1);
name = lua_setlocal(L1, &ar, nvar);
if (name == NULL)
lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
lua_pushstring(L, name);
return 1;
} }
static int db_traceback(lua_State *L) { /*
int arg; ** get (if 'get' is true) or set an upvalue from a closure
lua_State *L1 = getthread(L, &arg); */
const char *msg = lua_tostring(L, arg + 1); static int auxupvalue (lua_State *L, int get) {
if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ const char *name;
lua_pushvalue(L, arg + 1); /* return it untouched */ int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
else { luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0); name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
luaL_traceback(L, L1, msg, level); if (name == NULL) return 0;
} lua_pushstring(L, name);
lua_insert(L, -(get+1)); /* no-op if get is false */
return get + 1;
}
static int db_getupvalue (lua_State *L) {
return auxupvalue(L, 1);
}
static int db_setupvalue (lua_State *L) {
luaL_checkany(L, 3);
return auxupvalue(L, 0);
}
/*
** Check whether a given upvalue from a given closure exists and
** returns its index
*/
static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
void *id;
int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
id = lua_upvalueid(L, argf, nup);
if (pnup) {
luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
*pnup = nup;
}
return id;
}
static int db_upvalueid (lua_State *L) {
void *id = checkupval(L, 1, 2, NULL);
if (id != NULL)
lua_pushlightuserdata(L, id);
else
luaL_pushfail(L);
return 1;
}
static int db_upvaluejoin (lua_State *L) {
int n1, n2;
checkupval(L, 1, 2, &n1);
checkupval(L, 3, 4, &n2);
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
lua_upvaluejoin(L, 1, n1, 3, n2);
return 0;
}
/*
** Call hook function registered at hook table for the current
** thread (if there is one)
*/
static void hookf (lua_State *L, lua_Debug *ar) {
static const char *const hooknames[] =
{"call", "return", "line", "count", "tail call"};
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
lua_pushthread(L);
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
if (ar->currentline >= 0)
lua_pushinteger(L, ar->currentline); /* push current line */
else lua_pushnil(L);
lua_assert(lua_getinfo(L, "lS", ar));
lua_call(L, 2, 0); /* call hook function */
}
}
/*
** Convert a string mask (for 'sethook') into a bit mask
*/
static int makemask (const char *smask, int count) {
int mask = 0;
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
if (count > 0) mask |= LUA_MASKCOUNT;
return mask;
}
/*
** Convert a bit mask (for 'gethook') into a string mask
*/
static char *unmakemask (int mask, char *smask) {
int i = 0;
if (mask & LUA_MASKCALL) smask[i++] = 'c';
if (mask & LUA_MASKRET) smask[i++] = 'r';
if (mask & LUA_MASKLINE) smask[i++] = 'l';
smask[i] = '\0';
return smask;
}
static int db_sethook (lua_State *L) {
int arg, mask, count;
lua_Hook func;
lua_State *L1 = getthread(L, &arg);
if (lua_isnoneornil(L, arg+1)) { /* no hook? */
lua_settop(L, arg+1);
func = NULL; mask = 0; count = 0; /* turn off hooks */
}
else {
const char *smask = luaL_checkstring(L, arg+2);
luaL_checktype(L, arg+1, LUA_TFUNCTION);
count = (int)luaL_optinteger(L, arg + 3, 0);
func = hookf; mask = makemask(smask, count);
}
if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
/* table just created; initialize it */
lua_pushliteral(L, "k");
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
lua_pushvalue(L, -1);
lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
}
checkstack(L, L1, 1);
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
lua_pushvalue(L, arg + 1); /* value (hook function) */
lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
lua_sethook(L1, func, mask, count);
return 0;
}
static int db_gethook (lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
char buff[5];
int mask = lua_gethookmask(L1);
lua_Hook hook = lua_gethook(L1);
if (hook == NULL) { /* no hook? */
luaL_pushfail(L);
return 1; return 1;
}
else if (hook != hookf) /* external hook? */
lua_pushliteral(L, "external hook");
else { /* hook table must exist */
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
checkstack(L, L1, 1);
lua_pushthread(L1); lua_xmove(L1, L, 1);
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
lua_remove(L, -2); /* remove hook table */
}
lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
return 3;
}
static int db_debug (lua_State *L) {
for (;;) {
char buffer[250];
lua_writestringerror("%s", "lua_debug> ");
if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
strcmp(buffer, "cont\n") == 0)
return 0;
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
lua_pcall(L, 0, 0, 0))
lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
lua_settop(L, 0); /* remove eventual returns */
}
}
static int db_traceback (lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
const char *msg = lua_tostring(L, arg + 1);
if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
lua_pushvalue(L, arg + 1); /* return it untouched */
else {
int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
luaL_traceback(L, L1, msg, level);
}
return 1;
}
static int db_setcstacklimit (lua_State *L) {
int limit = (int)luaL_checkinteger(L, 1);
int res = lua_setcstacklimit(L, limit);
lua_pushinteger(L, res);
return 1;
} }
static const luaL_Reg dblib[] = { static const luaL_Reg dblib[] = {
{"debug", db_debug}, {"debug", db_debug},
{"getuservalue", db_getuservalue}, {"getuservalue", db_getuservalue},
{"gethook", db_gethook}, {"gethook", db_gethook},
{"getinfo", db_getinfo}, {"getinfo", db_getinfo},
{"getlocal", db_getlocal}, {"getlocal", db_getlocal},
{"getregistry", db_getregistry}, {"getregistry", db_getregistry},
{"getmetatable", db_getmetatable}, {"getmetatable", db_getmetatable},
{"getupvalue", db_getupvalue}, {"getupvalue", db_getupvalue},
{"upvaluejoin", db_upvaluejoin}, {"upvaluejoin", db_upvaluejoin},
{"upvalueid", db_upvalueid}, {"upvalueid", db_upvalueid},
{"setuservalue", db_setuservalue}, {"setuservalue", db_setuservalue},
{"sethook", db_sethook}, {"sethook", db_sethook},
{"setlocal", db_setlocal}, {"setlocal", db_setlocal},
{"setmetatable", db_setmetatable}, {"setmetatable", db_setmetatable},
{"setupvalue", db_setupvalue}, {"setupvalue", db_setupvalue},
{"traceback", db_traceback}, {"traceback", db_traceback},
{NULL, NULL} {"setcstacklimit", db_setcstacklimit},
{NULL, NULL}
}; };
LUAMOD_API int luaopen_debug(lua_State *L) { LUAMOD_API int luaopen_debug (lua_State *L) {
luaL_newlib(L, dblib); luaL_newlib(L, dblib);
return 1; return 1;
} }

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.h,v 2.7 2011/10/07 20:45:19 roberto Exp $ ** $Id: ldebug.h $
** 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,24 +11,54 @@
#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)
#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0)
#define resethookcount(L) (L->hookcount = L->basehookcount)
/* Active Lua function (given call info) */ /* Active Lua function (given call info) */
#define ci_func(ci) (clLvalue((ci)->func)) #define ci_func(ci) (clLvalue(s2v((ci)->func.p)))
LUAI_FUNC l_noret luaG_typeerror(lua_State *L, const TValue *o, #define resethookcount(L) (L->hookcount = L->basehookcount)
const char *opname);
LUAI_FUNC l_noret luaG_concaterror(lua_State *L, StkId p1, StkId p2); /*
LUAI_FUNC l_noret luaG_aritherror(lua_State *L, const TValue *p1, ** mark for entries in 'lineinfo' array that has absolute information in
const TValue *p2); ** 'abslineinfo' array
LUAI_FUNC l_noret luaG_ordererror(lua_State *L, const TValue *p1, */
const TValue *p2); #define ABSLINEINFO (-0x80)
LUAI_FUNC l_noret luaG_runerror(lua_State *L, const char *fmt, ...);
LUAI_FUNC l_noret luaG_errormsg(lua_State *L);
/*
** 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,
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,
const TValue *p2);
LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
const TValue *p2,
const char *msg);
LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
const TValue *p2);
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
const TValue *p2);
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
TString *src, int line);
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc);
LUAI_FUNC int luaG_tracecall (lua_State *L);
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 2.20 2011/11/29 15:55:08 roberto Exp $ ** $Id: ldo.h $
** 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,39 +8,80 @@
#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"
#define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ /*
luaD_growstack(L, n); else condmovestack(L); ** Macro to check stack size and grow stack if needed. Parameters
** 'pre'/'pos' allow the macro to preserve a pointer into the
** 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
** at every check.
*/
#define luaD_checkstackaux(L,n,pre,pos) \
if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \
{ pre; luaD_growstack(L, n, 1); pos; } \
else { condmovestack(L,pre,pos); }
/* In general, 'pre'/'pos' are empty (nothing to save) */
#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0)
#define incr_top(L) {L->top++; luaD_checkstack(L,0);}
#define savestack(L,p) ((char *)(p) - (char *)L->stack) #define savestack(L,pt) (cast_charp(pt) - cast_charp(L->stack.p))
#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) #define restorestack(L,n) cast(StkId, cast_charp(L->stack.p) + (n))
/* type of protected functions, to be ran by `runprotected' */ /* macro to check stack size, preserving 'p' */
typedef void (*Pfunc)(lua_State *L, void *ud); #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' */
LUAI_FUNC int luaD_protectedparser(lua_State *L, ZIO *z, const char *name,
const char *mode);
LUAI_FUNC void luaD_hook(lua_State *L, int event, int line);
LUAI_FUNC int luaD_precall(lua_State *L, StkId func, int nresults);
LUAI_FUNC void luaD_call(lua_State *L, StkId func, int nResults,
int allowyield);
LUAI_FUNC int luaD_pcall(lua_State *L, Pfunc func, void *u,
ptrdiff_t oldtop, ptrdiff_t ef);
LUAI_FUNC int luaD_poscall(lua_State *L, StkId firstResult);
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 l_noret luaD_throw(lua_State *L, int errcode); /* macro to check stack size and GC, preserving 'p' */
LUAI_FUNC int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud); #define checkstackGCp(L,n,p) \
luaD_checkstackaux(L, n, \
ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
luaC_checkGC(L), /* stack grow uses memory */ \
p = restorestack(L, t__)) /* 'pos' part: restore 'p' */
/* macro to check stack size and GC */
#define checkstackGC(L,fsize) \
luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0)
/* type of protected functions, to be ran by 'runprotected' */
typedef void (*Pfunc) (lua_State *L, void *ud);
LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
const char *mode);
LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
int fTransfer, int nTransfer);
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_callnoyield (lua_State *L, StkId func, int nResults);
LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status);
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
ptrdiff_t oldtop, ptrdiff_t ef);
LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror);
LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror);
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
LUAI_FUNC void luaD_inctop (lua_State *L);
LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
#endif #endif

View file

@ -1,154 +1,230 @@
/* /*
** $Id: ldump.c,v 2.17 2012/01/23 23:02:10 roberto Exp $ ** $Id: ldump.c $
** save precompiled Lua chunks ** save precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <stddef.h>
#define ldump_c #define ldump_c
#define LUA_CORE #define LUA_CORE
#include "lprefix.h"
#include <limits.h>
#include <stddef.h>
#include "lua.h" #include "lua.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h" #include "lstate.h"
#include "lundump.h" #include "lundump.h"
typedef struct { typedef struct {
lua_State *L; lua_State *L;
lua_Writer writer; lua_Writer writer;
void *data; void *data;
int strip; int strip;
int status; int status;
} DumpState; } DumpState;
#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
static void DumpBlock(const void *b, size_t size, DumpState *D) { /*
if (D->status == 0) { ** All high-level dumps go through dumpVector; you can change it to
lua_unlock(D->L); ** change the endianness of the result
D->status = (*D->writer)(D->L, b, size, D->data); */
lua_lock(D->L); #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
static void dumpBlock (DumpState *D, const void *b, size_t size) {
if (D->status == 0 && size > 0) {
lua_unlock(D->L);
D->status = (*D->writer)(D->L, b, size, D->data);
lua_lock(D->L);
}
}
#define dumpVar(D,x) dumpVector(D,&x,1)
static void dumpByte (DumpState *D, int y) {
lu_byte x = (lu_byte)y;
dumpVar(D, x);
}
/*
** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6"
** rounds up the division.)
*/
#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7)
static void dumpSize (DumpState *D, size_t x) {
lu_byte buff[DIBS];
int n = 0;
do {
buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */
x >>= 7;
} while (x != 0);
buff[DIBS - 1] |= 0x80; /* mark last byte */
dumpVector(D, buff + DIBS - n, n);
}
static void dumpInt (DumpState *D, int x) {
dumpSize(D, x);
}
static void dumpNumber (DumpState *D, lua_Number x) {
dumpVar(D, x);
}
static void dumpInteger (DumpState *D, lua_Integer x) {
dumpVar(D, x);
}
static void dumpString (DumpState *D, const TString *s) {
if (s == NULL)
dumpSize(D, 0);
else {
size_t size = tsslen(s);
const char *str = getstr(s);
dumpSize(D, size + 1);
dumpVector(D, str, size);
}
}
static void dumpCode (DumpState *D, const Proto *f) {
dumpInt(D, f->sizecode);
dumpVector(D, f->code, f->sizecode);
}
static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
static void dumpConstants (DumpState *D, const Proto *f) {
int i;
int n = f->sizek;
dumpInt(D, n);
for (i = 0; i < n; i++) {
const TValue *o = &f->k[i];
int tt = ttypetag(o);
dumpByte(D, tt);
switch (tt) {
case LUA_VNUMFLT:
dumpNumber(D, fltvalue(o));
break;
case LUA_VNUMINT:
dumpInteger(D, ivalue(o));
break;
case LUA_VSHRSTR:
case LUA_VLNGSTR:
dumpString(D, tsvalue(o));
break;
default:
lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
} }
}
} }
static void DumpChar(int y, DumpState *D) {
char x = (char)y; static void dumpProtos (DumpState *D, const Proto *f) {
DumpVar(x, D); int i;
int n = f->sizep;
dumpInt(D, n);
for (i = 0; i < n; i++)
dumpFunction(D, f->p[i], f->source);
} }
static void DumpInt(int x, DumpState *D) {
DumpVar(x, D); static void dumpUpvalues (DumpState *D, const Proto *f) {
int i, n = f->sizeupvalues;
dumpInt(D, n);
for (i = 0; i < n; i++) {
dumpByte(D, f->upvalues[i].instack);
dumpByte(D, f->upvalues[i].idx);
dumpByte(D, f->upvalues[i].kind);
}
} }
static void DumpNumber(lua_Number x, DumpState *D) {
DumpVar(x, D); static void dumpDebug (DumpState *D, const Proto *f) {
int i, n;
n = (D->strip) ? 0 : f->sizelineinfo;
dumpInt(D, n);
dumpVector(D, f->lineinfo, n);
n = (D->strip) ? 0 : f->sizeabslineinfo;
dumpInt(D, n);
for (i = 0; i < n; i++) {
dumpInt(D, f->abslineinfo[i].pc);
dumpInt(D, f->abslineinfo[i].line);
}
n = (D->strip) ? 0 : f->sizelocvars;
dumpInt(D, n);
for (i = 0; i < n; i++) {
dumpString(D, f->locvars[i].varname);
dumpInt(D, f->locvars[i].startpc);
dumpInt(D, f->locvars[i].endpc);
}
n = (D->strip) ? 0 : f->sizeupvalues;
dumpInt(D, n);
for (i = 0; i < n; i++)
dumpString(D, f->upvalues[i].name);
} }
static void DumpVector(const void *b, int n, size_t size, DumpState *D) {
DumpInt(n, D); static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
DumpMem(b, n, size, D); if (D->strip || f->source == psource)
dumpString(D, NULL); /* no debug info or same source as its parent */
else
dumpString(D, f->source);
dumpInt(D, f->linedefined);
dumpInt(D, f->lastlinedefined);
dumpByte(D, f->numparams);
dumpByte(D, f->is_vararg);
dumpByte(D, f->maxstacksize);
dumpCode(D, f);
dumpConstants(D, f);
dumpUpvalues(D, f);
dumpProtos(D, f);
dumpDebug(D, f);
} }
static void DumpString(const TString *s, DumpState *D) {
if (s == NULL) { static void dumpHeader (DumpState *D) {
size_t size = 0; dumpLiteral(D, LUA_SIGNATURE);
DumpVar(size, D); dumpByte(D, LUAC_VERSION);
} else { dumpByte(D, LUAC_FORMAT);
size_t size = s->tsv.len + 1; /* include trailing '\0' */ dumpLiteral(D, LUAC_DATA);
DumpVar(size, D); dumpByte(D, sizeof(Instruction));
DumpBlock(getstr(s), size * sizeof(char), D); dumpByte(D, sizeof(lua_Integer));
} dumpByte(D, sizeof(lua_Number));
dumpInteger(D, LUAC_INT);
dumpNumber(D, LUAC_NUM);
} }
#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
static void DumpFunction(const Proto *f, DumpState *D);
static void DumpConstants(const Proto *f, DumpState *D) {
int i, n = f->sizek;
DumpInt(n, D);
for (i = 0; i < n; i++) {
const TValue *o = &f->k[i];
DumpChar(ttypenv(o), D);
switch (ttypenv(o)) {
case LUA_TNIL:
break;
case LUA_TBOOLEAN:
DumpChar(bvalue(o), D);
break;
case LUA_TNUMBER:
DumpNumber(nvalue(o), D);
break;
case LUA_TSTRING:
DumpString(rawtsvalue(o), D);
break;
default:
lua_assert(0);
}
}
n = f->sizep;
DumpInt(n, D);
for (i = 0; i < n; i++) DumpFunction(f->p[i], D);
}
static void DumpUpvalues(const Proto *f, DumpState *D) {
int i, n = f->sizeupvalues;
DumpInt(n, D);
for (i = 0; i < n; i++) {
DumpChar(f->upvalues[i].instack, D);
DumpChar(f->upvalues[i].idx, D);
}
}
static void DumpDebug(const Proto *f, DumpState *D) {
int i, n;
DumpString((D->strip) ? NULL : f->source, D);
n = (D->strip) ? 0 : f->sizelineinfo;
DumpVector(f->lineinfo, n, sizeof(int), D);
n = (D->strip) ? 0 : f->sizelocvars;
DumpInt(n, D);
for (i = 0; i < n; i++) {
DumpString(f->locvars[i].varname, D);
DumpInt(f->locvars[i].startpc, D);
DumpInt(f->locvars[i].endpc, D);
}
n = (D->strip) ? 0 : f->sizeupvalues;
DumpInt(n, D);
for (i = 0; i < n; i++) DumpString(f->upvalues[i].name, D);
}
static void DumpFunction(const Proto *f, DumpState *D) {
DumpInt(f->linedefined, D);
DumpInt(f->lastlinedefined, D);
DumpChar(f->numparams, D);
DumpChar(f->is_vararg, D);
DumpChar(f->maxstacksize, D);
DumpCode(f, D);
DumpConstants(f, D);
DumpUpvalues(f, D);
DumpDebug(f, D);
}
static void DumpHeader(DumpState *D) {
lu_byte h[LUAC_HEADERSIZE];
luaU_header(h);
DumpBlock(h, LUAC_HEADERSIZE, D);
}
/* /*
** 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 strip) { int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
DumpState D; int strip) {
D.L = L; DumpState D;
D.writer = w; D.L = L;
D.data = data; D.writer = w;
D.strip = strip; D.data = data;
D.status = 0; D.strip = strip;
DumpHeader(&D); D.status = 0;
DumpFunction(f, &D); dumpHeader(&D);
return D.status; dumpByte(&D, f->sizeupvalues);
dumpFunction(&D, f, NULL);
return D.status;
} }

View file

@ -1,17 +1,21 @@
/* /*
** $Id: lfunc.c,v 2.30 2012/10/03 12:36:46 roberto Exp $ ** $Id: lfunc.c $
** 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
*/ */
#include <stddef.h>
#define lfunc_c #define lfunc_c
#define LUA_CORE #define LUA_CORE
#include "lprefix.h"
#include <stddef.h>
#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"
@ -20,142 +24,271 @@
Closure *luaF_newCclosure(lua_State *L, int n) { CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl; GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
c->c.nupvalues = cast_byte(n); CClosure *c = gco2ccl(o);
return c; c->nupvalues = cast_byte(nupvals);
return c;
} }
Closure *luaF_newLclosure(lua_State *L, int n) { LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
c->l.p = NULL; LClosure *c = gco2lcl(o);
c->l.nupvalues = cast_byte(n); c->p = NULL;
while (n--) c->l.upvals[n] = NULL; c->nupvalues = cast_byte(nupvals);
return c; while (nupvals--) c->upvals[nupvals] = NULL;
} return c;
UpVal *luaF_newupval(lua_State *L) {
UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv;
uv->v = &uv->u.value;
setnilvalue(uv->v);
return uv;
}
UpVal *luaF_findupval(lua_State *L, StkId level) {
global_State *g = G(L);
GCObject **pp = &L->openupval;
UpVal *p;
UpVal *uv;
while (*pp != NULL && (p = gco2uv(*pp))->v >= level) {
GCObject *o = obj2gco(p);
lua_assert(p->v != &p->u.value);
lua_assert(!isold(o) || isold(obj2gco(L)));
if (p->v == level) { /* found a corresponding upvalue? */
if (isdead(g, o)) /* is it dead? */
changewhite(o); /* resurrect it */
return p;
}
pp = &p->next;
}
/* not found: create a new one */
uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv;
uv->v = level; /* current value lives in the stack */
uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */
uv->u.l.next = g->uvhead.u.l.next;
uv->u.l.next->u.l.prev = uv;
g->uvhead.u.l.next = uv;
lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
return uv;
}
static void unlinkupval(UpVal *uv) {
lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */
uv->u.l.prev->u.l.next = uv->u.l.next;
}
void luaF_freeupval(lua_State *L, UpVal *uv) {
if (uv->v != &uv->u.value) /* is it open? */
unlinkupval(uv); /* remove from open list */
luaM_free(L, uv); /* free upvalue */
}
void luaF_close(lua_State *L, StkId level) {
UpVal *uv;
global_State *g = G(L);
while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) {
GCObject *o = obj2gco(uv);
lua_assert(!isblack(o) && uv->v != &uv->u.value);
L->openupval = uv->next; /* remove from `open' list */
if (isdead(g, o))
luaF_freeupval(L, uv); /* free upvalue */
else {
unlinkupval(uv); /* remove upvalue from 'uvhead' list */
setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */
uv->v = &uv->u.value; /* now current value lives here */
gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */
g->allgc = o;
luaC_checkupvalcolor(g, uv);
}
}
}
Proto *luaF_newproto(lua_State *L) {
Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p;
f->k = NULL;
f->sizek = 0;
f->p = NULL;
f->sizep = 0;
f->code = NULL;
f->cache = NULL;
f->sizecode = 0;
f->lineinfo = NULL;
f->sizelineinfo = 0;
f->upvalues = NULL;
f->sizeupvalues = 0;
f->numparams = 0;
f->is_vararg = 0;
f->maxstacksize = 0;
f->locvars = NULL;
f->sizelocvars = 0;
f->linedefined = 0;
f->lastlinedefined = 0;
f->source = NULL;
return f;
}
void luaF_freeproto(lua_State *L, Proto *f) {
luaM_freearray(L, f->code, f->sizecode);
luaM_freearray(L, f->p, f->sizep);
luaM_freearray(L, f->k, f->sizek);
luaM_freearray(L, f->lineinfo, f->sizelineinfo);
luaM_freearray(L, f->locvars, f->sizelocvars);
luaM_freearray(L, f->upvalues, f->sizeupvalues);
luaM_free(L, f);
} }
/* /*
** Look for n-th local variable at line `line' in function `func'. ** fill a closure with new closed upvalues
** Returns NULL if not found.
*/ */
const char *luaF_getlocalname(const Proto *f, int local_number, int pc) { void luaF_initupvals (lua_State *L, LClosure *cl) {
int i; int i;
for (i = 0; i < f->sizelocvars && f->locvars[i].startpc <= pc; i++) { for (i = 0; i < cl->nupvalues; i++) {
if (pc < f->locvars[i].endpc) { /* is variable active? */ GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
local_number--; UpVal *uv = gco2upv(o);
if (local_number == 0) uv->v.p = &uv->u.value; /* make it closed */
return getstr(f->locvars[i].varname); setnilvalue(uv->v.p);
} cl->upvals[i] = uv;
} luaC_objbarrier(L, cl, uv);
return NULL; /* not found */ }
}
/*
** Create a new upvalue at the given level, and link it to the list of
** open upvalues of 'L' after entry 'prev'.
**/
static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
UpVal *uv = gco2upv(o);
UpVal *next = *prev;
uv->v.p = s2v(level); /* current value lives in the stack */
uv->u.open.next = next; /* link it to list of open upvalues */
uv->u.open.previous = prev;
if (next)
next->u.open.previous = &uv->u.open.next;
*prev = uv;
if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
L->twups = G(L)->twups; /* link it to the list */
G(L)->twups = L;
}
return uv;
}
/*
** 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 message '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;
const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
setobj2s(L, top, tm); /* will call metamethod... */
setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
L->top.p = top + 3; /* add function and arguments */
if (yy)
luaD_call(L, top, 0);
else
luaD_callnoyield(L, top, 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, int status, int yy) {
TValue *uv = s2v(level); /* value being closed */
TValue *errobj;
if (status == CLOSEKTOP)
errobj = &G(L)->nilvalue; /* error object is nil */
else { /* '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 */
}
callclosemethod(L, uv, errobj, yy);
}
/*
** Maximum value for deltas in 'tbclist', dependent on the type
** of delta. (This macro assumes that an 'L' is in scope where it
** is used.)
*/
#define MAXDELTA \
((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1)
/*
** 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;
StkId upl; /* stack index pointed by 'uv' */
while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
TValue *slot = &uv->u.value; /* new position for value */
lua_assert(uplevel(uv) < L->top.p);
luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
setobj(L, slot, uv->v.p); /* move value to upvalue slot */
uv->v.p = slot; /* now current value lives here */
if (!iswhite(uv)) { /* neither white nor dead? */
nw2black(uv); /* closed upvalues cannot be gray */
luaC_barrier(L, uv, slot);
}
}
}
/*
** 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, int 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) {
GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
Proto *f = gco2p(o);
f->k = NULL;
f->sizek = 0;
f->p = NULL;
f->sizep = 0;
f->code = NULL;
f->sizecode = 0;
f->lineinfo = NULL;
f->sizelineinfo = 0;
f->abslineinfo = NULL;
f->sizeabslineinfo = 0;
f->upvalues = NULL;
f->sizeupvalues = 0;
f->numparams = 0;
f->is_vararg = 0;
f->maxstacksize = 0;
f->locvars = NULL;
f->sizelocvars = 0;
f->linedefined = 0;
f->lastlinedefined = 0;
f->source = NULL;
return f;
}
void luaF_freeproto (lua_State *L, Proto *f) {
luaM_freearray(L, f->code, f->sizecode);
luaM_freearray(L, f->p, f->sizep);
luaM_freearray(L, f->k, f->sizek);
luaM_freearray(L, f->lineinfo, f->sizelineinfo);
luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
luaM_freearray(L, f->locvars, f->sizelocvars);
luaM_freearray(L, f->upvalues, f->sizeupvalues);
luaM_free(L, f);
}
/*
** Look for n-th local variable at line 'line' in function 'func'.
** Returns NULL if not found.
*/
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
int i;
for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
if (pc < f->locvars[i].endpc) { /* is variable active? */
local_number--;
if (local_number == 0)
return getstr(f->locvars[i].varname);
}
}
return NULL; /* not found */
} }

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.h,v 2.8 2012/05/08 13:53:33 roberto Exp $ ** $Id: lfunc.h $
** 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,23 +11,54 @@
#include "lobject.h" #include "lobject.h"
#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ #define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \
cast(int, sizeof(TValue)*((n)-1))) cast_int(sizeof(TValue)) * (n))
#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ #define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \
cast(int, sizeof(TValue *)*((n)-1))) cast_int(sizeof(TValue *)) * (n))
LUAI_FUNC Proto *luaF_newproto(lua_State *L); /* test whether thread is in 'twups' list */
LUAI_FUNC Closure *luaF_newCclosure(lua_State *L, int nelems); #define isintwups(L) (L->twups != L)
LUAI_FUNC Closure *luaF_newLclosure(lua_State *L, int nelems);
LUAI_FUNC UpVal *luaF_newupval(lua_State *L);
LUAI_FUNC UpVal *luaF_findupval(lua_State *L, StkId level); /*
LUAI_FUNC void luaF_close(lua_State *L, StkId level); ** maximum number of upvalues in a closure (both C and Lua). (Value
LUAI_FUNC void luaF_freeproto(lua_State *L, Proto *f); ** must fit in a VM register.)
LUAI_FUNC void luaF_freeupval(lua_State *L, UpVal *uv); */
LUAI_FUNC const char *luaF_getlocalname(const Proto *func, int local_number, #define MAXUPVAL 255
int pc);
#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
** in prototypes
*/
#define MAXMISS 10
/* special status to close upvalues preserving the top of the stack */
#define CLOSEKTOP (-1)
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals);
LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals);
LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
LUAI_FUNC void luaF_newtbcupval (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, int status, int yy);
LUAI_FUNC void luaF_unlinkupval (UpVal *uv);
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
int pc);
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.h,v 2.58 2012/09/11 12:53:08 roberto Exp $ ** $Id: lgc.h $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -12,146 +12,191 @@
#include "lstate.h" #include "lstate.h"
/* /*
** Collectable objects may have one of three colors: white, which ** Collectable objects may have one of three colors: white, which means
** means the object is not marked; gray, which means the ** the object is not marked; gray, which means the object is marked, but
** object is marked, but its references may be not marked; and ** its references may be not marked; and black, which means that the
** black, which means that the object and all its references are marked. ** object and all its references are marked. The main invariant of the
** The main invariant of the garbage collector, while marking objects, ** garbage collector, while marking objects, is that a black object can
** is that a black object can never point to a white one. Moreover, ** never point to a white one. Moreover, any gray object must be in a
** any gray object must be in a "gray list" (gray, grayagain, weak, ** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
** allweak, ephemeron) so that it can be visited again before finishing ** can be visited again before finishing the collection cycle. (Open
** the collection cycle. These lists have no meaning when the invariant ** upvalues are an exception to this rule.) These lists have no meaning
** is not being enforced (e.g., sweep phase). ** when the 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 GCSatomic 1 #define GCSenteratomic 1
#define GCSsweepstring 2 #define GCSatomic 2
#define GCSsweepudata 3 #define GCSswpallgc 3
#define GCSsweep 4 #define GCSswpfinobj 4
#define GCSpause 5 #define GCSswptobefnz 5
#define GCSswpend 6
#define GCScallfin 7
#define GCSpause 8
#define issweepphase(g) \ #define issweepphase(g) \
(GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
#define isgenerational(g) ((g)->gckind == KGC_GEN)
/* /*
** macros 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 non-generational collection, the sweep ** ones) must be kept. During a collection, the sweep
** phase may break the invariant, as objects turned white may point to ** phase may break the invariant, as objects turned white may point to
** still-black objects. The invariant is restored when sweep ends and ** still-black objects. The invariant is restored when sweep ends and
** all objects are white again. During a generational collection, the ** all objects are white again.
** invariant must be kept all times.
*/ */
#define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) #define keepinvariant(g) ((g)->gcstate <= GCSatomic)
/*
** Outside the collector, the state in generational mode is kept in
** 'propagate', so 'keepinvariant' is always true.
*/
#define keepinvariantout(g) \
check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \
g->gcstate <= GCSatomic)
/* /*
** some useful bit tricks ** some useful bit tricks
*/ */
#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) #define resetbits(x,m) ((x) &= cast_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))
#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
#define l_setbit(x,b) setbits(x, bitmask(b)) #define l_setbit(x,b) setbits(x, bitmask(b))
#define resetbit(x,b) resetbits(x, bitmask(b)) #define resetbit(x,b) resetbits(x, bitmask(b))
#define testbit(x,b) testbits(x, bitmask(b)) #define testbit(x,b) testbits(x, bitmask(b))
/* Layout for bit use in `marked' field: */ /*
#define WHITE0BIT 0 /* object is white (type 0) */ ** Layout for bit use in 'marked' field. First three bits are
#define WHITE1BIT 1 /* object is white (type 1) */ ** used for object "age" in generational mode. Last bit is used
#define BLACKBIT 2 /* object is black */ ** by tests.
#define FINALIZEDBIT 3 /* object has been separated for finalization */ */
#define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ #define WHITE0BIT 3 /* object is white (type 0) */
#define FIXEDBIT 5 /* object is fixed (should not be collected) */ #define WHITE1BIT 4 /* object is white (type 1) */
#define OLDBIT 6 /* object is old (only in generational mode) */ #define BLACKBIT 5 /* object is black */
/* bit 7 is currently used by tests (luaL_checkmemory) */ #define FINALIZEDBIT 6 /* object has been marked for finalization */
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) #define TESTBIT 7
#define iswhite(x) testbits((x)->gch.marked, WHITEBITS)
#define isblack(x) testbit((x)->gch.marked, BLACKBIT) #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
#define iswhite(x) testbits((x)->marked, WHITEBITS)
#define isblack(x) testbit((x)->marked, BLACKBIT)
#define isgray(x) /* neither white nor black */ \ #define isgray(x) /* neither white nor black */ \
(!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
#define isold(x) testbit((x)->gch.marked, OLDBIT) #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
/* MOVE OLD rule: whenever an object is moved to the beginning of #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
a GC list, its old bit must be cleared */ #define isdeadm(ow,m) ((m) & (ow))
#define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
#define otherwhite(g) (g->currentwhite ^ WHITEBITS) #define changewhite(x) ((x)->marked ^= WHITEBITS)
#define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) #define nw2black(x) \
#define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
#define changewhite(x) ((x)->gch.marked ^= WHITEBITS) #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
#define luaC_condGC(L,c) \ /* object age in generational mode */
{if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} #define G_NEW 0 /* created in current cycle */
#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) #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)
#define changeage(o,f,t) \
check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ /* Default Values for GC parameters */
luaC_barrier_(L,obj2gco(p),gcvalue(v)); } #define LUAI_GENMAJORMUL 100
#define LUAI_GENMINORMUL 20
#define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ /* wait memory to double before starting new cycle */
luaC_barrierback_(L,p); } #define LUAI_GCPAUSE 200
#define luaC_objbarrier(L,p,o) \ /*
{ if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ ** some gc parameters are stored divided by 4 to allow a maximum value
luaC_barrier_(L,obj2gco(p),obj2gco(o)); } ** up to 1023 in a 'lu_byte'.
*/
#define getgcparam(p) ((p) * 4)
#define setgcparam(p,v) ((p) = (v) / 4)
#define luaC_objbarrierback(L,p,o) \ #define LUAI_GCMUL 100
{ if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); }
#define luaC_barrierproto(L,p,c) \ /* how much to allocate before next GC step (log2) */
{ if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } #define LUAI_GCSTEPSIZE 13 /* 8 KB */
/*
** Check whether the declared GC mode is generational. While in
** generational mode, the collector can go temporarily to incremental
** mode to improve performance. This is signaled by 'g->lastatomic != 0'.
*/
#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
/*
** 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 positive. 'pre'/'pos'
** allows some adjustments to be done only when needed. macro
** 'condchangemem' is used only for heavy tests (forcing a full
** GC cycle on every opportunity)
*/
#define luaC_condGC(L,pre,pos) \
{ if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
condchangemem(L,pre,pos); }
/* more often than not, 'pre'/'pos' are empty */
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
#define luaC_objbarrier(L,p,o) ( \
(isblack(p) && iswhite(o)) ? \
luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
#define luaC_barrier(L,p,v) ( \
iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : 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_freeallobjects (lua_State *L);
LUAI_FUNC void luaC_step (lua_State *L);
LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
size_t offset);
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_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
LUAI_FUNC void luaC_freeallobjects(lua_State *L);
LUAI_FUNC void luaC_step(lua_State *L);
LUAI_FUNC void luaC_forcestep(lua_State *L);
LUAI_FUNC void luaC_runtilstate(lua_State *L, int statesmask);
LUAI_FUNC void luaC_fullgc(lua_State *L, int isemergency);
LUAI_FUNC GCObject *luaC_newobj(lua_State *L, int tt, size_t sz,
GCObject **list, int offset);
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_barrierproto_(lua_State *L, Proto *p, Closure *c);
LUAI_FUNC void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt);
LUAI_FUNC void luaC_checkupvalcolor(global_State *g, UpVal *uv);
LUAI_FUNC void luaC_changemode(lua_State *L, int mode);
#endif #endif

View file

@ -1,20 +1,33 @@
/* /*
** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $ ** $Id: linit.c $
** 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
*/ */
#define linit_c
#define LUA_LIB
/* /*
** If you embed Lua in your program and need to open the standard ** If you embed Lua in your program and need to open the standard
** libraries, call luaL_openlibs in your program. If you need a ** libraries, call luaL_openlibs in your program. If you need a
** different set of libraries, copy this file to your project and edit ** different set of libraries, copy this file to your project and edit
** it to suit your needs. ** 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"
#define linit_c
#define LUA_LIB #include <stddef.h>
#include "lua.h" #include "lua.h"
@ -27,41 +40,26 @@
** program ** program
*/ */
static const luaL_Reg loadedlibs[] = { static const luaL_Reg loadedlibs[] = {
{"_G", luaopen_base}, {LUA_GNAME, luaopen_base},
{LUA_LOADLIBNAME, luaopen_package}, {LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine}, {LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table}, {LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io}, {LUA_IOLIBNAME, luaopen_io},
{LUA_OSLIBNAME, luaopen_os}, {LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string}, {LUA_STRLIBNAME, luaopen_string},
{LUA_BITLIBNAME, luaopen_bit32}, {LUA_MATHLIBNAME, luaopen_math},
{LUA_MATHLIBNAME, luaopen_math}, {LUA_UTF8LIBNAME, luaopen_utf8},
{LUA_DBLIBNAME, luaopen_debug}, {LUA_DBLIBNAME, luaopen_debug},
{NULL, NULL} {NULL, NULL}
}; };
/* LUALIB_API void luaL_openlibs (lua_State *L) {
** these libs are preloaded and must be required before used const luaL_Reg *lib;
*/ /* "require" functions from 'loadedlibs' and set results to global table */
static const luaL_Reg preloadedlibs[] = { for (lib = loadedlibs; lib->func; lib++) {
{NULL, NULL} luaL_requiref(L, lib->name, lib->func, 1);
}; lua_pop(L, 1); /* remove lib */
}
LUALIB_API void luaL_openlibs(lua_State *L) {
const luaL_Reg *lib;
/* call open functions from 'loadedlibs' and set results to global table */
for (lib = loadedlibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* remove lib */
}
/* add open functions from 'preloadedlibs' into 'package.preload' table */
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
for (lib = preloadedlibs; lib->func; lib++) {
lua_pushcfunction(L, lib->func);
lua_setfield(L, -2, lib->name);
}
lua_pop(L, 1); /* remove _PRELOAD table */
} }

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,112 @@
/*
** $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_SHRI,
&&L_OP_SHLI,
&&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_VARARGPREP,
&&L_OP_EXTRAARG
};

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: llex.h,v 1.72 2011/11/30 12:43:51 roberto Exp $ ** $Id: llex.h $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -7,72 +7,85 @@
#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)
#define LUA_ENV "_ENV"
#endif
/* /*
* WARNING: if you change the order of this enumeration, * WARNING: if you change the order of this enumeration,
* grep "ORDER RESERVED" * grep "ORDER RESERVED"
*/ */
enum RESERVED { 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_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, 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_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
TK_NUMBER, TK_NAME, TK_STRING TK_SHL, TK_SHR,
TK_DBCOLON, TK_EOS,
TK_FLT, TK_INT, TK_NAME, TK_STRING
}; };
/* 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 {
lua_Number r; lua_Number r;
TString *ts; lua_Integer i;
TString *ts;
} SemInfo; /* semantics information */ } SemInfo; /* semantics information */
typedef struct Token { typedef struct Token {
int token; int token;
SemInfo seminfo; SemInfo seminfo;
} Token; } Token;
/* state of the lexer 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) */
int linenumber; /* input line counter */ int linenumber; /* input line counter */
int lastline; /* line of last token `consumed' */ int lastline; /* line of last token 'consumed' */
Token t; /* current token */ Token t; /* current token */
Token lookahead; /* look ahead token */ Token lookahead; /* look ahead token */
struct FuncState *fs; /* current function (parser) */ struct FuncState *fs; /* current function (parser) */
struct lua_State *L; struct lua_State *L;
ZIO *z; /* input stream */ ZIO *z; /* input stream */
Mbuffer *buff; /* buffer for tokens */ Mbuffer *buff; /* buffer for tokens */
struct Dyndata *dyd; /* dynamic structures used by the parser */ Table *h; /* to avoid collection/reuse strings */
TString *source; /* current source name */ struct Dyndata *dyd; /* dynamic structures used by the parser */
TString *envn; /* environment variable name */ TString *source; /* current source name */
char decpoint; /* locale decimal point */ TString *envn; /* environment variable name */
} LexState; } LexState;
LUAI_FUNC void luaX_init(lua_State *L); LUAI_FUNC void luaX_init (lua_State *L);
LUAI_FUNC void luaX_setinput(lua_State *L, LexState *ls, ZIO *z, LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
TString *source, int firstchar); TString *source, int firstchar);
LUAI_FUNC TString *luaX_newstring(LexState *ls, const char *str, size_t l); LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
LUAI_FUNC void luaX_next(LexState *ls); LUAI_FUNC void luaX_next (LexState *ls);
LUAI_FUNC int luaX_lookahead(LexState *ls); LUAI_FUNC int luaX_lookahead (LexState *ls);
LUAI_FUNC l_noret luaX_syntaxerror(LexState *ls, const char *s); LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s);
LUAI_FUNC const char *luaX_token2str(LexState *ls, int token); LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);
#endif #endif

View file

@ -1,6 +1,6 @@
/* /*
** $Id: llimits.h,v 1.103 2013/02/20 14:08:56 roberto Exp $ ** $Id: llimits.h $
** 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
*/ */
@ -15,295 +15,366 @@
#include "lua.h" #include "lua.h"
typedef unsigned LUA_INT32 lu_int32; /*
** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
** the total memory used by Lua (in bytes). Usually, 'size_t' and
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
*/
#if defined(LUAI_MEM) /* { external definitions? */
typedef LUAI_UMEM lu_mem; typedef LUAI_UMEM lu_mem;
typedef LUAI_MEM l_mem; typedef LUAI_MEM l_mem;
#elif LUAI_IS32INT /* }{ */
typedef size_t lu_mem;
typedef ptrdiff_t l_mem;
#else /* 16-bit ints */ /* }{ */
typedef unsigned long lu_mem;
typedef long l_mem;
#endif /* } */
/* 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;
#define MAX_SIZET ((size_t)(~(size_t)0)-2) /* maximum value for size_t */
#define MAX_SIZET ((size_t)(~(size_t)0))
#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) /* maximum size visible for Lua (must be representable in a lua_Integer) */
#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
#define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) : (size_t)(LUA_MAXINTEGER))
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ #define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
#define MAX_INT INT_MAX /* maximum value of an int */
/* /*
** conversion of pointer to integer ** floor of the log2 of the maximum signed value for integral type 't'.
** this is for hashing only; there is no problem if the integer ** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
** cannot hold the whole pointer value
*/ */
#define IntPoint(p) ((unsigned int)(lu_mem)(p)) #define log2maxs(t) (sizeof(t) * 8 - 2)
/*
** test whether an unsigned value is a power of 2 (or zero)
*/
#define ispow2(x) (((x) & ((x) - 1)) == 0)
/* type to ensure maximum alignment */
#if !defined(LUAI_USER_ALIGNMENT_T) /* number of chars of a literal string without the ending \0 */
#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } #define LL(x) (sizeof(x)/sizeof(char) - 1)
/*
** conversion of pointer to unsigned integer: this is for hashing only;
** there is no problem if the integer cannot hold the whole pointer
** value. (In strict ISO C this may cause undefined behavior, but no
** actual machine seems to bother.)
*/
#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
__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 #endif
typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; #define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX))
/* result of a `usual argument conversion' over lua_Number */
/* types of 'usual argument conversions' for lua_Number and lua_Integer */
typedef LUAI_UACNUMBER l_uacNumber; typedef LUAI_UACNUMBER l_uacNumber;
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)
#endif
#if defined(lua_assert) #if defined(lua_assert)
#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) { if (!(c)) lua_assert(0); } #define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
#else #else
#define lua_assert(c) ((void)0) #define lua_assert(c) ((void)0)
#define check_exp(c,e) (e) #define check_exp(c,e) (e)
#define lua_longassert(c) ((void)0) #define lua_longassert(c) ((void)0)
#endif #endif
/* /*
** assertion for checking API calls ** assertion for checking API calls
*/ */
#if !defined(luai_apicheck) #if !defined(luai_apicheck)
#define luai_apicheck(l,e) ((void)l, lua_assert(e))
#if defined(LUA_USE_APICHECK)
#include <assert.h>
#define luai_apicheck(L,e) assert(e)
#else
#define luai_apicheck(L,e) lua_assert(e)
#endif #endif
#endif #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
/* macro to avoid warnings about unused variables */
#if !defined(UNUSED) #if !defined(UNUSED)
#define UNUSED(x) ((void)(x)) /* to avoid warnings */ #define UNUSED(x) ((void)(x))
#endif #endif
#define cast(t, exp) ((t)(exp)) /* type casts (a macro highlights casts in the code) */
#define cast(t, exp) ((t)(exp))
#define cast_byte(i) cast(lu_byte, (i)) #define cast_void(i) cast(void, (i))
#define cast_num(i) cast(lua_Number, (i)) #define cast_voidp(i) cast(void *, (i))
#define cast_int(i) cast(int, (i)) #define cast_num(i) cast(lua_Number, (i))
#define cast_uchar(i) cast(unsigned char, (i)) #define cast_int(i) cast(int, (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_char(i) cast(char, (i))
#define cast_charp(i) cast(char *, (i))
#define cast_sizet(i) cast(size_t, (i))
/* cast a signed lua_Integer to lua_Unsigned */
#if !defined(l_castS2U)
#define l_castS2U(i) ((lua_Unsigned)(i))
#endif
/*
** cast a lua_Unsigned to a signed lua_Integer; this cast is
** not strict ISO C, but two-complement architectures should
** work fine.
*/
#if !defined(l_castU2S)
#define l_castU2S(i) ((lua_Integer)(i))
#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) #elif defined(_MSC_VER) && _MSC_VER >= 1200
#define l_noret void __declspec(noreturn) #define l_noret void __declspec(noreturn)
#else #else
#define l_noret void #define l_noret void
#endif
#endif #endif
/* /*
** maximum depth for nested C calls and syntactical nested non-terminals ** Inline functions
** in a program. (Value must fit in an unsigned short int.)
*/ */
#if !defined(LUAI_MAXCCALLS) #if !defined(LUA_USE_C89)
#define LUAI_MAXCCALLS 200 #define l_inline inline
#elif defined(__GNUC__)
#define l_inline __inline__
#else
#define l_inline /* empty */
#endif #endif
/* #define l_sinline static l_inline
** maximum number of upvalues in a closure (both C and Lua). (Value
** must fit in an unsigned char.)
*/
#define MAXUPVAL UCHAR_MAX
/* /*
** type for virtual-machine instructions ** type for virtual-machine instructions;
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
*/ */
typedef lu_int32 Instruction; #if LUAI_IS32INT
typedef unsigned int l_uint32;
#else
typedef unsigned long l_uint32;
#endif
typedef l_uint32 Instruction;
/* maximum stack for a Lua function */ /*
#define MAXSTACK 250 ** 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
/*
/* minimum size for the string table (must be power of 2) */ ** 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(MINSTRTABSIZE) #if !defined(MINSTRTABSIZE)
#define MINSTRTABSIZE 32 #define MINSTRTABSIZE 128
#endif
/*
** 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 #endif
/* minimum size for string buffer */ /* minimum size for string buffer */
#if !defined(LUA_MINBUFFER) #if !defined(LUA_MINBUFFER)
#define LUA_MINBUFFER 32 #define LUA_MINBUFFER 32
#endif
#if !defined(lua_lock)
#define lua_lock(L) ((void) 0)
#define lua_unlock(L) ((void) 0)
#endif
#if !defined(luai_threadyield)
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
#endif #endif
/* /*
** these macros allow user-specific actions on threads when you defined ** Maximum depth for nested C calls, syntactical nested non-terminals,
** LUAI_EXTRASPACE and need to do something extra when a thread is ** 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
/*
** 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 when a thread is
** created/deleted/resumed/yielded. ** created/deleted/resumed/yielded.
*/ */
#if !defined(luai_userstateopen) #if !defined(luai_userstateopen)
#define luai_userstateopen(L) ((void)L) #define luai_userstateopen(L) ((void)L)
#endif #endif
#if !defined(luai_userstateclose) #if !defined(luai_userstateclose)
#define luai_userstateclose(L) ((void)L) #define luai_userstateclose(L) ((void)L)
#endif #endif
#if !defined(luai_userstatethread) #if !defined(luai_userstatethread)
#define luai_userstatethread(L,L1) ((void)L) #define luai_userstatethread(L,L1) ((void)L)
#endif #endif
#if !defined(luai_userstatefree) #if !defined(luai_userstatefree)
#define luai_userstatefree(L,L1) ((void)L) #define luai_userstatefree(L,L1) ((void)L)
#endif #endif
#if !defined(luai_userstateresume) #if !defined(luai_userstateresume)
#define luai_userstateresume(L,n) ((void)L) #define luai_userstateresume(L,n) ((void)L)
#endif #endif
#if !defined(luai_userstateyield) #if !defined(luai_userstateyield)
#define luai_userstateyield(L,n) ((void)L) #define luai_userstateyield(L,n) ((void)L)
#endif
/*
** The luai_num* macros define the primitive operations over numbers.
*/
/* floor division (defined as 'floor(a/b)') */
#if !defined(luai_numidiv)
#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
#endif
/* float division */
#if !defined(luai_numdiv)
#define luai_numdiv(L,a,b) ((a)/(b))
#endif #endif
/* /*
** lua_number2int is a macro to convert lua_Number to int. ** modulo: defined as 'a - floor(a/b)*b'; the direct computation
** lua_number2integer is a macro to convert lua_Number to lua_Integer. ** using this definition has several problems with rounding errors,
** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. ** so it is better to use 'fmod'. 'fmod' gives the result of
** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. ** 'a - trunc(a/b)*b', and therefore must be corrected when
** luai_hashnum is a macro to hash a lua_Number value into an integer. ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
** The hash must be deterministic and give reasonable values for ** non-integer negative result: non-integer result is equivalent to
** both small and large values (outside the range of integers). ** 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(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */ #define luai_nummod(L,a,b,m) \
/* trick with Microsoft assembler for X86 */ { (void)L; (m) = l_mathop(fmod)(a,b); \
if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
#define lua_number2int(i,n) __asm {__asm fld n __asm fistp i}
#define lua_number2integer(i,n) lua_number2int(i, n)
#define lua_number2unsigned(i,n) \
{__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;}
#elif defined(LUA_IEEE754TRICK) /* }{ */
/* the next trick should work on any machine using IEEE754 with
a 32-bit int type */
union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
#if !defined(LUA_IEEEENDIAN) /* { */
#define LUAI_EXTRAIEEE \
static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)};
#define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33)
#else
#define LUA_IEEEENDIANLOC LUA_IEEEENDIAN
#define LUAI_EXTRAIEEE /* empty */
#endif /* } */
#define lua_number2int32(i,n,t) \
{ LUAI_EXTRAIEEE \
volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
(i) = (t)u.l_p[LUA_IEEEENDIANLOC]; }
#define luai_hashnum(i,n) \
{ volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \
(i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */
#define lua_number2int(i,n) lua_number2int32(i, n, int)
#define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
/* the trick can be expanded to lua_Integer when it is a 32-bit value */
#if defined(LUA_IEEELL)
#define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer)
#endif #endif
#endif /* } */ /* exponentiation */
#if !defined(luai_numpow)
#define luai_numpow(L,a,b) \
/* the following definitions always work, but may be slow */ ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
#if !defined(lua_number2int)
#define lua_number2int(i,n) ((i)=(int)(n))
#endif #endif
#if !defined(lua_number2integer) /* the others are quite standard operations */
#define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) #if !defined(luai_numadd)
#endif #define luai_numadd(L,a,b) ((a)+(b))
#define luai_numsub(L,a,b) ((a)-(b))
#if !defined(lua_number2unsigned) /* { */ #define luai_nummul(L,a,b) ((a)*(b))
/* the following definition assures proper modulo behavior */ #define luai_numunm(L,a) (-(a))
#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT) #define luai_numeq(a,b) ((a)==(b))
#include <math.h> #define luai_numlt(a,b) ((a)<(b))
#define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) #define luai_numle(a,b) ((a)<=(b))
#define lua_number2unsigned(i,n) \ #define luai_numgt(a,b) ((a)>(b))
((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) #define luai_numge(a,b) ((a)>=(b))
#else #define luai_numisnan(a) (!luai_numeq((a), (a)))
#define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
#endif
#endif /* } */
#if !defined(lua_unsigned2number)
/* on several machines, coercion from unsigned to double is slow,
so it may be worth to avoid */
#define lua_unsigned2number(u) \
(((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
#endif #endif
#if defined(ltable_c) && !defined(luai_hashnum)
#include <float.h>
#include <math.h>
#define luai_hashnum(i,n) { int e; \
n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
lua_number2int(i, n); i += e; }
#endif
/* /*
** macro to control inclusion of some hard tests on stack reallocation ** macro to control inclusion of some hard tests on stack reallocation
*/ */
#if !defined(HARDSTACKTESTS) #if !defined(HARDSTACKTESTS)
#define condmovestack(L) ((void)0) #define condmovestack(L,pre,pos) ((void)0)
#else #else
/* realloc stack keeping its size */ /* realloc stack keeping its size */
#define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) #define condmovestack(L,pre,pos) \
{ int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
#endif #endif
#if !defined(HARDMEMTESTS) #if !defined(HARDMEMTESTS)
#define condchangemem(L) condmovestack(L) #define condchangemem(L,pre,pos) ((void)0)
#else #else
#define condchangemem(L) \ #define condchangemem(L,pre,pos) \
((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
#endif #endif
#endif #endif

View file

@ -1,16 +1,21 @@
/* /*
** $Id: lmathlib.c,v 1.83 2013/03/07 18:21:32 roberto Exp $ ** $Id: lmathlib.c $
** Standard mathematical library ** Standard mathematical library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <stdlib.h>
#include <math.h>
#define lmathlib_c #define lmathlib_c
#define LUA_LIB #define LUA_LIB
#include "lprefix.h"
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
@ -18,263 +23,759 @@
#undef PI #undef PI
#define PI ((lua_Number)(3.1415926535897932384626433832795)) #define PI (l_mathop(3.141592653589793238462643383279502884))
#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))
static int math_abs (lua_State *L) {
static int math_abs(lua_State *L) { if (lua_isinteger(L, 1)) {
lua_Integer n = lua_tointeger(L, 1);
if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
lua_pushinteger(L, n);
}
else
lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_sin(lua_State *L) { static int math_sin (lua_State *L) {
lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_sinh(lua_State *L) { static int math_cos (lua_State *L) {
lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_cos(lua_State *L) { static int math_tan (lua_State *L) {
lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_cosh(lua_State *L) { static int math_asin (lua_State *L) {
lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_tan(lua_State *L) { static int math_acos (lua_State *L) {
lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_tanh(lua_State *L) { static int math_atan (lua_State *L) {
lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); lua_Number y = luaL_checknumber(L, 1);
return 1; lua_Number x = luaL_optnumber(L, 2, 1);
lua_pushnumber(L, l_mathop(atan2)(y, x));
return 1;
} }
static int math_asin(lua_State *L) {
lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); static int math_toint (lua_State *L) {
return 1; int valid;
lua_Integer n = lua_tointegerx(L, 1, &valid);
if (l_likely(valid))
lua_pushinteger(L, n);
else {
luaL_checkany(L, 1);
luaL_pushfail(L); /* value is not convertible to integer */
}
return 1;
} }
static int math_acos(lua_State *L) {
lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); static void pushnumint (lua_State *L, lua_Number d) {
return 1; lua_Integer n;
if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
lua_pushinteger(L, n); /* result is integer */
else
lua_pushnumber(L, d); /* result is float */
} }
static int math_atan(lua_State *L) {
lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); static int math_floor (lua_State *L) {
return 1; if (lua_isinteger(L, 1))
lua_settop(L, 1); /* integer is its own floor */
else {
lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
pushnumint(L, d);
}
return 1;
} }
static int math_atan2(lua_State *L) {
lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), static int math_ceil (lua_State *L) {
luaL_checknumber(L, 2))); if (lua_isinteger(L, 1))
return 1; lua_settop(L, 1); /* integer is its own ceil */
else {
lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
pushnumint(L, d);
}
return 1;
} }
static int math_ceil(lua_State *L) {
lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));
return 1;
}
static int math_floor(lua_State *L) { static int math_fmod (lua_State *L) {
lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
return 1; lua_Integer d = lua_tointeger(L, 2);
} if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
luaL_argcheck(L, d != 0, 2, "zero");
static int math_fmod(lua_State *L) { lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
}
else
lua_pushinteger(L, lua_tointeger(L, 1) % d);
}
else
lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
luaL_checknumber(L, 2))); luaL_checknumber(L, 2)));
return 1; return 1;
} }
static int math_modf(lua_State *L) {
lua_Number ip; /*
lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); ** next function does not use 'modf', avoiding problems with 'double*'
lua_pushnumber(L, ip); ** (which is not compatible with 'float*') when lua_Number is not
lua_pushnumber(L, fp); ** 'double'.
return 2; */
static int math_modf (lua_State *L) {
if (lua_isinteger(L ,1)) {
lua_settop(L, 1); /* number is its own integer part */
lua_pushnumber(L, 0); /* no fractional part */
}
else {
lua_Number n = luaL_checknumber(L, 1);
/* integer part (rounds toward zero) */
lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
pushnumint(L, ip);
/* fractional part (test needed for inf/-inf) */
lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
}
return 2;
} }
static int math_sqrt(lua_State *L) {
lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); static int math_sqrt (lua_State *L) {
return 1; lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
return 1;
} }
static int math_pow(lua_State *L) {
lua_Number x = luaL_checknumber(L, 1); static int math_ult (lua_State *L) {
lua_Number y = luaL_checknumber(L, 2); lua_Integer a = luaL_checkinteger(L, 1);
lua_pushnumber(L, l_mathop(pow)(x, y)); lua_Integer b = luaL_checkinteger(L, 2);
return 1; lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
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;
if (lua_isnoneornil(L, 2)) if (lua_isnoneornil(L, 2))
res = l_mathop(log)(x); res = l_mathop(log)(x);
else { else {
lua_Number base = luaL_checknumber(L, 2); lua_Number base = luaL_checknumber(L, 2);
if (base == (lua_Number)10.0) res = l_mathop(log10)(x); #if !defined(LUA_USE_C89)
else res = l_mathop(log)(x) / l_mathop(log)(base); if (base == l_mathop(2.0))
} res = l_mathop(log2)(x);
lua_pushnumber(L, res); else
return 1; #endif
if (base == l_mathop(10.0))
res = l_mathop(log10)(x);
else
res = l_mathop(log)(x)/l_mathop(log)(base);
}
lua_pushnumber(L, res);
return 1;
} }
#if defined(LUA_COMPAT_LOG10) static int math_exp (lua_State *L) {
static int math_log10(lua_State *L) { lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); return 1;
return 1;
} }
static int math_deg (lua_State *L) {
lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
return 1;
}
static int math_rad (lua_State *L) {
lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
return 1;
}
static int math_min (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int imin = 1; /* index of current minimum value */
int i;
luaL_argcheck(L, n >= 1, 1, "value expected");
for (i = 2; i <= n; i++) {
if (lua_compare(L, i, imin, LUA_OPLT))
imin = i;
}
lua_pushvalue(L, imin);
return 1;
}
static int math_max (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int imax = 1; /* index of current maximum value */
int i;
luaL_argcheck(L, n >= 1, 1, "value expected");
for (i = 2; i <= n; i++) {
if (lua_compare(L, imax, i, LUA_OPLT))
imax = i;
}
lua_pushvalue(L, imax);
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;
}
/*
** {==================================================================
** Pseudo-Random Number Generator based on 'xoshiro256**'.
** ===================================================================
*/
/*
** This code uses lots of shifts. ANSI 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 #endif
static int math_exp(lua_State *L) {
lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); /*
return 1; ** 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 int math_deg(lua_State *L) { static Rand64 nextrand (Rand64 *state) {
lua_pushnumber(L, luaL_checknumber(L, 1) / RADIANS_PER_DEGREE); Rand64 state0 = state[0];
return 1; Rand64 state1 = state[1];
} Rand64 state2 = state[2] ^ state0;
Rand64 state3 = state[3] ^ state1;
static int math_rad(lua_State *L) { Rand64 res = rotl(state1 * 5, 7) * 9;
lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); state[0] = state0 ^ state3;
return 1; state[1] = state1 ^ state2;
} state[2] = state2 ^ (state1 << 17);
state[3] = rotl(state3, 45);
static int math_frexp(lua_State *L) { return res;
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 = luaL_checkint(L, 2);
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
return 1;
} }
/*
** 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.
*/
static int math_min(lua_State *L) { /* must throw out the extra (64 - FIGS) bits */
int n = lua_gettop(L); /* number of arguments */ #define shift64_FIG (64 - FIGS)
lua_Number dmin = luaL_checknumber(L, 1);
int i; /* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */
for (i = 2; i <= n; i++) { #define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))
lua_Number d = luaL_checknumber(L, i);
if (d < dmin) static lua_Number I2d (Rand64 x) {
dmin = d; 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' }{ */
/* get an integer with at least 32 bits */
#if LUAI_IS32INT
typedef unsigned int lu_int32;
#else
typedef unsigned long lu_int32;
#endif
/*
** Use two 32-bit integers to represent a 64-bit quantity.
*/
typedef struct Rand64 {
lu_int32 h; /* higher half */
lu_int32 l; /* lower half */
} Rand64;
/*
** If 'lu_int32' 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 (lu_int32 h, lu_int32 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 ((lu_int32)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((lu_int32)((n >> 31) >> 1), (lu_int32)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). Otherwise, 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) {
if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */
return ran & n; /* no bias */
else {
lua_Unsigned lim = n;
/* compute the smallest (2^b - 1) not smaller than 'n' */
lim |= (lim >> 1);
lim |= (lim >> 2);
lim |= (lim >> 4);
lim |= (lim >> 8);
lim |= (lim >> 16);
#if (LUA_MAXUNSIGNED >> 31) >= 3
lim |= (lim >> 32); /* integer type has more than 32 bits */
#endif
lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */
&& lim >= n /* not smaller than 'n', */
&& (lim >> 1) < n); /* and it is the smallest one */
while ((ran &= lim) > n) /* project 'ran' into [0..lim] */
ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
return ran;
}
}
static int math_random (lua_State *L) {
lua_Integer low, up;
lua_Unsigned p;
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 */
case 0: { /* no arguments */
lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */
return 1;
} }
lua_pushnumber(L, dmin); case 1: { /* only upper limit */
return 1; low = 1;
} up = luaL_checkinteger(L, 1);
if (up == 0) { /* single 0 as argument? */
lua_pushinteger(L, I2UInt(rv)); /* full random integer */
static int math_max(lua_State *L) { return 1;
int n = lua_gettop(L); /* number of arguments */ }
lua_Number dmax = luaL_checknumber(L, 1); break;
int i;
for (i = 2; i <= n; i++) {
lua_Number d = luaL_checknumber(L, i);
if (d > dmax)
dmax = d;
} }
lua_pushnumber(L, dmax); case 2: { /* lower and upper limits */
return 1; low = luaL_checkinteger(L, 1);
} up = luaL_checkinteger(L, 2);
break;
static int math_random(lua_State *L) {
/* the `%' avoids the (rare) case of r==1, and is needed also because on
some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
lua_Number r = (lua_Number)(rand() % RAND_MAX) / (lua_Number)RAND_MAX;
switch (lua_gettop(L)) { /* check number of arguments */
case 0: { /* no arguments */
lua_pushnumber(L, r); /* Number between 0 and 1 */
break;
}
case 1: { /* only upper limit */
lua_Number u = luaL_checknumber(L, 1);
luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty");
lua_pushnumber(L, l_mathop(floor)(r * u) + (lua_Number)(1.0)); /* [1, u] */
break;
}
case 2: { /* lower and upper limits */
lua_Number l = luaL_checknumber(L, 1);
lua_Number u = luaL_checknumber(L, 2);
luaL_argcheck(L, l <= u, 2, "interval is empty");
lua_pushnumber(L, l_mathop(floor)(r * (u - l + 1)) + l); /* [l, u] */
break;
}
default:
return luaL_error(L, "wrong number of arguments");
} }
return 1; default: return luaL_error(L, "wrong number of arguments");
}
/* random integer in the interval [low, up] */
luaL_argcheck(L, low <= up, 1, "interval is empty");
/* project random integer into the interval [0, up - low] */
p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
lua_pushinteger(L, p + (lua_Unsigned)low);
return 1;
} }
static int math_randomseed(lua_State *L) { static void setseed (lua_State *L, Rand64 *state,
srand(luaL_checkunsigned(L, 1)); lua_Unsigned n1, lua_Unsigned n2) {
(void)rand(); /* discard first value to avoid undesirable correlations */ int i;
return 0; state[0] = Int2I(n1);
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, n1);
lua_pushinteger(L, n2);
} }
/*
** Set a "random" seed. To get some randomness, use the current time
** and the address of 'L' (in case the machine does address space layout
** randomization).
*/
static void randseed (lua_State *L, RanState *state) {
lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
setseed(L, state->s, seed1, seed2);
}
static int math_randomseed (lua_State *L) {
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
if (lua_isnone(L, 1)) {
randseed(L, state);
}
else {
lua_Integer n1 = luaL_checkinteger(L, 1);
lua_Integer n2 = luaL_optinteger(L, 2, 0);
setseed(L, state->s, n1, n2);
}
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);
randseed(L, state); /* initialize with a "random" seed */
lua_pop(L, 2); /* remove pushed seeds */
luaL_setfuncs(L, randfuncs, 1);
}
/* }================================================================== */
/*
** {==================================================================
** Deprecated functions (for compatibility only)
** ===================================================================
*/
#if defined(LUA_COMPAT_MATHLIB)
static int math_cosh (lua_State *L) {
lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
return 1;
}
static int math_sinh (lua_State *L) {
lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
return 1;
}
static int math_tanh (lua_State *L) {
lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
return 1;
}
static int math_pow (lua_State *L) {
lua_Number x = luaL_checknumber(L, 1);
lua_Number y = luaL_checknumber(L, 2);
lua_pushnumber(L, l_mathop(pow)(x, y));
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) {
lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
return 1;
}
#endif
/* }================================================================== */
static const luaL_Reg mathlib[] = { static const luaL_Reg mathlib[] = {
{"abs", math_abs}, {"abs", math_abs},
{"acos", math_acos}, {"acos", math_acos},
{"asin", math_asin}, {"asin", math_asin},
{"atan2", math_atan2}, {"atan", math_atan},
{"atan", math_atan}, {"ceil", math_ceil},
{"ceil", math_ceil}, {"cos", math_cos},
{"cosh", math_cosh}, {"deg", math_deg},
{"cos", math_cos}, {"exp", math_exp},
{"deg", math_deg}, {"tointeger", math_toint},
{"exp", math_exp}, {"floor", math_floor},
{"floor", math_floor}, {"fmod", math_fmod},
{"fmod", math_fmod}, {"ult", math_ult},
{"frexp", math_frexp}, {"log", math_log},
{"ldexp", math_ldexp}, {"max", math_max},
#if defined(LUA_COMPAT_LOG10) {"min", math_min},
{"log10", math_log10}, {"modf", math_modf},
{"rad", math_rad},
{"sin", math_sin},
{"sqrt", math_sqrt},
{"tan", math_tan},
{"type", math_type},
#if defined(LUA_COMPAT_MATHLIB)
{"atan2", math_atan},
{"cosh", math_cosh},
{"sinh", math_sinh},
{"tanh", math_tanh},
{"pow", math_pow},
{"frexp", math_frexp},
{"ldexp", math_ldexp},
{"log10", math_log10},
#endif #endif
{"log", math_log}, /* placeholders */
{"max", math_max}, {"random", NULL},
{"min", math_min}, {"randomseed", NULL},
{"modf", math_modf}, {"pi", NULL},
{"pow", math_pow}, {"huge", NULL},
{"rad", math_rad}, {"maxinteger", NULL},
{"random", math_random}, {"mininteger", NULL},
{"randomseed", math_randomseed}, {NULL, NULL}
{"sinh", math_sinh},
{"sin", math_sin},
{"sqrt", math_sqrt},
{"tanh", math_tanh},
{"tan", math_tan},
{NULL, NULL}
}; };
/* /*
** Open math library ** Open math library
*/ */
LUAMOD_API int luaopen_math(lua_State *L) { LUAMOD_API int luaopen_math (lua_State *L) {
luaL_newlib(L, mathlib); luaL_newlib(L, mathlib);
lua_pushnumber(L, PI); lua_pushnumber(L, PI);
lua_setfield(L, -2, "pi"); lua_setfield(L, -2, "pi");
lua_pushnumber(L, HUGE_VAL); lua_pushnumber(L, (lua_Number)HUGE_VAL);
lua_setfield(L, -2, "huge"); lua_setfield(L, -2, "huge");
return 1; lua_pushinteger(L, LUA_MAXINTEGER);
lua_setfield(L, -2, "maxinteger");
lua_pushinteger(L, LUA_MININTEGER);
lua_setfield(L, -2, "mininteger");
setrandfunc(L);
return 1;
} }

View file

@ -1,15 +1,17 @@
/* /*
** $Id: lmem.c,v 1.84 2012/05/23 15:41:53 roberto Exp $ ** $Id: lmem.c $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <stddef.h>
#define lmem_c #define lmem_c
#define LUA_CORE #define LUA_CORE
#include "lprefix.h"
#include <stddef.h>
#include "lua.h" #include "lua.h"
#include "ldebug.h" #include "ldebug.h"
@ -23,76 +25,191 @@
/* /*
** 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, NULL, x, s) creates a new block of size `s' (no ** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.
** matter 'x'). ** Particularly, frealloc(ud, NULL, 0, 0) does nothing,
** which is equivalent to free(NULL) in ISO C.
** **
** * frealloc(ud, p, x, 0) frees the block `p' ** - frealloc(ud, NULL, x, s) creates a new block of size 's'
** (in this specific case, frealloc must return NULL); ** (no matter 'x'). Returns NULL if it cannot create the new block.
** particularly, frealloc(ud, NULL, 0, 0) does nothing
** (which is equivalent to free(NULL) in ANSI C)
** **
** frealloc returns NULL if it cannot create or reallocate the area ** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from
** (any reallocation to an equal or smaller size cannot fail!) ** size 'x' to size 'y'. Returns NULL if it cannot reallocate the
** block to the new size.
*/ */
/*
#define MINSIZEARRAY 4 ** Macro to call the allocation function.
*/
#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))
void *luaM_growaux_(lua_State *L, void *block, int *size, size_t size_elems, /*
int limit, const char *what) { ** When an allocation fails, it will try again after an emergency
void *newblock; ** collection, except when it cannot run a collection. The GC should
int newsize; ** not be called while the state is not fully built, as the collector
if (*size >= limit / 2) { /* cannot double it? */ ** is not yet fully initialized. Also, it should not be called when
if (*size >= limit) /* cannot grow even a little? */ ** 'gcstopem' is true, because then the interpreter is in the middle of
luaG_runerror(L, "too many %s (limit is %d)", what, limit); ** a collection step.
newsize = limit; /* still have at least one free place */ */
} else { #define cantryagain(g) (completestate(g) && !g->gcstopem)
newsize = (*size) * 2;
if (newsize < MINSIZEARRAY)
newsize = MINSIZEARRAY; /* minimum size */
}
newblock = luaM_reallocv(L, block, *size, newsize, size_elems); #if defined(EMERGENCYGCTESTS)
*size = newsize; /* update only when everything else is OK */ /*
return newblock; ** 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
l_noret luaM_toobig(lua_State *L) {
luaG_runerror(L, "memory allocation error: block too big");
}
/* /*
** generic allocation routine. ** {==================================================================
** Functions to allocate/deallocate arrays for the Parser
** ===================================================================
*/ */
void *luaM_realloc_(lua_State *L, void *block, size_t osize, size_t nsize) {
void *newblock; /*
global_State *g = G(L); ** Minimum size for arrays during parsing, to avoid overhead of
size_t realosize = (block) ? osize : 0; ** reallocating to size 1, then 2, and then 4. All these arrays
lua_assert((realosize == 0) == (block == NULL)); ** will be reallocated to exact sizes or erased when parsing ends.
#if defined(HARDMEMTESTS) */
if (nsize > realosize && g->gcrunning) #define MINSIZEARRAY 4
luaC_fullgc(L, 1); /* force a GC whenever possible */
#endif
newblock = (*g->frealloc)(g->ud, block, osize, nsize); void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
if (newblock == NULL && nsize > 0) { int size_elems, int limit, const char *what) {
api_check(L, nsize > realosize, void *newblock;
"realloc cannot fail when shrinking a block"); int size = *psize;
if (g->gcrunning) { if (nelems + 1 <= size) /* does one extra element still fit? */
luaC_fullgc(L, 1); /* try to free some memory... */ return block; /* nothing to be done */
newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ if (size >= limit / 2) { /* cannot double it? */
} if (l_unlikely(size >= limit)) /* cannot grow even a little? */
if (newblock == NULL) luaG_runerror(L, "too many %s (limit is %d)", what, limit);
luaD_throw(L, LUA_ERRMEM); size = limit; /* still have at least one free place */
} }
lua_assert((nsize == 0) == (newblock == NULL)); else {
g->GCdebt = (g->GCdebt + nsize) - realosize; size *= 2;
return newblock; if (size < MINSIZEARRAY)
size = MINSIZEARRAY; /* minimum size */
}
lua_assert(nelems + 1 <= size && size <= limit);
/* 'limit' ensures that multiplication will not overflow */
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;
} }
/*
** 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, int 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) {
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 -= osize;
}
/*
** In case of allocation fail, this function will do an emergency
** 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 *newblock;
global_State *g = G(L);
lua_assert((osize == 0) == (block == NULL));
newblock = firsttry(g, block, osize, nsize);
if (l_unlikely(newblock == NULL && nsize > 0)) {
newblock = tryagain(L, block, osize, nsize);
if (newblock == NULL) /* still no memory? */
return NULL; /* do not update 'GCdebt' */
}
lua_assert((nsize == 0) == (newblock == NULL));
g->GCdebt = (g->GCdebt + nsize) - osize;
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, tag, size);
if (l_unlikely(newblock == NULL)) {
newblock = tryagain(L, NULL, tag, size);
if (newblock == NULL)
luaM_error(L);
}
g->GCdebt += size;
return newblock;
}
}

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lmem.h,v 1.40 2013/02/20 14:08:21 roberto Exp $ ** $Id: lmem.h $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -14,44 +14,80 @@
#include "lua.h" #include "lua.h"
#define luaM_error(L) luaD_throw(L, LUA_ERRMEM)
/* /*
** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is ** This macro tests whether it is safe to multiply 'n' by the size of
** always constant. ** type 't' without overflows. Because 'e' is always constant, it avoids
** The macro is somewhat complex to avoid warnings: ** the runtime division MAX_SIZET/(e).
** +1 avoids warnings of "comparison has constant result"; ** (The macro is somewhat complex to avoid warnings: The 'sizeof'
** cast to 'void' avoids warnings of "value unused". ** comparison avoids a runtime comparison when overflow cannot occur.
** The compiler should be able to optimize the real test by itself, but
** when it does it, it may give a warning about "comparison is always
** false due to limited range of data type"; the +1 tricks the compiler,
** avoiding this warning but also this optimization.)
*/ */
#define luaM_reallocv(L,b,on,n,e) \ #define luaM_testsize(n,e) \
(cast(void, \ (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e))
(cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \
luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) #define luaM_checksize(L,n,e) \
#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0))
#define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0]))
#define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s))
#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
#define luaM_newvector(L,n,t) \
cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
#define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) /*
** 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' or 'unsigned int' and that 'int' is not larger than 'size_t'.)
*/
#define luaM_limitN(n,t) \
((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \
cast_uint((MAX_SIZET/sizeof(t))))
/*
** Arrays of chars do not need any test
*/
#define luaM_reallocvchar(L,b,on,n) \
cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
#define luaM_freemem(L, b, s) luaM_free_(L, (b), (s))
#define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b)))
#define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b)))
#define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0))
#define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0))
#define luaM_newvectorchecked(L,n,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_growvector(L,v,nelems,size,t,limit,e) \ #define luaM_growvector(L,v,nelems,size,t,limit,e) \
if ((nelems)+1 > (size)) \ ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \
((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) luaM_limitN(limit,t),e)))
#define luaM_reallocvector(L, v,oldn,n,t) \ #define luaM_reallocvector(L, v,oldn,n,t) \
((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \
cast_sizet(n) * sizeof(t))))
LUAI_FUNC l_noret luaM_toobig(lua_State *L); #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);
/* 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_growaux_(lua_State *L, void *block, int *size, LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
size_t size_elem, int limit, size_t size);
const char *what); 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, int size_elem, int limit,
const char *what);
LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
int final_n, int size_elem);
LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -1,17 +1,22 @@
/* /*
** $Id: lobject.c,v 2.58 2013/02/20 14:08:56 roberto Exp $ ** $Id: lobject.c $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#define lobject_c
#define LUA_CORE
#include "lprefix.h"
#include <locale.h>
#include <math.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define lobject_c
#define LUA_CORE
#include "lua.h" #include "lua.h"
#include "lctype.h" #include "lctype.h"
@ -24,270 +29,574 @@
#include "lvm.h" #include "lvm.h"
/*
** 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,
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,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
};
int l = 0;
x--;
while (x >= 256) { l += 8; x >>= 8; }
return l + log_2[x];
}
static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
lua_Integer v2) {
switch (op) {
case LUA_OPADD: return intop(+, v1, v2);
case LUA_OPSUB:return intop(-, v1, v2);
case LUA_OPMUL:return intop(*, v1, v2);
case LUA_OPMOD: return luaV_mod(L, v1, v2);
case LUA_OPIDIV: return luaV_idiv(L, v1, v2);
case LUA_OPBAND: return intop(&, v1, v2);
case LUA_OPBOR: return intop(|, v1, v2);
case LUA_OPBXOR: return intop(^, v1, v2);
case LUA_OPSHL: return luaV_shiftl(v1, v2);
case LUA_OPSHR: return luaV_shiftr(v1, v2);
case LUA_OPUNM: return intop(-, 0, v1);
case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
default: lua_assert(0); return 0;
}
}
static lua_Number numarith (lua_State *L, int op, lua_Number v1,
lua_Number v2) {
switch (op) {
case LUA_OPADD: return luai_numadd(L, v1, v2);
case LUA_OPSUB: return luai_numsub(L, v1, v2);
case LUA_OPMUL: return luai_nummul(L, v1, v2);
case LUA_OPDIV: return luai_numdiv(L, v1, v2);
case LUA_OPPOW: return luai_numpow(L, v1, v2);
case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
case LUA_OPUNM: return luai_numunm(L, v1);
case LUA_OPMOD: return luaV_modf(L, v1, v2);
default: lua_assert(0); return 0;
}
}
int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2,
TValue *res) {
switch (op) {
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
case LUA_OPSHL: case LUA_OPSHR:
case LUA_OPBNOT: { /* operate only on integers */
lua_Integer i1; lua_Integer i2;
if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) {
setivalue(res, intarith(L, op, i1, i2));
return 1;
}
else return 0; /* fail */
}
case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
lua_Number n1; lua_Number n2;
if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
setfltvalue(res, numarith(L, op, n1, n2));
return 1;
}
else return 0; /* fail */
}
default: { /* other operations */
lua_Number n1; lua_Number n2;
if (ttisinteger(p1) && ttisinteger(p2)) {
setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
return 1;
}
else if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
setfltvalue(res, numarith(L, op, n1, n2));
return 1;
}
else return 0; /* fail */
}
}
}
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
StkId res) {
if (!luaO_rawarith(L, op, p1, p2, s2v(res))) {
/* could not perform raw operation; try metamethod */
luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
}
}
int luaO_hexavalue (int c) {
if (lisdigit(c)) return c - '0';
else return (ltolower(c) - 'a') + 10;
}
static int isneg (const char **s) {
if (**s == '-') { (*s)++; return 1; }
else if (**s == '+') (*s)++;
return 0;
}
LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
/* /*
** converts an integer to a "floating point byte", represented as ** {==================================================================
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if ** Lua's implementation for 'lua_strx2number'
** eeeee != 0 and (xxx) otherwise. ** ===================================================================
*/ */
int luaO_int2fb(unsigned int x) {
int e = 0; /* exponent */
if (x < 8) return x;
while (x >= 0x10) {
x = (x + 1) >> 1;
e++;
}
return ((e + 1) << 3) | (cast_int(x) - 8);
}
/* converts back */
int luaO_fb2int(int x) {
int e = (x >> 3) & 0x1f;
if (e == 0) return x;
else return ((x & 7) + 8) << (e - 1);
}
int luaO_ceillog2(unsigned int x) {
static const lu_byte log_2[256] = {
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,
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,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
};
int l = 0;
x--;
while (x >= 256) { l += 8; x >>= 8; }
return l + log_2[x];
}
lua_Number luaO_arith(int op, lua_Number v1, lua_Number v2) {
switch (op) {
case LUA_OPADD:
return luai_numadd(NULL, v1, v2);
case LUA_OPSUB:
return luai_numsub(NULL, v1, v2);
case LUA_OPMUL:
return luai_nummul(NULL, v1, v2);
case LUA_OPDIV:
return luai_numdiv(NULL, v1, v2);
case LUA_OPMOD:
return luai_nummod(NULL, v1, v2);
case LUA_OPPOW:
return luai_numpow(NULL, v1, v2);
case LUA_OPUNM:
return luai_numunm(NULL, v1);
default:
lua_assert(0);
return 0;
}
}
int luaO_hexavalue(int c) {
if (lisdigit(c)) return c - '0';
else return ltolower(c) - 'a' + 10;
}
#if !defined(lua_strx2number) #if !defined(lua_strx2number)
#include <math.h> /* maximum number of significant digits to read (to avoid overflows
even with single floats) */
#define MAXSIGDIG 30
/*
static int isneg(const char **s) { ** convert a hexadecimal numeric string to a number, following
if (**s == '-') { (*s)++; return 1; } ** C99 specification for 'strtod'
else if (**s == '+')(*s)++; */
return 0; static lua_Number lua_strx2number (const char *s, char **endptr) {
int dot = lua_getlocaledecpoint();
lua_Number r = l_mathop(0.0); /* result (accumulator) */
int sigdig = 0; /* number of significant digits */
int nosigdig = 0; /* number of non-significant digits */
int e = 0; /* exponent correction */
int neg; /* 1 if number is negative */
int hasdot = 0; /* true after seen a dot */
*endptr = cast_charp(s); /* nothing is valid yet */
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
neg = isneg(&s); /* check sign */
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
return l_mathop(0.0); /* invalid format (no '0x') */
for (s += 2; ; s++) { /* skip '0x' and read numeral */
if (*s == dot) {
if (hasdot) break; /* second dot? stop loop */
else hasdot = 1;
}
else if (lisxdigit(cast_uchar(*s))) {
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
nosigdig++;
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
r = (r * l_mathop(16.0)) + luaO_hexavalue(*s);
else e++; /* too many digits; ignore, but still count for exponent */
if (hasdot) e--; /* decimal digit? correct exponent */
}
else break; /* neither a dot nor a digit */
}
if (nosigdig + sigdig == 0) /* no digits? */
return l_mathop(0.0); /* invalid format */
*endptr = cast_charp(s); /* valid up to here */
e *= 4; /* each digit multiplies/divides value by 2^4 */
if (*s == 'p' || *s == 'P') { /* exponent part? */
int exp1 = 0; /* exponent value */
int neg1; /* exponent sign */
s++; /* skip 'p' */
neg1 = isneg(&s); /* sign */
if (!lisdigit(cast_uchar(*s)))
return l_mathop(0.0); /* invalid; must have at least one digit */
while (lisdigit(cast_uchar(*s))) /* read exponent */
exp1 = exp1 * 10 + *(s++) - '0';
if (neg1) exp1 = -exp1;
e += exp1;
*endptr = cast_charp(s); /* valid up to here */
}
if (neg) r = -r;
return l_mathop(ldexp)(r, e);
} }
#endif
/* }====================================================== */
static lua_Number readhexa(const char **s, lua_Number r, int *count) {
for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */ /* maximum length of a numeral to be converted to a number */
r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s))); #if !defined (L_MAXLENNUM)
(*count)++; #define L_MAXLENNUM 200
} #endif
return r;
/*
** 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) {
char *endptr;
*result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
: lua_str2number(s, &endptr);
if (endptr == s) return NULL; /* nothing recognized? */
while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */
} }
/* /*
** convert an hexadecimal numeric string to a number, following ** Convert string 's' to a Lua number (put in 'result') handling the
** C99 specification for 'strtod' ** current locale.
** 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
** locale accepts something else. In that case, the code copies 's'
** to a buffer (because 's' is read-only), changes the dot to the
** 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 lua_Number lua_strx2number(const char *s, char **endptr) { static const char *l_str2d (const char *s, lua_Number *result) {
lua_Number r = 0.0; const char *endptr;
int e = 0, i = 0; const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */
int neg = 0; /* 1 if number is negative */ int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
*endptr = cast(char *, s); /* nothing is valid yet */ if (mode == 'n') /* reject 'inf' and 'nan' */
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ return NULL;
neg = isneg(&s); /* check signal */ endptr = l_str2dloc(s, result, mode); /* try to convert */
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ if (endptr == NULL) { /* failed? may be a different locale */
return 0.0; /* invalid format (no '0x') */ char buff[L_MAXLENNUM + 1];
const char *pdot = strchr(s, '.');
if (pdot == NULL || strlen(s) > L_MAXLENNUM)
return NULL; /* string too long or no dot; fail */
strcpy(buff, s); /* copy string to buffer */
buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
endptr = l_str2dloc(buff, result, mode); /* try again */
if (endptr != NULL)
endptr = s + (endptr - buff); /* make relative to 's' */
}
return endptr;
}
#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
#define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
static const char *l_str2int (const char *s, lua_Integer *result) {
lua_Unsigned a = 0;
int empty = 1;
int neg;
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
neg = isneg(&s);
if (s[0] == '0' &&
(s[1] == 'x' || s[1] == 'X')) { /* hex? */
s += 2; /* skip '0x' */ s += 2; /* skip '0x' */
r = readhexa(&s, r, &i); /* read integer part */ for (; lisxdigit(cast_uchar(*s)); s++) {
if (*s == '.') { a = a * 16 + luaO_hexavalue(*s);
s++; /* skip dot */ empty = 0;
r = readhexa(&s, r, &e); /* read fractional part */
} }
if (i == 0 && e == 0) }
return 0.0; /* invalid format (no digit) */ else { /* decimal */
e *= -4; /* each fractional digit divides value by 2^-4 */ for (; lisdigit(cast_uchar(*s)); s++) {
*endptr = cast(char *, s); /* valid up to here */ int d = *s - '0';
if (*s == 'p' || *s == 'P') { /* exponent part? */ if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
int exp1 = 0; return NULL; /* do not accept it (as integer) */
int neg1; a = a * 10 + d;
s++; /* skip 'p' */ empty = 0;
neg1 = isneg(&s); /* signal */
if (!lisdigit(cast_uchar(*s)))
goto ret; /* must have at least one digit */
while (lisdigit(cast_uchar(*s))) /* read exponent */
exp1 = exp1 * 10 + *(s++) - '0';
if (neg1) exp1 = -exp1;
e += exp1;
} }
*endptr = cast(char *, s); /* valid up to here */ }
ret: while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
if (neg) r = -r; if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
return l_mathop(ldexp)(r, e); else {
} *result = l_castU2S((neg) ? 0u - a : a);
return s;
#endif }
int luaO_str2d(const char *s, size_t len, lua_Number *result) {
char *endptr;
if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
return 0;
else if (strpbrk(s, "xX")) /* hexa? */
*result = lua_strx2number(s, &endptr);
else
*result = lua_str2number(s, &endptr);
if (endptr == s) return 0; /* nothing recognized */
while (lisspace(cast_uchar(*endptr))) endptr++;
return (endptr == s + len); /* OK if no trailing characters */
} }
size_t luaO_str2num (const char *s, TValue *o) {
static void pushstr(lua_State *L, const char *str, size_t l) { lua_Integer i; lua_Number n;
setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); const char *e;
if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
setivalue(o, i);
}
else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
setfltvalue(o, n);
}
else
return 0; /* conversion failed */
return (e - s) + 1; /* success; return string size */
} }
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ int luaO_utf8esc (char *buff, unsigned long x) {
const char *luaO_pushvfstring(lua_State *L, const char *fmt, va_list argp) { int n = 1; /* number of bytes put in buffer (backwards) */
int n = 0; lua_assert(x <= 0x7FFFFFFFu);
for (;;) { if (x < 0x80) /* ascii? */
const char *e = strchr(fmt, '%'); buff[UTF8BUFFSZ - 1] = cast_char(x);
if (e == NULL) break; else { /* need continuation bytes */
luaD_checkstack(L, 2); /* fmt + item */ unsigned int mfb = 0x3f; /* maximum that fits in first byte */
pushstr(L, fmt, e - fmt); do { /* add continuation bytes */
switch (*(e + 1)) { buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f));
case 's': { x >>= 6; /* remove added bits */
const char *s = va_arg(argp, char *); mfb >>= 1; /* now there is one less bit available in first byte */
if (s == NULL) s = "(null)"; } while (x > mfb); /* still needs continuation byte? */
pushstr(L, s, strlen(s)); buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */
break; }
} return n;
case 'c': { }
char buff;
buff = cast(char, va_arg(argp, int));
pushstr(L, &buff, 1); /*
break; ** Maximum length of the conversion of a number to a string. Must be
} ** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT.
case 'd': { ** (For a long long int, this is 19 digits plus a sign and a final '\0',
setnvalue(L->top++, cast_num(va_arg(argp, int))); ** adding to 21. For a long double, it can go to a sign, 33 digits,
break; ** the dot, an exponent letter, an exponent sign, 5 exponent digits,
} ** and a final '\0', adding to 43.)
case 'f': { */
setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); #define MAXNUMBER2STR 44
break;
}
case 'p': { /*
char buff[4 * sizeof(void *) + 8]; /* should be enough space for a `%p' */ ** Convert a number object to a string, adding it to a buffer
int l = snprintf(buff, sizeof(buff), "%p", va_arg(argp, void *)); */
pushstr(L, buff, l); static int tostringbuff (TValue *obj, char *buff) {
break; int len;
} lua_assert(ttisnumber(obj));
case '%': { if (ttisinteger(obj))
pushstr(L, "%", 1); len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj));
break; else {
} len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj));
default: { if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
luaG_runerror(L, buff[len++] = lua_getlocaledecpoint();
"invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"), buff[len++] = '0'; /* adds '.0' to result */
*(e + 1));
}
}
n += 2;
fmt = e + 2;
} }
luaD_checkstack(L, 1); }
pushstr(L, fmt, strlen(fmt)); return len;
if (n > 0) luaV_concat(L, n + 1);
return svalue(L->top - 1);
} }
const char *luaO_pushfstring(lua_State *L, const char *fmt, ...) { /*
const char *msg; ** Convert a number object to a Lua string, replacing the value at 'obj'
va_list argp; */
va_start(argp, fmt); void luaO_tostring (lua_State *L, TValue *obj) {
msg = luaO_pushvfstring(L, fmt, argp); char buff[MAXNUMBER2STR];
va_end(argp); int len = tostringbuff(obj, buff);
return msg; setsvalue(L, obj, luaS_newlstr(L, buff, len));
} }
/* number of chars of a literal string without the ending \0 */
#define LL(x) (sizeof(x)/sizeof(char) - 1)
#define RETS "..."
#define PRE "[string \""
#define POS "\"]"
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) /*
** {==================================================================
** 'luaO_pushvfstring'
** ===================================================================
*/
void luaO_chunkid(char *out, const char *source, size_t bufflen) { /*
size_t l = strlen(source); ** Size for buffer space used by 'luaO_pushvfstring'. It should be
if (*source == '=') { /* 'literal' source */ ** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages,
if (l <= bufflen) /* small enough? */ ** so that 'luaG_addinfo' can work directly on the buffer.
memcpy(out, source + 1, l * sizeof(char)); */
else { /* truncate it */ #define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95)
addstr(out, source + 1, bufflen - 1);
*out = '\0'; /* buffer used by 'luaO_pushvfstring' */
} typedef struct BuffFS {
} else if (*source == '@') { /* file name */ lua_State *L;
if (l <= bufflen) /* small enough? */ int pushed; /* true if there is a part of the result on the stack */
memcpy(out, source + 1, l * sizeof(char)); int blen; /* length of partial string in 'space' */
else { /* add '...' before rest of name */ char space[BUFVFS]; /* holds last part of the result */
addstr(out, RETS, LL(RETS)); } BuffFS;
bufflen -= LL(RETS);
memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
} /*
} else { /* string; format as [string "source"] */ ** Push given string to the stack, as part of the result, and
const char *nl = strchr(source, '\n'); /* find first new line (if any) */ ** join it to previous partial result if there is one.
addstr(out, PRE, LL(PRE)); /* add prefix */ ** It may call 'luaV_concat' while using one slot from EXTRA_STACK.
bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ ** This call cannot invoke metamethods, as both operands must be
if (l < bufflen && nl == NULL) { /* small one-line source? */ ** strings. It can, however, raise an error if the result is too
addstr(out, source, l); /* keep it */ ** long. In that case, 'luaV_concat' frees the extra slot before
} else { ** raising the error.
if (nl != NULL) l = nl - source; /* stop at first newline */ */
if (l > bufflen) l = bufflen; static void pushstr (BuffFS *buff, const char *str, size_t lstr) {
addstr(out, source, l); lua_State *L = buff->L;
addstr(out, RETS, LL(RETS)); setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr));
} L->top.p++; /* may use one slot from EXTRA_STACK */
memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); if (!buff->pushed) /* no previous string on the stack? */
buff->pushed = 1; /* now there is one */
else /* join previous string with new one */
luaV_concat(L, 2);
}
/*
** empty the buffer space into the stack
*/
static void clearbuff (BuffFS *buff) {
pushstr(buff, buff->space, buff->blen); /* push buffer contents */
buff->blen = 0; /* space now is empty */
}
/*
** Get a space of size 'sz' in the buffer. If buffer has not enough
** space, empty it. 'sz' must fit in an empty buffer.
*/
static char *getbuff (BuffFS *buff, int sz) {
lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS);
if (sz > BUFVFS - buff->blen) /* not enough space? */
clearbuff(buff);
return buff->space + buff->blen;
}
#define addsize(b,sz) ((b)->blen += (sz))
/*
** Add 'str' to the buffer. If string is larger than the buffer space,
** push the string directly to the stack.
*/
static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
if (slen <= BUFVFS) { /* does string fit into buffer? */
char *bf = getbuff(buff, cast_int(slen));
memcpy(bf, str, slen); /* add string to buffer */
addsize(buff, cast_int(slen));
}
else { /* string larger than buffer */
clearbuff(buff); /* string comes after buffer's content */
pushstr(buff, str, slen); /* push string */
}
}
/*
** Add a numeral to the buffer.
*/
static void addnum2buff (BuffFS *buff, TValue *num) {
char *numbuff = getbuff(buff, MAXNUMBER2STR);
int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */
addsize(buff, len);
}
/*
** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%'
conventional formats, plus Lua-specific '%I' and '%U'
*/
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
BuffFS buff; /* holds last part of the result */
const char *e; /* points to next '%' */
buff.pushed = buff.blen = 0;
buff.L = L;
while ((e = strchr(fmt, '%')) != NULL) {
addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */
switch (*(e + 1)) { /* conversion specifier */
case 's': { /* zero-terminated string */
const char *s = va_arg(argp, char *);
if (s == NULL) s = "(null)";
addstr2buff(&buff, s, strlen(s));
break;
}
case 'c': { /* an 'int' as a character */
char c = cast_uchar(va_arg(argp, int));
addstr2buff(&buff, &c, sizeof(char));
break;
}
case 'd': { /* an 'int' */
TValue num;
setivalue(&num, va_arg(argp, int));
addnum2buff(&buff, &num);
break;
}
case 'I': { /* a 'lua_Integer' */
TValue num;
setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt)));
addnum2buff(&buff, &num);
break;
}
case 'f': { /* a 'lua_Number' */
TValue num;
setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber)));
addnum2buff(&buff, &num);
break;
}
case 'p': { /* a pointer */
const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
char *bf = getbuff(&buff, sz);
void *p = va_arg(argp, void *);
int len = lua_pointer2str(bf, sz, p);
addsize(&buff, len);
break;
}
case 'U': { /* a 'long' as a UTF-8 sequence */
char bf[UTF8BUFFSZ];
int len = luaO_utf8esc(bf, va_arg(argp, long));
addstr2buff(&buff, bf + UTF8BUFFSZ - len, len);
break;
}
case '%': {
addstr2buff(&buff, "%", 1);
break;
}
default: {
luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
*(e + 1));
}
} }
fmt = e + 2; /* skip '%' and the specifier */
}
addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */
clearbuff(&buff); /* empty buffer into the stack */
lua_assert(buff.pushed == 1);
return getstr(tsvalue(s2v(L->top.p - 1)));
}
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
const char *msg;
va_list argp;
va_start(argp, fmt);
msg = luaO_pushvfstring(L, fmt, argp);
va_end(argp);
return msg;
}
/* }================================================================== */
#define RETS "..."
#define PRE "[string \""
#define POS "\"]"
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
void luaO_chunkid (char *out, const char *source, size_t srclen) {
size_t bufflen = LUA_IDSIZE; /* free space in buffer */
if (*source == '=') { /* 'literal' source */
if (srclen <= bufflen) /* small enough? */
memcpy(out, source + 1, srclen * sizeof(char));
else { /* truncate it */
addstr(out, source + 1, bufflen - 1);
*out = '\0';
}
}
else if (*source == '@') { /* file name */
if (srclen <= bufflen) /* small enough? */
memcpy(out, source + 1, srclen * sizeof(char));
else { /* add '...' before rest of name */
addstr(out, RETS, LL(RETS));
bufflen -= LL(RETS);
memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char));
}
}
else { /* string; format as [string "source"] */
const char *nl = strchr(source, '\n'); /* find first new line (if any) */
addstr(out, PRE, LL(PRE)); /* add prefix */
bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
if (srclen < bufflen && nl == NULL) { /* small one-line source? */
addstr(out, source, srclen); /* keep it */
}
else {
if (nl != NULL) srclen = nl - source; /* stop at first newline */
if (srclen > bufflen) srclen = bufflen;
addstr(out, source, srclen);
addstr(out, RETS, LL(RETS));
}
memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
}
} }

File diff suppressed because it is too large Load diff

View file

@ -1,107 +1,104 @@
/* /*
** $Id: lopcodes.c,v 1.49 2012/05/14 13:34:18 roberto Exp $ ** $Id: lopcodes.c $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#define lopcodes_c #define lopcodes_c
#define LUA_CORE #define LUA_CORE
#include "lprefix.h"
#include "lopcodes.h" #include "lopcodes.h"
/* ORDER OP */ /* ORDER OP */
LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES + 1] = {
"MOVE",
"LOADK",
"LOADKX",
"LOADBOOL",
"LOADNIL",
"GETUPVAL",
"GETTABUP",
"GETTABLE",
"SETTABUP",
"SETUPVAL",
"SETTABLE",
"NEWTABLE",
"SELF",
"ADD",
"SUB",
"MUL",
"DIV",
"MOD",
"POW",
"UNM",
"NOT",
"LEN",
"CONCAT",
"JMP",
"EQ",
"LT",
"LE",
"TEST",
"TESTSET",
"CALL",
"TAILCALL",
"RETURN",
"FORLOOP",
"FORPREP",
"TFORCALL",
"TFORLOOP",
"SETLIST",
"CLOSURE",
"VARARG",
"EXTRAARG",
NULL
};
#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] = { LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
/* T A B C mode opcode */ /* MM OT IT T A mode opcode */
opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */
, opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */
, opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */
, opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */
, opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */
, opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */
, opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */
, opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */
, opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */
, opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */
, opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */
, opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */
, opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */
, opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */
, opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */
, opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */
, opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */
, opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */
, opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */
, opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */
, opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */
, opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */
, opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */
, opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */
, opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */
, opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */
, opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */
, opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */
, opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */
, opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */
, opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */
, opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */
, opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */
, opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */
, opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */
, opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */
, opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */
, opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */
, opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */
, opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */
,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, iABC) /* OP_SETLIST */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */
,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */
,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */
,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */
}; };

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.142.1.1 2013/04/12 18:48:47 roberto Exp roberto $ ** $Id: lopcodes.h $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,278 +11,395 @@
/*=========================================================================== /*===========================================================================
We assume that instructions are unsigned numbers. We assume that instructions are unsigned 32-bit integers.
All instructions have an opcode in the first 6 bits. All instructions have an opcode in the first 7 bits.
Instructions can have the following fields: Instructions can have the following formats:
`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
A signed argument is represented in excess K; that is, the number 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
value is the unsigned value minus K. K is exactly the maximum value 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
for that argument (so that -max is represented by 0, and +max is iABC C(8) | B(8) |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) |
A signed argument is represented in excess K: the represented value is
the written unsigned value minus K, where K is half the maximum for the
corresponding unsigned argument.
===========================================================================*/ ===========================================================================*/
enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
/* /*
** size and position of opcode arguments. ** size and position of opcode arguments.
*/ */
#define SIZE_C 9 #define SIZE_C 8
#define SIZE_B 9 #define SIZE_B 8
#define SIZE_Bx (SIZE_C + SIZE_B) #define SIZE_Bx (SIZE_C + SIZE_B + 1)
#define SIZE_A 8 #define SIZE_A 8
#define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A) #define SIZE_Ax (SIZE_Bx + SIZE_A)
#define SIZE_sJ (SIZE_Bx + SIZE_A)
#define SIZE_OP 6 #define SIZE_OP 7
#define POS_OP 0 #define POS_OP 0
#define POS_A (POS_OP + SIZE_OP)
#define POS_C (POS_A + SIZE_A) #define POS_A (POS_OP + SIZE_OP)
#define POS_B (POS_C + SIZE_C) #define POS_k (POS_A + SIZE_A)
#define POS_Bx POS_C #define POS_B (POS_k + 1)
#define POS_Ax POS_A #define POS_C (POS_B + SIZE_B)
#define POS_Bx POS_k
#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 LUAI_BITSINT-1 bits (-1 for sign) ** so they must fit in ints.
*/ */
#if SIZE_Bx < LUAI_BITSINT-1
#define MAXARG_Bx ((1<<SIZE_Bx)-1) /* Check whether type 'int' has at least 'b' bits ('b' < 32) */
#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */ #define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1)
#if L_INTHASBITS(SIZE_Bx)
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
#else #else
#define MAXARG_Bx MAX_INT #define MAXARG_Bx MAX_INT
#define MAXARG_sBx MAX_INT
#endif #endif
#if SIZE_Ax < LUAI_BITSINT-1 #define OFFSET_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
#define MAXARG_Ax ((1<<SIZE_Ax)-1)
#if L_INTHASBITS(SIZE_Ax)
#define MAXARG_Ax ((1<<SIZE_Ax)-1)
#else #else
#define MAXARG_Ax MAX_INT #define MAXARG_Ax MAX_INT
#endif #endif
#if L_INTHASBITS(SIZE_sJ)
#define MAXARG_sJ ((1 << SIZE_sJ) - 1)
#else
#define MAXARG_sJ MAX_INT
#endif
#define MAXARG_A ((1<<SIZE_A)-1) #define OFFSET_sJ (MAXARG_sJ >> 1)
#define MAXARG_B ((1<<SIZE_B)-1)
#define MAXARG_C ((1<<SIZE_C)-1)
/* creates a mask with `n' 1 bits at position `p' */ #define MAXARG_A ((1<<SIZE_A)-1)
#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p)) #define MAXARG_B ((1<<SIZE_B)-1)
#define MAXARG_C ((1<<SIZE_C)-1)
#define OFFSET_sC (MAXARG_C >> 1)
/* creates a mask with `n' 0 bits at position `p' */ #define int2sC(i) ((i) + OFFSET_sC)
#define MASK0(n,p) (~MASK1(n,p)) #define sC2int(i) ((i) - OFFSET_sC)
/* creates a mask with 'n' 1 bits at position 'p' */
#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
/* creates a mask with 'n' 0 bits at position 'p' */
#define MASK0(n,p) (~MASK1(n,p))
/* /*
** the following macros help to manipulate instructions ** the following macros help to manipulate instructions
*/ */
#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(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP)))) ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
#define getarg(i,pos,size) (cast(int, ((i)>>pos) & MASK1(size,0))) #define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m)
#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
((cast(Instruction, v)<<pos)&MASK1(size,pos))))
#define GETARG_A(i) getarg(i, POS_A, SIZE_A)
#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
#define GETARG_B(i) getarg(i, POS_B, SIZE_B)
#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
#define GETARG_C(i) getarg(i, POS_C, SIZE_C)
#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
#define GETARG_Bx(i) getarg(i, POS_Bx, SIZE_Bx)
#define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
#define GETARG_Ax(i) getarg(i, POS_Ax, SIZE_Ax)
#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \ #define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0)))
| (cast(Instruction, a)<<POS_A) \ #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
| (cast(Instruction, b)<<POS_B) \ ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
| (cast(Instruction, c)<<POS_C))
#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \ #define GETARG_A(i) getarg(i, POS_A, SIZE_A)
| (cast(Instruction, a)<<POS_A) \ #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
| (cast(Instruction, bc)<<POS_Bx))
#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \ #define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
| (cast(Instruction, a)<<POS_Ax)) #define GETARG_sB(i) sC2int(GETARG_B(i))
#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
#define GETARG_sC(i) sC2int(GETARG_C(i))
#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
#define TESTARG_k(i) check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k)))))
#define GETARG_k(i) check_exp(checkopm(i, iABC), 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 GETARG_Ax(i) check_exp(checkopm(i, iAx), getarg(i, POS_Ax, SIZE_Ax))
#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
#define GETARG_sBx(i) \
check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_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(Instruction, o)<<POS_OP) \
** Macros to operate RK indices | (cast(Instruction, a)<<POS_A) \
*/ | (cast(Instruction, b)<<POS_B) \
| (cast(Instruction, c)<<POS_C) \
| (cast(Instruction, k)<<POS_k))
/* this bit 1 means constant (0 means register) */ #define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
#define BITRK (1 << (SIZE_B - 1)) | (cast(Instruction, a)<<POS_A) \
| (cast(Instruction, bc)<<POS_Bx))
/* test whether value is a constant */ #define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \
#define ISK(x) ((x) & BITRK) | (cast(Instruction, a)<<POS_Ax))
/* gets the index of the constant */ #define CREATE_sJ(o,j,k) ((cast(Instruction, o) << POS_OP) \
#define INDEXK(r) ((int)(r) & ~BITRK) | (cast(Instruction, j) << POS_sJ) \
| (cast(Instruction, k) << POS_k))
#define MAXINDEXRK (BITRK - 1)
/* code a constant index as a RK value */ #if !defined(MAXINDEXRK) /* (for debugging only) */
#define RKASK(x) ((x) | BITRK) #define MAXINDEXRK MAXARG_B
#endif
/* /*
** invalid register that fits in 8 bits ** invalid register that fits in 8 bits
*/ */
#define NO_REG MAXARG_A #define NO_REG MAXARG_A
/* /*
** R(x) - register ** R[x] - register
** Kst(x) - constant (in constant table) ** K[x] - constant (in constant table)
** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x) ** RK(x) == if k(i) then K[x] else R[x]
*/ */
/* /*
** grep "ORDER OP" if you change these enums ** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)
** has extra descriptions in the notes after the enumeration.
*/ */
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_LOADK,/* A Bx R(A) := Kst(Bx) */ OP_LOADI,/* A sBx R[A] := sBx */
OP_LOADKX,/* A R(A) := Kst(extra arg) */ OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */
OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ OP_LOADK,/* A Bx R[A] := K[Bx] */
OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */ OP_LOADKX,/* A R[A] := K[extra arg] */
OP_GETUPVAL,/* A B R(A) := UpValue[B] */ OP_LOADFALSE,/* A R[A] := false */
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][RK(C)] */ OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */
OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ OP_GETTABLE,/* A B C R[A] := R[B][R[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][RK(B)] := RK(C) */ OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := RK(C) */
OP_SETUPVAL,/* A B UpValue[B] := R(A) */ OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */
OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ OP_SETI,/* A B C R[A][B] := RK(C) */
OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */
OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ OP_NEWTABLE,/* A B C k R[A] := {} */
OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */
OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ OP_ADDI,/* A B sC R[A] := R[B] + sC */
OP_SUB,/* A B C R(A) := RK(B) - RK(C) */
OP_MUL,/* A B C R(A) := RK(B) * RK(C) */
OP_DIV,/* 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_UNM,/* 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_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */
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_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) - 1 */ OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */
OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */
OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */
OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
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_SHLI,/* A B sC R[A] := sC << R[B] */
OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_ADD,/* 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_SUB,/* A B C R[A] := R[B] - R[C] */
OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ OP_MUL,/* A B C R[A] := R[B] * R[C] */
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_FORLOOP,/* A sBx R(A)+=R(A+2); OP_BAND,/* A B C R[A] := R[B] & R[C] */
if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ OP_BOR,/* A B C R[A] := R[B] | R[C] */
OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */ OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */
OP_SHL,/* A B C R[A] := R[B] << R[C] */
OP_SHR,/* A B C R[A] := R[B] >> R[C] */
OP_TFORCALL,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */ OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */
OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/ OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */
OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */
OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ 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] := #R[B] (length operator) */
OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */ OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */
OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */ OP_CLOSE,/* A close all upvalues >= R[A] */
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_EXTRAARG/* Ax extra (larger) argument for previous opcode */ 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] (see note) */
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 B C k R[A][C+i] := R[A+i], 1 <= i <= B */
OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */
OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
OP_VARARGPREP,/*A (adjust vararg parameters) */
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
} OpCode; } OpCode;
#define NUM_OPCODES (cast(int, OP_EXTRAARG) + 1) #define NUM_OPCODES ((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'.
(*) In OP_VARARG, if (B == 0) then use actual number of varargs and (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean
value, in a code equivalent to (not cond ? false : true). (It
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). set top (like in OP_CALL with C == 0).
(*) In OP_RETURN, if (B == 0) then return up to `top'. (*) In OP_RETURN, if (B == 0) then return up to 'top'.
(*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
'instruction' is EXTRAARG(real C). OP_EXTRAARG.
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG. (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then
real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the
bits of C).
(*) For comparisons, A specifies what condition the test should accept (*) In OP_NEWTABLE, B is log2 of the hash size (which is always a
power of 2) plus 1, or zero for size zero. If not k, the array size
is C. Otherwise, the array size is EXTRAARG _ C.
(*) For comparisons, k specifies what condition the test should accept
(true or false). (true or false).
(*) All `skips' (pc++) assume that next instruction is a jump. (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
(the constant is the first operand).
(*) All 'skips' (pc++) assume that next instruction 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 is vararg, 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-1: op mode ** bits 0-2: op mode
** bits 2-3: C arg mode ** bit 3: instruction set register A
** bits 4-5: B arg mode ** bit 4: operator is a test (next instruction must be a jump)
** bit 6: instruction set register A ** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
** bit 7: operator is a test (next instruction must be a jump) ** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
** bit 7: instruction is an MM instruction (call a metamethod)
*/ */
enum OpArgMask { LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
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 */
};
LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7))
#define testAMode(m) (luaP_opmodes[m] & (1 << 3))
#define testTMode(m) (luaP_opmodes[m] & (1 << 4))
#define testITMode(m) (luaP_opmodes[m] & (1 << 5))
#define testOTMode(m) (luaP_opmodes[m] & (1 << 6))
#define testMMMode(m) (luaP_opmodes[m] & (1 << 7))
#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3)) /* "out top" (set top for next instruction) */
#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3)) #define isOT(i) \
#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) ((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \
#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) GET_OPCODE(i) == OP_TAILCALL)
#define testTMode(m) (luaP_opmodes[m] & (1 << 7))
/* "in top" (uses top from previous instruction) */
#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES + 1]; /* opcode names */ #define opmode(mm,ot,it,t,a,m) \
(((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))
/* number of list items to accumulate before a SETLIST instruction */ /* number of list items to accumulate before a SETLIST instruction */
#define LFIELDS_PER_FLUSH 50 #define LFIELDS_PER_FLUSH 50
#endif #endif

View file

@ -0,0 +1,103 @@
/*
** $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",
"SHRI",
"SHLI",
"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",
"VARARGPREP",
"EXTRAARG",
NULL
};
#endif

View file

@ -1,9 +1,14 @@
/* /*
** $Id: loslib.c,v 1.40 2012/10/19 15:54:02 roberto Exp $ ** $Id: loslib.c $
** Standard Operating System library ** Standard Operating System library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#define loslib_c
#define LUA_LIB
#include "lprefix.h"
#include <errno.h> #include <errno.h>
#include <locale.h> #include <locale.h>
@ -11,9 +16,6 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#define loslib_c
#define LUA_LIB
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
@ -21,107 +23,171 @@
/* /*
** list of valid conversion specifiers for the 'strftime' function ** {==================================================================
** List of valid conversion specifiers for the 'strftime' function;
** options are grouped by length; group of length 2 start with '||'.
** ===================================================================
*/ */
#if !defined(LUA_STRFTIMEOPTIONS) #if !defined(LUA_STRFTIMEOPTIONS) /* { */
#if !defined(LUA_USE_POSIX)
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
#else
#define LUA_STRFTIMEOPTIONS \
{ "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \
"", "E", "cCxXyY", \
"O", "deHImMSuUVwWy" }
#endif
#if defined(LUA_USE_WINDOWS)
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
#elif defined(LUA_USE_C89) /* ANSI C 89 (only 1-char options) */
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%"
#else /* C99 specification */
#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
"||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */
#endif #endif
#endif /* } */
/* }================================================================== */
/* /*
** By default, Lua uses tmpnam except when POSIX is available, where it ** {==================================================================
** uses mkstemp. ** Configuration for time-related stuff
** ===================================================================
*/ */
#if defined(LUA_USE_MKSTEMP)
#include <unistd.h>
#define LUA_TMPNAMBUFSIZE 32
#define lua_tmpnam(b,e) { \
strcpy(b, "/tmp/lua_XXXXXX"); \
e = mkstemp(b); \
if (e != -1) close(e); \
e = (e == -1); }
#elif !defined(lua_tmpnam) /*
** type to represent time_t in Lua
*/
#if !defined(LUA_NUMTIME) /* { */
#define LUA_TMPNAMBUFSIZE L_tmpnam #define l_timet lua_Integer
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
#define l_gettime(L,arg) luaL_checkinteger(L, arg)
#endif #else /* }{ */
#define l_timet lua_Number
#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t))
#define l_gettime(L,arg) luaL_checknumber(L, arg)
#endif /* } */
#if !defined(l_gmtime) /* { */
/* /*
** By default, Lua uses gmtime/localtime, except when POSIX is available, ** By default, Lua uses gmtime/localtime, except when POSIX is available,
** where it uses gmtime_r/localtime_r ** where it uses gmtime_r/localtime_r
*/ */
#if defined(LUA_USE_GMTIME_R)
#define l_gmtime(t,r) gmtime_r(t,r) #if defined(LUA_USE_POSIX) /* { */
#define l_localtime(t,r) localtime_r(t,r)
#elif !defined(l_gmtime) #define l_gmtime(t,r) gmtime_r(t,r)
#define l_localtime(t,r) localtime_r(t,r)
#define l_gmtime(t,r) ((void)r, gmtime(t)) #else /* }{ */
#define l_localtime(t,r) ((void)r, localtime(t))
/* ISO C definitions */
#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
#endif /* } */
#endif /* } */
/* }================================================================== */
/*
** {==================================================================
** Configuration for 'tmpnam':
** By default, Lua uses tmpnam except when POSIX is available, where
** it uses mkstemp.
** ===================================================================
*/
#if !defined(lua_tmpnam) /* { */
#if defined(LUA_USE_POSIX) /* { */
#include <unistd.h>
#define LUA_TMPNAMBUFSIZE 32
#if !defined(LUA_TMPNAMTEMPLATE)
#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
#endif
#define lua_tmpnam(b,e) { \
strcpy(b, LUA_TMPNAMTEMPLATE); \
e = mkstemp(b); \
if (e != -1) close(e); \
e = (e == -1); }
#else /* }{ */
/* ISO C definitions */
#define LUA_TMPNAMBUFSIZE L_tmpnam
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
#endif /* } */
#endif /* } */
/* }================================================================== */
#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 #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;
if (cmd != NULL) stat = l_system(cmd);
return luaL_execresult(L, stat); if (cmd != NULL)
else { return luaL_execresult(L, stat);
lua_pushboolean(L, stat); /* true if there is a shell */ else {
return 1; lua_pushboolean(L, stat); /* true if there is a shell */
}
}
static int os_remove(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
return luaL_fileresult(L, remove(filename) == 0, filename);
}
static int os_rename(lua_State *L) {
const char *fromname = luaL_checkstring(L, 1);
const char *toname = luaL_checkstring(L, 2);
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
}
static int os_tmpname(lua_State *L) {
char buff[LUA_TMPNAMBUFSIZE];
int err;
lua_tmpnam(buff, err);
if (err)
return luaL_error(L, "unable to generate a unique filename");
lua_pushstring(L, buff);
return 1; return 1;
}
} }
static int os_getenv(lua_State *L) { static int os_remove (lua_State *L) {
lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ const char *filename = luaL_checkstring(L, 1);
return 1; errno = 0;
return luaL_fileresult(L, remove(filename) == 0, filename);
} }
static int os_clock(lua_State *L) { static int os_rename (lua_State *L) {
lua_pushnumber(L, ((lua_Number)clock()) / (lua_Number)CLOCKS_PER_SEC); const char *fromname = luaL_checkstring(L, 1);
return 1; const char *toname = luaL_checkstring(L, 2);
errno = 0;
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
}
static int os_tmpname (lua_State *L) {
char buff[LUA_TMPNAMBUFSIZE];
int err;
lua_tmpnam(buff, err);
if (l_unlikely(err))
return luaL_error(L, "unable to generate a unique filename");
lua_pushstring(L, buff);
return 1;
}
static int os_getenv (lua_State *L) {
lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
return 1;
}
static int os_clock (lua_State *L) {
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
return 1;
} }
@ -133,190 +199,232 @@ static int os_clock(lua_State *L) {
** ======================================================= ** =======================================================
*/ */
static void setfield(lua_State *L, const char *key, int value) { /*
lua_pushinteger(L, value); ** About the overflow check: an overflow cannot occur when time
lua_setfield(L, -2, key); ** 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
static void setboolfield(lua_State *L, const char *key, int value) { ** times are represented as doubles and lua_Integer is int, then the
if (value < 0) /* undefined? */ ** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900
return; /* does not set field */ ** to compute the year.
lua_pushboolean(L, value); */
lua_setfield(L, -2, key); 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))
static int getboolfield(lua_State *L, const char *key) { luaL_error(L, "field '%s' is out-of-bound", key);
int res; #endif
lua_getfield(L, -1, key); lua_pushinteger(L, (lua_Integer)value + delta);
res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); lua_setfield(L, -2, key);
lua_pop(L, 1);
return res;
} }
static int getfield(lua_State *L, const char *key, int d) { static void setboolfield (lua_State *L, const char *key, int value) {
int res, isnum; if (value < 0) /* undefined? */
lua_getfield(L, -1, key); return; /* does not set field */
res = (int)lua_tointegerx(L, -1, &isnum); lua_pushboolean(L, value);
if (!isnum) { lua_setfield(L, -2, key);
if (d < 0) }
return luaL_error(L, "field " LUA_QS " missing in date table", key);
res = d;
/*
** Set all fields from structure 'tm' in the table on top of the stack
*/
static void setallfields (lua_State *L, struct tm *stm) {
setfield(L, "year", stm->tm_year, 1900);
setfield(L, "month", stm->tm_mon, 1);
setfield(L, "day", stm->tm_mday, 0);
setfield(L, "hour", stm->tm_hour, 0);
setfield(L, "min", stm->tm_min, 0);
setfield(L, "sec", stm->tm_sec, 0);
setfield(L, "yday", stm->tm_yday, 1);
setfield(L, "wday", stm->tm_wday, 1);
setboolfield(L, "isdst", stm->tm_isdst);
}
static int getboolfield (lua_State *L, const char *key) {
int res;
res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
lua_pop(L, 1);
return res;
}
static int getfield (lua_State *L, const char *key, int d, int delta) {
int isnum;
int t = lua_getfield(L, -1, key); /* get field and its type */
lua_Integer res = lua_tointegerx(L, -1, &isnum);
if (!isnum) { /* field is not an integer? */
if (l_unlikely(t != LUA_TNIL)) /* some other value? */
return luaL_error(L, "field '%s' is not an integer", key);
else if (l_unlikely(d < 0)) /* absent field; no default? */
return luaL_error(L, "field '%s' missing in date table", key);
res = d;
}
else {
if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
return luaL_error(L, "field '%s' is out-of-bound", key);
res -= delta;
}
lua_pop(L, 1);
return (int)res;
}
static const char *checkoption (lua_State *L, const char *conv,
ptrdiff_t convlen, char *buff) {
const char *option = LUA_STRFTIMEOPTIONS;
int oplen = 1; /* length of options being checked */
for (; *option != '\0' && oplen <= convlen; option += oplen) {
if (*option == '|') /* next block? */
oplen++; /* will check options with next length (+1) */
else if (memcmp(conv, option, oplen) == 0) { /* match? */
memcpy(buff, conv, oplen); /* copy valid option to buffer */
buff[oplen] = '\0';
return conv + oplen; /* return next item */
} }
lua_pop(L, 1); }
return res; luaL_argerror(L, 1,
lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
return conv; /* to avoid warnings */
} }
static const char *checkoption(lua_State *L, const char *conv, char *buff) { static time_t l_checktime (lua_State *L, int arg) {
static const char *const options[] = LUA_STRFTIMEOPTIONS; l_timet t = l_gettime(L, arg);
unsigned int i; luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
for (i = 0; i < sizeof(options) / sizeof(options[0]); i += 2) { return (time_t)t;
if (*conv != '\0' && strchr(options[i], *conv) != NULL) { }
buff[1] = *conv;
if (*options[i + 1] == '\0') { /* one-char conversion specifier? */
buff[2] = '\0'; /* end buffer */ /* maximum size for an individual 'strftime' item */
return conv + 1; #define SIZETIMEFMT 250
} else if (*(conv + 1) != '\0' &&
strchr(options[i + 1], *(conv + 1)) != NULL) {
buff[2] = *(conv + 1); /* valid two-char conversion specifier */ static int os_date (lua_State *L) {
buff[3] = '\0'; /* end buffer */ size_t slen;
return conv + 2; const char *s = luaL_optlstring(L, 1, "%c", &slen);
} time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
} const char *se = s + slen; /* 's' end */
struct tm tmr, *stm;
if (*s == '!') { /* UTC? */
stm = l_gmtime(&t, &tmr);
s++; /* skip '!' */
}
else
stm = l_localtime(&t, &tmr);
if (stm == NULL) /* invalid date? */
return luaL_error(L,
"date result cannot be represented in this installation");
if (strcmp(s, "*t") == 0) {
lua_createtable(L, 0, 9); /* 9 = number of fields */
setallfields(L, stm);
}
else {
char cc[4]; /* buffer for individual conversion specifiers */
luaL_Buffer b;
cc[0] = '%';
luaL_buffinit(L, &b);
while (s < se) {
if (*s != '%') /* not a conversion specifier? */
luaL_addchar(&b, *s++);
else {
size_t reslen;
char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
s++; /* skip '%' */
s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
reslen = strftime(buff, SIZETIMEFMT, cc, stm);
luaL_addsize(&b, reslen);
}
} }
luaL_argerror(L, 1, luaL_pushresult(&b);
lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv)); }
return conv; /* to avoid warnings */ return 1;
} }
static int os_date(lua_State *L) { static int os_time (lua_State *L) {
const char *s = luaL_optstring(L, 1, "%c"); time_t t;
time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); if (lua_isnoneornil(L, 1)) /* called without args? */
struct tm tmr, *stm; t = time(NULL); /* get current time */
if (*s == '!') { /* UTC? */ else {
stm = l_gmtime(&t, &tmr); struct tm ts;
s++; /* skip `!' */ luaL_checktype(L, 1, LUA_TTABLE);
} else lua_settop(L, 1); /* make sure table is at the top */
stm = l_localtime(&t, &tmr); ts.tm_year = getfield(L, "year", -1, 1900);
if (stm == NULL) /* invalid date? */ ts.tm_mon = getfield(L, "month", -1, 1);
lua_pushnil(L); ts.tm_mday = getfield(L, "day", -1, 0);
else if (strcmp(s, "*t") == 0) { ts.tm_hour = getfield(L, "hour", 12, 0);
lua_createtable(L, 0, 9); /* 9 = number of fields */ ts.tm_min = getfield(L, "min", 0, 0);
setfield(L, "sec", stm->tm_sec); ts.tm_sec = getfield(L, "sec", 0, 0);
setfield(L, "min", stm->tm_min); ts.tm_isdst = getboolfield(L, "isdst");
setfield(L, "hour", stm->tm_hour); t = mktime(&ts);
setfield(L, "day", stm->tm_mday); setallfields(L, &ts); /* update fields with normalized values */
setfield(L, "month", stm->tm_mon + 1); }
setfield(L, "year", stm->tm_year + 1900); if (t != (time_t)(l_timet)t || t == (time_t)(-1))
setfield(L, "wday", stm->tm_wday + 1); return luaL_error(L,
setfield(L, "yday", stm->tm_yday + 1); "time result cannot be represented in this installation");
setboolfield(L, "isdst", stm->tm_isdst); l_pushtime(L, t);
} else { return 1;
char cc[4];
luaL_Buffer b;
cc[0] = '%';
luaL_buffinit(L, &b);
while (*s) {
if (*s != '%') /* no conversion specifier? */
luaL_addchar(&b, *s++);
else {
size_t reslen;
char buff[200]; /* should be big enough for any conversion result */
s = checkoption(L, s + 1, cc);
reslen = strftime(buff, sizeof(buff), cc, stm);
luaL_addlstring(&b, buff, reslen);
}
}
luaL_pushresult(&b);
}
return 1;
} }
static int os_time(lua_State *L) { static int os_difftime (lua_State *L) {
time_t t; time_t t1 = l_checktime(L, 1);
if (lua_isnoneornil(L, 1)) /* called without args? */ time_t t2 = l_checktime(L, 2);
t = time(NULL); /* get current time */ lua_pushnumber(L, (lua_Number)difftime(t1, t2));
else { return 1;
struct tm ts;
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 1); /* make sure table is at the top */
ts.tm_sec = getfield(L, "sec", 0);
ts.tm_min = getfield(L, "min", 0);
ts.tm_hour = getfield(L, "hour", 12);
ts.tm_mday = getfield(L, "day", -1);
ts.tm_mon = getfield(L, "month", -1) - 1;
ts.tm_year = getfield(L, "year", -1) - 1900;
ts.tm_isdst = getboolfield(L, "isdst");
t = mktime(&ts);
}
if (t == (time_t)(-1))
lua_pushnil(L);
else
lua_pushnumber(L, (lua_Number)t);
return 1;
}
static int os_difftime(lua_State *L) {
lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
(time_t)(luaL_optnumber(L, 2, 0))));
return 1;
} }
/* }====================================================== */ /* }====================================================== */
static int os_setlocale(lua_State *L) { static int os_setlocale (lua_State *L) {
static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
LC_NUMERIC, LC_TIME LC_NUMERIC, LC_TIME};
}; static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
static const char *const catnames[] = {"all", "collate", "ctype", "monetary", "numeric", "time", NULL};
"numeric", "time", NULL const char *l = luaL_optstring(L, 1, NULL);
}; int op = luaL_checkoption(L, 2, "all", catnames);
const char *l = luaL_optstring(L, 1, NULL); lua_pushstring(L, setlocale(cat[op], l));
int op = luaL_checkoption(L, 2, "all", catnames); return 1;
lua_pushstring(L, setlocale(cat[op], l));
return 1;
} }
static int os_exit(lua_State *L) { static int os_exit (lua_State *L) {
int status; int status;
if (lua_isboolean(L, 1)) if (lua_isboolean(L, 1))
status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
else else
status = luaL_optint(L, 1, EXIT_SUCCESS); status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
if (lua_toboolean(L, 2)) if (lua_toboolean(L, 2))
lua_close(L); lua_close(L);
if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
return 0; return 0;
} }
static const luaL_Reg syslib[] = { static const luaL_Reg syslib[] = {
{"clock", os_clock}, {"clock", os_clock},
{"date", os_date}, {"date", os_date},
{"difftime", os_difftime}, {"difftime", os_difftime},
{"execute", os_execute}, {"execute", os_execute},
{"exit", os_exit}, {"exit", os_exit},
{"getenv", os_getenv}, {"getenv", os_getenv},
{"remove", os_remove}, {"remove", os_remove},
{"rename", os_rename}, {"rename", os_rename},
{"setlocale", os_setlocale}, {"setlocale", os_setlocale},
{"time", os_time}, {"time", os_time},
{"tmpname", os_tmpname}, {"tmpname", os_tmpname},
{NULL, NULL} {NULL, NULL}
}; };
/* }====================================================== */ /* }====================================================== */
LUAMOD_API int luaopen_os(lua_State *L) { LUAMOD_API int luaopen_os (lua_State *L) {
luaL_newlib(L, syslib); luaL_newlib(L, syslib);
return 1; return 1;
} }

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.h,v 1.70 2012/05/08 13:53:33 roberto Exp $ ** $Id: lparser.h $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,78 +13,126 @@
/* /*
** Expression descriptor ** Expression and variable descriptor.
** Code generation for variables and expressions can be delayed to allow
** optimizations; An 'expdesc' structure describes a potentially-delayed
** variable/expression. It has a description of its "main" value plus a
** list of conditional jumps that can also produce its value (generated
** by short-circuit operators 'and'/'or').
*/ */
/* kinds of variables/expressions */
typedef enum { typedef enum {
VVOID, /* no value */ VVOID, /* when 'expdesc' describes the last expression of a list,
VNIL, this kind means an empty list (so, no expression) */
VTRUE, VNIL, /* constant nil */
VFALSE, VTRUE, /* constant true */
VK, /* info = index of constant in `k' */ VFALSE, /* constant false */
VKNUM, /* nval = numerical value */ VK, /* constant in 'k'; info = index of constant in 'k' */
VNONRELOC, /* info = result register */ VKFLT, /* floating constant; nval = numerical float value */
VLOCAL, /* info = local register */ VKINT, /* integer constant; ival = numerical integer value */
VUPVAL, /* info = index of upvalue in 'upvalues' */ VKSTR, /* string constant; strval = TString address;
VINDEXED, /* t = table register/upvalue; idx = index R/K */ (string is fixed by the lexer) */
VJMP, /* info = instruction pc */ VNONRELOC, /* expression has its value in a fixed register;
VRELOCABLE, /* info = instruction pc */ info = result register */
VCALL, /* info = instruction pc */ VLOCAL, /* local variable; var.ridx = register index;
VVARARG /* info = instruction pc */ var.vidx = relative index in 'actvar.arr' */
VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
VCONST, /* compile-time <const> variable;
info = absolute index in 'actvar.arr' */
VINDEXED, /* indexed variable;
ind.t = table register;
ind.idx = key's R index */
VINDEXUP, /* indexed upvalue;
ind.t = table upvalue;
ind.idx = key's K index */
VINDEXI, /* indexed variable with constant integer;
ind.t = table register;
ind.idx = key's value */
VINDEXSTR, /* indexed variable with literal string;
ind.t = table register;
ind.idx = key's K index */
VJMP, /* expression is a test/comparison;
info = pc of corresponding jump instruction */
VRELOC, /* expression can put result in any register;
info = instruction pc */
VCALL, /* expression is a function call; info = instruction pc */
VVARARG /* vararg expression; info = instruction pc */
} expkind; } expkind;
#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR)
#define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) #define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR)
typedef struct expdesc { typedef struct expdesc {
expkind k; expkind k;
union { union {
struct { /* for indexed variables (VINDEXED) */ lua_Integer ival; /* for VKINT */
short idx; /* index (R/K) */ lua_Number nval; /* for VKFLT */
lu_byte t; /* table (register or upvalue) */ TString *strval; /* for VKSTR */
lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ int info; /* for generic use */
} ind; struct { /* for indexed variables */
int info; /* for generic use */ short idx; /* index (R or "long" K) */
lua_Number nval; /* for VKNUM */ lu_byte t; /* table (register or upvalue) */
} u; } ind;
int t; /* patch list of `exit when true' */ struct { /* for local variables */
int f; /* patch list of `exit when false' */ lu_byte ridx; /* register holding the variable */
unsigned short vidx; /* compiler index (in 'actvar.arr') */
} var;
} u;
int t; /* patch list of 'exit when true' */
int f; /* patch list of 'exit when false' */
} expdesc; } expdesc;
/* description of active local variable */ /* kinds of variables */
typedef struct Vardesc { #define VDKREG 0 /* regular */
short idx; /* variable index in stack */ #define RDKCONST 1 /* constant */
#define RDKTOCLOSE 2 /* to-be-closed */
#define RDKCTC 3 /* compile-time constant */
/* description of an active local 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 */
lu_byte nactvar; /* local level where it appears in current block */ lu_byte nactvar; /* number of active variables in that position */
lu_byte close; /* goto that escapes upvalues */
} Labeldesc; } Labeldesc;
/* list of labels or gotos */ /* list of labels or gotos */
typedef struct Labellist { typedef struct Labellist {
Labeldesc *arr; /* array */ Labeldesc *arr; /* array */
int n; /* number of entries in use */ int n; /* number of entries in use */
int size; /* array size */ int size; /* array size */
} Labellist; } Labellist;
/* dynamic structures used by the parser */ /* dynamic structures used by the parser */
typedef struct Dyndata { typedef struct Dyndata {
struct { /* list of active local variables */ struct { /* list of all active local variables */
Vardesc *arr; Vardesc *arr;
int n; int n;
int size; int size;
} actvar; } actvar;
Labellist gt; /* list of pending gotos */ Labellist gt; /* list of pending gotos */
Labellist label; /* list of active labels */ Labellist label; /* list of active labels */
} Dyndata; } Dyndata;
@ -94,26 +142,30 @@ struct BlockCnt; /* defined in lparser.c */
/* state needed to generate code for a given function */ /* state needed to generate code for a given function */
typedef struct FuncState { typedef struct FuncState {
Proto *f; /* current function header */ Proto *f; /* current function header */
Table *h; /* table to find (and reuse) elements in `k' */ 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 */ 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) */
short nlocvars; /* number of elements in 'f->locvars' */ int firstlabel; /* index of first label (in 'dyd->label->arr') */
lu_byte nactvar; /* number of active local variables */ short ndebugvars; /* number of elements in 'f->locvars' */
lu_byte nups; /* number of upvalues */ lu_byte nactvar; /* number of active local variables */
lu_byte freereg; /* first free register */ lu_byte nups; /* number of upvalues */
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 Closure *luaY_parser(lua_State *L, ZIO *z, Mbuffer *buff, LUAI_FUNC int luaY_nvarstack (FuncState *fs);
Dyndata *dyd, const char *name, int firstchar); LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
Dyndata *dyd, const char *name, int firstchar);
#endif #endif

View file

@ -0,0 +1,45 @@
/*
** $Id: lprefix.h $
** Definitions for Lua code that must come before any other header file
** See Copyright Notice in lua.h
*/
#ifndef lprefix_h
#define lprefix_h
/*
** Allows POSIX/XSI stuff
*/
#if !defined(LUA_USE_C89) /* { */
#if !defined(_XOPEN_SOURCE)
#define _XOPEN_SOURCE 600
#elif _XOPEN_SOURCE == 0
#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */
#endif
/*
** Allows manipulation of large files in gcc and some other compilers
*/
#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS)
#define _LARGEFILE_SOURCE 1
#define _FILE_OFFSET_BITS 64
#endif
#endif /* } */
/*
** Windows stuff
*/
#if defined(_WIN32) /* { */
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
#endif
#endif /* } */
#endif

View file

@ -1,16 +1,18 @@
/* /*
** $Id: lstate.c,v 2.99 2012/10/02 17:40:53 roberto Exp $ ** $Id: lstate.c $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#define lstate_c
#define LUA_CORE
#include "lprefix.h"
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
#define lstate_c
#define LUA_CORE
#include "lua.h" #include "lua.h"
#include "lapi.h" #include "lapi.h"
@ -26,41 +28,13 @@
#include "ltm.h" #include "ltm.h"
#if !defined(LUAI_GCPAUSE)
#define LUAI_GCPAUSE 200 /* 200% */
#endif
#if !defined(LUAI_GCMAJOR)
#define LUAI_GCMAJOR 200 /* 200% */
#endif
#if !defined(LUAI_GCMUL)
#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
#endif
#define MEMERRMSG "not enough memory"
/*
** 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 ** thread state + extra space
*/ */
typedef struct LX { typedef struct LX {
#if defined(LUAI_EXTRASPACE) lu_byte extra_[LUA_EXTRASPACE];
char buff[LUAI_EXTRASPACE]; lua_State l;
#endif
lua_State l;
} LX; } LX;
@ -68,258 +42,404 @@ typedef struct LX {
** Main thread combines a thread state and the global state ** Main thread combines a thread state and the global state
*/ */
typedef struct LG { typedef struct LG {
LX l; LX l;
global_State g; global_State g;
} LG; } 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)))
/* /*
** Compute an initial seed as random as possible. In ANSI, rely on ** A macro to create a "random" seed when a state is created;
** Address Space Layout Randomization (if present) to increase ** the seed is used to randomize string hashes.
** randomness.. */
#if !defined(luai_makeseed)
#include <time.h>
/*
** Compute an initial seed with some level of randomness.
** Rely on Address Space Layout Randomization (if present) and
** current time.
*/ */
#define addbuff(b,p,e) \ #define addbuff(b,p,e) \
{ size_t t = cast(size_t, e); \ { size_t t = cast_sizet(e); \
memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
static unsigned int makeseed(lua_State *L) { static unsigned int luai_makeseed (lua_State *L) {
char buff[4 * sizeof(size_t)]; char buff[3 * sizeof(size_t)];
unsigned int h = luai_makeseed(); unsigned int h = cast_uint(time(NULL));
int p = 0; int p = 0;
addbuff(buff, p, L); /* heap variable */ addbuff(buff, p, L); /* heap variable */
addbuff(buff, p, &h); /* local variable */ addbuff(buff, p, &h); /* local variable */
addbuff(buff, p, luaO_nilobject); /* global variable */ addbuff(buff, p, &lua_newstate); /* public function */
addbuff(buff, p, &lua_newstate); /* public function */ lua_assert(p == sizeof(buff));
lua_assert(p == sizeof(buff)); return luaS_hash(buff, p, h);
return luaS_hash(buff, p, h);
} }
#endif
/* /*
** set GCdebt to a new value keeping the value (totalbytes + GCdebt) ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
** invariant ** invariant (and avoiding underflows in 'totalbytes')
*/ */
void luaE_setdebt(global_State *g, l_mem debt) { void luaE_setdebt (global_State *g, l_mem debt) {
g->totalbytes -= (debt - g->GCdebt); l_mem tb = gettotalbytes(g);
g->GCdebt = debt; lua_assert(tb > 0);
if (debt < tb - MAX_LMEM)
debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
g->totalbytes = tb - debt;
g->GCdebt = debt;
} }
CallInfo *luaE_extendCI(lua_State *L) { LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
CallInfo *ci = luaM_new(L, CallInfo); UNUSED(L); UNUSED(limit);
lua_assert(L->ci->next == NULL); return LUAI_MAXCCALLS; /* warning?? */
L->ci->next = ci;
ci->previous = L->ci;
ci->next = NULL;
return ci;
} }
void luaE_freeCI(lua_State *L) { CallInfo *luaE_extendCI (lua_State *L) {
CallInfo *ci = L->ci; CallInfo *ci;
CallInfo *next = ci->next; lua_assert(L->ci->next == NULL);
ci->next = NULL; ci = luaM_new(L, CallInfo);
while ((ci = next) != NULL) { lua_assert(L->ci->next == NULL);
next = ci->next; L->ci->next = ci;
luaM_free(L, ci); ci->previous = L->ci;
ci->next = NULL;
ci->u.l.trap = 0;
L->nci++;
return ci;
}
/*
** free all CallInfo structures not in use by a thread
*/
static void freeCI (lua_State *L) {
CallInfo *ci = L->ci;
CallInfo *next = ci->next;
ci->next = NULL;
while ((ci = next) != NULL) {
next = ci->next;
luaM_free(L, ci);
L->nci--;
}
}
/*
** free half of the CallInfo structures not in use by a thread,
** keeping the first one.
*/
void luaE_shrinkCI (lua_State *L) {
CallInfo *ci = L->ci->next; /* first free CallInfo */
CallInfo *next;
if (ci == NULL)
return; /* no extra elements */
while ((next = ci->next) != NULL) { /* two extra elements? */
CallInfo *next2 = next->next; /* next's next */
ci->next = next2; /* remove next from the list */
L->nci--;
luaM_free(L, next); /* free next */
if (next2 == NULL)
break; /* no more elements */
else {
next2->previous = ci;
ci = next2; /* continue */
} }
}
} }
static void stack_init(lua_State *L1, lua_State *L) { /*
int i; ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
CallInfo *ci; ** If equal, raises an overflow error. If value is larger than
/* initialize stack array */ ** LUAI_MAXCCALLS (which means it is handling an overflow) but
L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); ** not much larger, does not report an error (to allow overflow
L1->stacksize = BASIC_STACK_SIZE; ** handling to work).
for (i = 0; i < BASIC_STACK_SIZE; i++) */
setnilvalue(L1->stack + i); /* erase new stack */ void luaE_checkcstack (lua_State *L) {
L1->top = L1->stack; if (getCcalls(L) == LUAI_MAXCCALLS)
L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; luaG_runerror(L, "C stack overflow");
/* initialize first ci */ else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
ci = &L1->base_ci; luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
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) { LUAI_FUNC void luaE_incCstack (lua_State *L) {
if (L->stack == NULL) L->nCcalls++;
return; /* stack not completely built yet */ if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
L->ci = &L->base_ci; /* free the entire 'ci' list */ luaE_checkcstack(L);
luaE_freeCI(L); }
luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
static void stack_init (lua_State *L1, lua_State *L) {
int i; CallInfo *ci;
/* initialize stack array */
L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
L1->tbclist.p = L1->stack.p;
for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */
L1->top.p = L1->stack.p;
L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
/* initialize first ci */
ci = &L1->base_ci;
ci->next = ci->previous = NULL;
ci->callstatus = CIST_C;
ci->func.p = L1->top.p;
ci->u.c.k = NULL;
ci->nresults = 0;
setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */
L1->top.p++;
ci->top.p = L1->top.p + LUA_MINSTACK;
L1->ci = ci;
}
static void freestack (lua_State *L) {
if (L->stack.p == NULL)
return; /* stack not completely built yet */
L->ci = &L->base_ci; /* free the entire 'ci' list */
freeCI(L);
lua_assert(L->nci == 0);
luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */
} }
/* /*
** 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 mt; /* create registry */
/* create registry */ 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[LUA_RIDX_MAINTHREAD] = L */
/* registry[LUA_RIDX_MAINTHREAD] = L */ setthvalue(L, &registry->array[LUA_RIDX_MAINTHREAD - 1], L);
setthvalue(L, &mt, L); /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt); sethvalue(L, &registry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L));
/* registry[LUA_RIDX_GLOBALS] = table of globals */
sethvalue(L, &mt, luaH_new(L));
luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
} }
/* /*
** open parts of the state that may cause memory-allocation errors ** open parts of the state that may cause memory-allocation errors.
*/ */
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);
UNUSED(ud); UNUSED(ud);
stack_init(L, L); /* init stack */ stack_init(L, L); /* init stack */
init_registry(L, g); init_registry(L, g);
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaS_init(L);
luaT_init(L); luaT_init(L);
luaX_init(L); luaX_init(L);
/* pre-create memory-error message */ g->gcstp = 0; /* allow gc */
g->memerrmsg = luaS_newliteral(L, MEMERRMSG); setnilvalue(&g->nilvalue); /* now state is complete */
luaS_fix(g->memerrmsg); /* it should never be collected */ luai_userstateopen(L);
g->gcrunning = 1; /* allow gc */
g->version = lua_version(NULL);
luai_userstateopen(L);
} }
/* /*
** preinitialize a state with consistent values without allocating ** preinitialize a thread with consistent values without allocating
** any memory (to avoid errors) ** any memory (to avoid errors)
*/ */
static void preinit_state(lua_State *L, global_State *g) { static void preinit_thread (lua_State *L, global_State *g) {
G(L) = g; G(L) = g;
L->stack = NULL; L->stack.p = NULL;
L->ci = NULL; L->ci = NULL;
L->stacksize = 0; L->nci = 0;
L->errorJmp = NULL; L->twups = L; /* thread has no upvalues */
L->nCcalls = 0; L->nCcalls = 0;
L->hook = NULL; L->errorJmp = NULL;
L->hookmask = 0; L->hook = NULL;
L->basehookcount = 0; L->hookmask = 0;
L->allowhook = 1; L->basehookcount = 0;
resethookcount(L); L->allowhook = 1;
L->openupval = NULL; resethookcount(L);
L->nny = 1; L->openupval = NULL;
L->status = LUA_OK; L->status = LUA_OK;
L->errfunc = 0; L->errfunc = 0;
L->oldpc = 0;
} }
static void close_state(lua_State *L) { static void close_state (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
luaF_close(L, L->stack); /* close all upvalues for this thread */ if (!completestate(g)) /* closing a partially built state? */
luaC_freeallobjects(L); /* just collect its objects */
else { /* closing a fully built state */
L->ci = &L->base_ci; /* unwind CallInfo list */
luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */
luaC_freeallobjects(L); /* collect all objects */ luaC_freeallobjects(L); /* collect all objects */
if (g->version) /* closing a fully built state? */ luai_userstateclose(L);
luai_userstateclose(L); }
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); freestack(L);
luaZ_freebuffer(L, &g->buff); lua_assert(gettotalbytes(g) == sizeof(LG));
freestack(L); (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
lua_assert(gettotalbytes(g) == sizeof(LG));
(*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) {
lua_State *L1; global_State *g = G(L);
lua_lock(L); GCObject *o;
luaC_checkGC(L); lua_State *L1;
L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th; lua_lock(L);
setthvalue(L, L->top, L1); luaC_checkGC(L);
api_incr_top(L); /* create new thread */
preinit_state(L1, G(L)); o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
L1->hookmask = L->hookmask; L1 = gco2th(o);
L1->basehookcount = L->basehookcount; /* anchor it on L stack */
L1->hook = L->hook; setthvalue2s(L, L->top.p, L1);
resethookcount(L1); api_incr_top(L);
luai_userstatethread(L, L1); preinit_thread(L1, g);
stack_init(L1, L); /* init stack */ L1->hookmask = L->hookmask;
lua_unlock(L); L1->basehookcount = L->basehookcount;
return L1; L1->hook = L->hook;
resethookcount(L1);
/* initialize L1 extra space */
memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
LUA_EXTRASPACE);
luai_userstatethread(L, L1);
stack_init(L1, L); /* init stack */
lua_unlock(L);
return L1;
} }
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_close(L1, L1->stack); /* close all upvalues for this thread */ luaF_closeupval(L1, L1->stack.p); /* close all upvalues */
lua_assert(L1->openupval == NULL); lua_assert(L1->openupval == NULL);
luai_userstatefree(L, L1); luai_userstatefree(L, L1);
freestack(L1); freestack(L1);
luaM_free(L, l); luaM_free(L, l);
} }
LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) { int luaE_resetthread (lua_State *L, int status) {
int i; CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */
lua_State *L; setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */
global_State *g; ci->func.p = L->stack.p;
LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); ci->callstatus = CIST_C;
if (l == NULL) return NULL; if (status == LUA_YIELD)
L = &l->l.l; status = LUA_OK;
g = &l->g; L->status = LUA_OK; /* so it can run __close metamethods */
L->next = NULL; status = luaD_closeprotected(L, 1, status);
L->tt = LUA_TTHREAD; if (status != LUA_OK) /* errors? */
g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); luaD_seterrorobj(L, status, L->stack.p + 1);
L->marked = luaC_white(g); else
g->gckind = KGC_NORMAL; L->top.p = L->stack.p + 1;
preinit_state(L, g); ci->top.p = L->top.p + LUA_MINSTACK;
g->frealloc = f; luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0);
g->ud = ud; return status;
g->mainthread = L;
g->seed = makeseed(L);
g->uvhead.u.l.prev = &g->uvhead;
g->uvhead.u.l.next = &g->uvhead;
g->gcrunning = 0; /* no GC while building state */
g->GCestimate = 0;
g->strt.size = 0;
g->strt.nuse = 0;
g->strt.hash = NULL;
setnilvalue(&g->l_registry);
luaZ_initbuffer(L, &g->buff);
g->panic = NULL;
g->version = NULL;
g->gcstate = GCSpause;
g->allgc = NULL;
g->finobj = NULL;
g->tobefnz = NULL;
g->sweepgc = g->sweepfin = NULL;
g->gray = g->grayagain = NULL;
g->weak = g->ephemeron = g->allweak = NULL;
g->totalbytes = sizeof(LG);
g->GCdebt = 0;
g->gcpause = LUAI_GCPAUSE;
g->gcmajorinc = LUAI_GCMAJOR;
g->gcstepmul = LUAI_GCMUL;
for (i = 0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
/* memory allocation error: free partial state */
close_state(L);
L = NULL;
}
return L;
} }
LUA_API void lua_close(lua_State *L) { LUA_API int lua_closethread (lua_State *L, lua_State *from) {
L = G(L)->mainthread; /* only the main thread can be closed */ int status;
lua_lock(L); lua_lock(L);
L->nCcalls = (from) ? getCcalls(from) : 0;
status = luaE_resetthread(L, L->status);
lua_unlock(L);
return status;
}
/*
** Deprecated! Use 'lua_closethread' instead.
*/
LUA_API int lua_resetthread (lua_State *L) {
return lua_closethread(L, NULL);
}
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
int i;
lua_State *L;
global_State *g;
LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
if (l == NULL) return NULL;
L = &l->l.l;
g = &l->g;
L->tt = LUA_VTHREAD;
g->currentwhite = bitmask(WHITE0BIT);
L->marked = luaC_white(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->ud = ud;
g->warnf = NULL;
g->ud_warn = NULL;
g->mainthread = L;
g->seed = luai_makeseed(L);
g->gcstp = GCSTPGC; /* no GC while building state */
g->strt.size = g->strt.nuse = 0;
g->strt.hash = NULL;
setnilvalue(&g->l_registry);
g->panic = NULL;
g->gcstate = GCSpause;
g->gckind = KGC_INC;
g->gcstopem = 0;
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->gray = g->grayagain = NULL;
g->weak = g->ephemeron = g->allweak = NULL;
g->twups = NULL;
g->totalbytes = sizeof(LG);
g->GCdebt = 0;
g->lastatomic = 0;
setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
setgcparam(g->gcpause, LUAI_GCPAUSE);
setgcparam(g->gcstepmul, LUAI_GCMUL);
g->gcstepsize = LUAI_GCSTEPSIZE;
setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
g->genminormul = LUAI_GENMINORMUL;
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
/* memory allocation error: free partial state */
close_state(L); close_state(L);
L = NULL;
}
return L;
} }
LUA_API void lua_close (lua_State *L) {
lua_lock(L);
L = G(L)->mainthread; /* only the main thread can be closed */
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);
}

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 2.82 2012/07/02 13:37:04 roberto Exp $ ** $Id: lstate.h $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -9,219 +9,399 @@
#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. ** belong to one (and only one) of these lists, using field 'next' of
** the 'CommonHeader' for the link:
** **
** Lua keeps most objects linked in list g->allgc. The link uses field ** 'allgc': all objects not marked for finalization;
** 'next' of the CommonHeader. ** 'finobj': all objects marked for finalization;
** 'tobefnz': all objects ready to be finalized;
** 'fixedgc': all objects that are not to be collected (currently
** only small strings, such as reserved words).
** **
** Strings are kept in several lists headed by the array g->strt.hash. ** 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.
** **
** Open upvalues are not subject to independent garbage collection. They ** 'allgc' -> 'survival': new objects;
** are collected together with their respective threads. Lua keeps a ** 'survival' -> 'old': objects that survived one collection;
** double-linked list with all open upvalues (g->uvhead) so that it can ** 'old1' -> 'reallyold': objects that became old in last collection;
** mark objects referred by them. (They are always gray, so they must ** 'reallyold' -> NULL: objects old for more than one cycle.
** be remarked in the atomic step. Usually their contents would be marked
** when traversing the respective threads, but the thread may already be
** dead, while the upvalue is still accessible through closures.)
** **
** Objects with finalizers are kept in the list g->finobj. ** 'finobj' -> 'finobjsur': new objects marked for finalization;
** 'finobjsur' -> 'finobjold1': survived """";
** 'finobjold1' -> 'finobjrold': just old """";
** 'finobjrold' -> NULL: really old """".
** **
** The list g->tobefnz links all objects being finalized. ** 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.
** These lists are linked by fields 'gclist'. (All objects that
** 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
** must belong to one of these lists, and all objects in these lists
** must be gray (with two exceptions explained below):
**
** 'gray': regular gray objects, still waiting to be visited.
** 'grayagain': objects that must be revisited at the atomic phase.
** That includes
** - black objects got in a write barrier;
** - all kinds of weak tables during propagation phase;
** - all threads.
** 'weak': tables with weak values to be cleared;
** 'ephemeron': ephemeron tables with white->white entries;
** 'allweak': tables with weak keys and/or weak values to be cleared.
**
** 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 upvales 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 */
/*
** Atomic type (relative to signals) to better ensure that 'lua_sethook'
** is thread safe
*/
#if !defined(l_signalT)
#include <signal.h>
#define l_signalT sig_atomic_t
#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
#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_NORMAL 0 #define KGC_INC 0 /* incremental gc */
#define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ #define KGC_GEN 1 /* generational gc */
#define KGC_GEN 2 /* generational collection */
typedef struct stringtable { typedef struct stringtable {
GCObject **hash; TString **hash;
lu_int32 nuse; /* number of elements */ int nuse; /* number of elements */
int size; int size;
} stringtable; } stringtable;
/* /*
** information about a call ** Information about a call.
** About union 'u':
** - field 'l' is used only for Lua functions;
** - field 'c' is used only for C functions.
** About union 'u2':
** - field 'funcidx' is used only by C functions while doing a
** protected call;
** - 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;
** - field 'transferinfo' is used only during call/returnhooks,
** before the function starts or after it ends.
*/ */
typedef struct CallInfo { struct CallInfo {
StkId func; /* function index in the stack */ StkIdRel func; /* function index in the stack */
StkId top; /* top for this function */ StkIdRel top; /* top for this function */
struct CallInfo *previous, *next; /* dynamic call link */ struct CallInfo *previous, *next; /* dynamic call link */
short nresults; /* expected number of results from this function */ union {
lu_byte callstatus; struct { /* only for Lua functions */
ptrdiff_t extra; const Instruction *savedpc;
union { volatile l_signalT trap; /* function is tracing lines/counts */
struct { /* only for Lua functions */ int nextraargs; /* # of extra arguments in vararg functions */
StkId base; /* base for this function */ } l;
const Instruction *savedpc; struct { /* only for C functions */
} l; lua_KFunction k; /* continuation in case of yields */
struct { /* only for C functions */ ptrdiff_t old_errfunc;
int ctx; /* context info. in case of yields */ lua_KContext ctx; /* context info. in case of yields */
lua_CFunction k; /* continuation in case of yields */ } c;
ptrdiff_t old_errfunc; } u;
lu_byte old_allowhook; union {
lu_byte status; int funcidx; /* called-function index */
} c; int nyield; /* number of values yielded */
} u; int nres; /* number of values returned */
} CallInfo; struct { /* info about transferred values (for call/return hooks) */
unsigned short ftransfer; /* offset of first value transferred */
unsigned short ntransfer; /* number of values transferred */
} transferinfo;
} u2;
short nresults; /* expected number of results from this function */
unsigned short callstatus;
};
/* /*
** Bits in CallInfo status ** Bits in CallInfo status
*/ */
#define CIST_LUA (1<<0) /* call is running a Lua function */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */
#define CIST_HOOKED (1<<1) /* call is running a debug hook */ #define CIST_C (1<<1) /* call is running a C function */
#define CIST_REENTRY (1<<2) /* call is running on same invocation of #define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */
luaV_execute of previous call */ #define CIST_HOOKED (1<<3) /* call is running a debug hook */
#define CIST_YIELDED (1<<3) /* call reentered after suspension */ #define CIST_YPCALL (1<<4) /* doing a yieldable protected call */
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ #define CIST_TAIL (1<<5) /* call was tail called */
#define CIST_STAT (1<<5) /* call has an error status (pcall) */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
#define CIST_TAIL (1<<6) /* call was tail called */ #define CIST_FIN (1<<7) /* function "called" a finalizer */
#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ #define CIST_TRAN (1<<8) /* 'ci' has transfer information */
#define CIST_CLSRET (1<<9) /* function is closing tbc variables */
/* Bits 10-12 are used for CIST_RECST (see below) */
#define isLua(ci) ((ci)->callstatus & CIST_LUA) #define CIST_RECST 10
#if defined(LUA_COMPAT_LT_LE)
#define CIST_LEQ (1<<13) /* using __lt for __le */
#endif
/* /*
** `global state', shared by all threads of this state ** 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 & ~(7 << CIST_RECST)) \
| ((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)))
/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
#define getoah(st) ((st) & CIST_OAH)
/*
** 'global state', shared by all threads of this state
*/ */
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' */
lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
lu_mem GCmemtrav; /* memory traversed by the GC */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */
stringtable strt; /* hash table for strings */ stringtable strt; /* hash table for strings */
TValue l_registry; TValue l_registry;
unsigned int seed; /* randomized seed for hashes */ TValue nilvalue; /* a nil value */
lu_byte currentwhite; unsigned int seed; /* randomized seed for hashes */
lu_byte gcstate; /* state of garbage collector */ lu_byte currentwhite;
lu_byte gckind; /* kind of GC running */ lu_byte gcstate; /* state of garbage collector */
lu_byte gcrunning; /* true if GC is running */ lu_byte gckind; /* kind of GC running */
int sweepstrgc; /* position of sweep in `strt' */ lu_byte gcstopem; /* stops emergency collections */
GCObject *allgc; /* list of all collectable objects */ lu_byte genminormul; /* control for minor generational collections */
GCObject *finobj; /* list of collectable objects with finalizers */ lu_byte genmajormul; /* control for major generational collections */
GCObject **sweepgc; /* current position of sweep in list 'allgc' */ lu_byte gcstp; /* control whether GC is running */
GCObject **sweepfin; /* current position of sweep in list 'finobj' */ lu_byte gcemergency; /* true if this is an emergency collection */
GCObject *gray; /* list of gray objects */ lu_byte gcpause; /* size of pause between successive GCs */
GCObject *grayagain; /* list of objects to be traversed atomically */ lu_byte gcstepmul; /* GC "speed" */
GCObject *weak; /* list of tables with weak values */ lu_byte gcstepsize; /* (log2 of) GC granularity */
GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ GCObject *allgc; /* list of all collectable objects */
GCObject *allweak; /* list of all-weak tables */ GCObject **sweepgc; /* current position of sweep in list */
GCObject *tobefnz; /* list of userdata to be GC */ GCObject *finobj; /* list of collectable objects with finalizers */
UpVal uvhead; /* head of double-linked list of all open upvalues */ GCObject *gray; /* list of gray objects */
Mbuffer buff; /* temporary buffer for string concatenation */ GCObject *grayagain; /* list of objects to be traversed atomically */
int gcpause; /* size of pause between successive GCs */ GCObject *weak; /* list of tables with weak values */
int gcmajorinc; /* pause between major collections (only in gen. mode) */ GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
int gcstepmul; /* GC `granularity' */ GCObject *allweak; /* list of all-weak tables */
lua_CFunction panic; /* to be called in unprotected errors */ GCObject *tobefnz; /* list of userdata to be GC */
struct lua_State *mainthread; GCObject *fixedgc; /* list of objects not to be collected */
const lua_Number *version; /* pointer to version number */ /* fields for generational collector */
TString *memerrmsg; /* memory-error message */ GCObject *survival; /* start of objects that survived one GC cycle */
TString *tmname[TM_N]; /* array with tag-method names */ GCObject *old1; /* start of old1 objects */
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 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 */
lua_CFunction panic; /* to be called in unprotected errors */
struct lua_State *mainthread;
TString *memerrmsg; /* message for memory-allocation errors */
TString *tmname[TM_N]; /* array with tag-method names */
struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
lua_WarnFunction warnf; /* warning function */
void *ud_warn; /* auxiliary data to 'warnf' */
} global_State; } global_State;
/* /*
** `per thread' state ** 'per thread' state
*/ */
struct lua_State { struct lua_State {
CommonHeader; CommonHeader;
lu_byte status; lu_byte status;
StkId top; /* first free slot in the stack */ lu_byte allowhook;
global_State *l_G; unsigned short nci; /* number of items in 'ci' list */
CallInfo *ci; /* call info for current function */ StkIdRel top; /* first free slot in the stack */
const Instruction *oldpc; /* last pc traced */ global_State *l_G;
StkId stack_last; /* last free slot in the stack */ CallInfo *ci; /* call info for current function */
StkId stack; /* stack base */ StkIdRel stack_last; /* end of stack (last element + 1) */
int stacksize; StkIdRel stack; /* stack base */
unsigned short nny; /* number of non-yieldable calls in stack */ UpVal *openupval; /* list of open upvalues in this stack */
unsigned short nCcalls; /* number of nested C calls */ StkIdRel tbclist; /* list of to-be-closed variables */
lu_byte hookmask; GCObject *gclist;
lu_byte allowhook; struct lua_State *twups; /* list of threads with open upvalues */
int basehookcount; struct lua_longjmp *errorJmp; /* current error recover point */
int hookcount; CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
lua_Hook hook; volatile lua_Hook hook;
GCObject *openupval; /* list of open upvalues in this stack */ ptrdiff_t errfunc; /* current error handling function (stack index) */
GCObject *gclist; l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */
struct lua_longjmp *errorJmp; /* current error recover point */ int oldpc; /* last pc traced */
ptrdiff_t errfunc; /* current error handling function (stack index) */ int basehookcount;
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ int hookcount;
volatile l_signalT hookmask;
}; };
#define G(L) (L->l_G) #define G(L) (L->l_G)
/*
** 'g->nilvalue' being a nil value flags that the state was completely
** build.
*/
#define completestate(g) ttisnil(&g->nilvalue)
/* /*
** Union of all collectable objects ** 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 GCObject { union GCUnion {
GCheader gch; /* common header */ GCObject gc; /* common header */
union TString ts; struct TString ts;
union Udata u; struct Udata u;
union Closure cl; union Closure cl;
struct Table h; struct Table h;
struct Proto p; struct Proto p;
struct UpVal uv; struct lua_State th; /* thread */
struct lua_State th; /* thread */ struct UpVal upv;
}; };
#define gch(o) (&(o)->gch) /*
** 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))
/* macros to convert a GCObject into a specific value */ /* macros to convert a GCObject into a specific value */
#define rawgco2ts(o) \ #define gco2ts(o) \
check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
#define gco2ts(o) (&rawgco2ts(o)->tsv) #define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u))
#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) #define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l))
#define gco2u(o) (&rawgco2u(o)->uv) #define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c))
#define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l))
#define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c))
#define gco2cl(o) \ #define gco2cl(o) \
check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
#define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) #define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h))
#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) #define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p))
#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) #define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th))
#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) #define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv))
/* macro to convert any Lua object into a GCObject */
#define obj2gco(v) (cast(GCObject *, (v))) /*
** macro to convert a Lua object into a GCObject
** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.)
*/
#define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc))
/* actual number of total bytes allocated */ /* actual number of total bytes allocated */
#define gettotalbytes(g) ((g)->totalbytes + (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 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_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 int luaE_resetthread (lua_State *L, int status);
#endif #endif

View file

@ -1,17 +1,21 @@
/* /*
** $Id: lstring.c,v 2.26 2013/01/08 13:50:10 roberto Exp $ ** $Id: lstring.c $
** 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
*/ */
#include <string.h>
#define lstring_c #define lstring_c
#define LUA_CORE #define LUA_CORE
#include "lprefix.h"
#include <string.h>
#include "lua.h" #include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h" #include "lstate.h"
@ -19,167 +23,252 @@
/* /*
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to ** Maximum size for string table.
** compute its hash
*/ */
#if !defined(LUAI_HASHLIMIT) #define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*))
#define LUAI_HASHLIMIT 5
#endif
/* /*
** equality for long strings ** equality for long strings
*/ */
int luaS_eqlngstr(TString *a, TString *b) { int luaS_eqlngstr (TString *a, TString *b) {
size_t len = a->tsv.len; size_t len = a->u.lnglen;
lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR); lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
return (a == b) || /* same instance or... */ return (a == b) || /* same instance or... */
((len == b->tsv.len) && /* equal length and ... */ ((len == b->u.lnglen) && /* equal length and ... */
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ (memcmp(getlngstr(a), getlngstr(b), len) == 0)); /* equal contents */
}
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
unsigned int h = seed ^ cast_uint(l);
for (; l > 0; l--)
h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
return h;
}
unsigned int luaS_hashlongstr (TString *ts) {
lua_assert(ts->tt == LUA_VLNGSTR);
if (ts->extra == 0) { /* no hash? */
size_t len = ts->u.lnglen;
ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
ts->extra = 1; /* now it has its hash */
}
return ts->hash;
}
static void tablerehash (TString **vect, int osize, int nsize) {
int i;
for (i = osize; i < nsize; i++) /* clear new elements */
vect[i] = NULL;
for (i = 0; i < osize; i++) { /* rehash old part of the array */
TString *p = vect[i];
vect[i] = NULL;
while (p) { /* for each string in the list */
TString *hnext = p->u.hnext; /* save next */
unsigned int h = lmod(p->hash, nsize); /* new position */
p->u.hnext = vect[h]; /* chain it into array */
vect[h] = p;
p = hnext;
}
}
} }
/* /*
** equality for strings ** Resize the string table. If allocation fails, keep the current size.
** (This can degrade performance, but any non-zero size should work
** correctly.)
*/ */
int luaS_eqstr(TString *a, TString *b) { void luaS_resize (lua_State *L, int nsize) {
return (a->tsv.tt == b->tsv.tt) && stringtable *tb = &G(L)->strt;
(a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b)); int osize = tb->size;
} TString **newvect;
if (nsize < osize) /* shrinking table? */
tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed) { newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
unsigned int h = seed ^ cast(unsigned int, l); if (l_unlikely(newvect == NULL)) { /* reallocation failed? */
size_t l1; if (nsize < osize) /* was it shrinking table? */
size_t step = (l >> LUAI_HASHLIMIT) + 1; tablerehash(tb->hash, nsize, osize); /* restore to original size */
for (l1 = l; l1 >= step; l1 -= step) /* leave table as it was */
h = h ^ ((h << 5) + (h >> 2) + cast_byte(str[l1 - 1])); }
return h; else { /* allocation succeeded */
tb->hash = newvect;
tb->size = nsize;
if (nsize > osize)
tablerehash(newvect, osize, nsize); /* rehash for new size */
}
} }
/* /*
** resizes the string table ** Clear API string cache. (Entries cannot be empty, so fill them with
** a non-collectable string.)
*/ */
void luaS_resize(lua_State *L, int newsize) { void luaS_clearcache (global_State *g) {
int i; int i, j;
stringtable *tb = &G(L)->strt; for (i = 0; i < STRCACHE_N; i++)
/* cannot resize while GC is traversing strings */ for (j = 0; j < STRCACHE_M; j++) {
luaC_runtilstate(L, ~bitmask(GCSsweepstring)); if (iswhite(g->strcache[i][j])) /* will entry be collected? */
if (newsize > tb->size) { g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;
} }
/* rehash */
for (i = 0; i < tb->size; i++) {
GCObject *p = tb->hash[i];
tb->hash[i] = NULL;
while (p) { /* for each node in the list */
GCObject *next = gch(p)->next; /* save next */
unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */
gch(p)->next = tb->hash[h]; /* chain it */
tb->hash[h] = p;
resetoldbit(p); /* see MOVE OLD rule */
p = next;
}
}
if (newsize < tb->size) {
/* shrinking slice must be empty */
lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
}
tb->size = newsize;
} }
/*
** Initialize the string table and the string cache
*/
void luaS_init (lua_State *L) {
global_State *g = G(L);
int i, j;
stringtable *tb = &G(L)->strt;
tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */
tb->size = MINSTRTABSIZE;
/* pre-create memory-error message */
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
for (j = 0; j < STRCACHE_M; j++)
g->strcache[i][j] = g->memerrmsg;
}
/* /*
** creates a new string object ** creates a new string object
*/ */
static TString *createstrobj(lua_State *L, const char *str, size_t l, static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
int tag, unsigned int h, GCObject **list) { TString *ts;
TString *ts; GCObject *o;
size_t totalsize; /* total size of TString object */ size_t totalsize; /* total size of TString object */
totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); totalsize = sizelstring(l);
ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; o = luaC_newobj(L, tag, totalsize);
ts->tsv.len = l; ts = gco2ts(o);
ts->tsv.hash = h; ts->hash = h;
ts->tsv.extra = 0; ts->extra = 0;
memcpy(ts + 1, str, l * sizeof(char)); getstr(ts)[l] = '\0'; /* ending 0 */
((char *)(ts + 1))[l] = '\0'; /* ending 0 */ return ts;
return ts; }
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
ts->u.lnglen = l;
ts->shrlen = 0xFF; /* signals that it is a long string */
return ts;
}
void luaS_remove (lua_State *L, TString *ts) {
stringtable *tb = &G(L)->strt;
TString **p = &tb->hash[lmod(ts->hash, tb->size)];
while (*p != ts) /* find previous element */
p = &(*p)->u.hnext;
*p = (*p)->u.hnext; /* remove element from its list */
tb->nuse--;
}
static void growstrtab (lua_State *L, stringtable *tb) {
if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
luaC_fullgc(L, 1); /* try to free some... */
if (tb->nuse == MAX_INT) /* 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);
} }
/* /*
** creates a new short string, inserting it into string table ** Checks whether short string exists and reuses it or creates a new one.
*/ */
static TString *newshrstr(lua_State *L, const char *str, size_t l, static TString *internshrstr (lua_State *L, const char *str, size_t l) {
unsigned int h) { TString *ts;
GCObject **list; /* (pointer to) list where it will be inserted */ global_State *g = G(L);
stringtable *tb = &G(L)->strt; stringtable *tb = &g->strt;
TString *s; unsigned int h = luaS_hash(str, l, g->seed);
if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT / 2) TString **list = &tb->hash[lmod(h, tb->size)];
luaS_resize(L, tb->size * 2); /* too crowded */ lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
list = &tb->hash[lmod(h, tb->size)]; for (ts = *list; ts != NULL; ts = ts->u.hnext) {
s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); if (l == ts->shrlen && (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
tb->nuse++; /* found! */
return s; if (isdead(g, ts)) /* dead (but not collected yet)? */
} changewhite(ts); /* resurrect it */
return ts;
/*
** 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) {
GCObject *o;
global_State *g = G(L);
unsigned int h = luaS_hash(str, l, g->seed);
for (o = g->strt.hash[lmod(h, g->strt.size)];
o != NULL;
o = gch(o)->next) {
TString *ts = rawgco2ts(o);
if (h == ts->tsv.hash &&
l == ts->tsv.len &&
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
changewhite(o); /* resurrect it */
return ts;
}
} }
return newshrstr(L, str, l, h); /* not found; create a new string */ }
/* else must create a new string */
if (tb->nuse >= tb->size) { /* need to grow string table? */
growstrtab(L, tb);
list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
}
ts = createstrobj(L, l, LUA_VSHRSTR, h);
ts->shrlen = cast_byte(l);
memcpy(getshrstr(ts), str, l * sizeof(char));
ts->u.hnext = *list;
*list = ts;
tb->nuse++;
return ts;
} }
/* /*
** new string (with explicit length) ** new string (with explicit length)
*/ */
TString *luaS_newlstr(lua_State *L, const char *str, size_t l) { TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
if (l <= LUAI_MAXSHORTLEN) /* short string? */ if (l <= LUAI_MAXSHORTLEN) /* short string? */
return internshrstr(L, str, l); return internshrstr(L, str, l);
else { else {
if (l + 1 > (MAX_SIZET - sizeof(TString)) / sizeof(char)) TString *ts;
luaM_toobig(L); if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); luaM_toobig(L);
} ts = luaS_createlngstrobj(L, l);
memcpy(getlngstr(ts), str, l * sizeof(char));
return ts;
}
} }
/* /*
** new zero-terminated string ** Create or reuse a zero-terminated string, first checking in the
** cache (using the string address as a key). The cache can contain
** only zero-terminated strings, so it is safe to use 'strcmp' to
** check hits.
*/ */
TString *luaS_new(lua_State *L, const char *str) { TString *luaS_new (lua_State *L, const char *str) {
return luaS_newlstr(L, str, strlen(str)); unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
int j;
TString **p = G(L)->strcache[i];
for (j = 0; j < STRCACHE_M; j++) {
if (strcmp(str, getstr(p[j])) == 0) /* hit? */
return p[j]; /* that is it */
}
/* normal route */
for (j = STRCACHE_M - 1; j > 0; j--)
p[j] = p[j - 1]; /* move out last element */
/* new element is first in the list */
p[0] = luaS_newlstr(L, str, strlen(str));
return p[0];
} }
Udata *luaS_newudata(lua_State *L, size_t s, Table *e) { Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
Udata *u; Udata *u;
if (s > MAX_SIZET - sizeof(Udata)) int i;
luaM_toobig(L); GCObject *o;
u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
u->uv.len = s; luaM_toobig(L);
u->uv.metatable = NULL; o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
u->uv.env = e; u = gco2u(o);
return u; u->len = s;
u->nuvalue = nuvalue;
u->metatable = NULL;
for (i = 0; i < nuvalue; i++)
setnilvalue(&u->uv[i].uv);
return u;
} }

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.h,v 1.49 2012/02/01 21:57:15 roberto Exp $ ** $Id: lstring.h $
** 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,35 +12,46 @@
#include "lstate.h" #include "lstate.h"
#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) /*
** Memory-allocation error message must be preallocated (it cannot
** be created after memory is exhausted)
*/
#define MEMERRMSG "not enough memory"
#define sizeudata(u) (sizeof(union Udata)+(u)->len)
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ /*
(sizeof(s)/sizeof(char))-1)) ** Size of a TString: Size of the header plus space for the string
** itself (including final '\0').
*/
#define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char))
#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
(sizeof(s)/sizeof(char))-1))
/* /*
** test whether a string is a reserved word ** test whether a string is a reserved word
*/ */
#define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) #define isreserved(s) ((s)->tt == LUA_VSHRSTR && (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)->tsv.tt == LUA_TSHRSTR, (a) == (b)) #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b))
LUAI_FUNC unsigned int luaS_hash(const char *str, size_t l, unsigned int seed); LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
LUAI_FUNC int luaS_eqlngstr(TString *a, TString *b); LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts);
LUAI_FUNC int luaS_eqstr(TString *a, TString *b); 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 Udata *luaS_newudata(lua_State *L, size_t s, Table *e); LUAI_FUNC void luaS_clearcache (global_State *g);
LUAI_FUNC TString *luaS_newlstr(lua_State *L, const char *str, size_t l); LUAI_FUNC void luaS_init (lua_State *L);
LUAI_FUNC TString *luaS_new(lua_State *L, const char *str); LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue);
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_createlngstrobj (lua_State *L, size_t l);
#endif #endif

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.h,v 2.16 2011/08/17 20:26:47 roberto Exp $ ** $Id: ltable.h $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -10,35 +10,53 @@
#include "lobject.h" #include "lobject.h"
#define gnode(t,i) (&(t)->node[i]) #define gnode(t,i) (&(t)->node[i])
#define gkey(n) (&(n)->i_key.tvk) #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)
#define invalidateTMcache(t) ((t)->flags = 0)
/* returns the key, given the value of a table entry */
#define keyfromval(v) \
(gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))
LUAI_FUNC const TValue *luaH_getint(Table *t, int key); /*
LUAI_FUNC void luaH_setint(lua_State *L, Table *t, int key, TValue *value); ** Clear all bits of fast-access metamethods, which means that the table
LUAI_FUNC const TValue *luaH_getstr(Table *t, TString *key); ** may have any of these metamethods. (First access that fails after the
LUAI_FUNC const TValue *luaH_get(Table *t, const TValue *key); ** clearing will set the bit again.)
LUAI_FUNC TValue *luaH_newkey(lua_State *L, Table *t, const TValue *key); */
LUAI_FUNC TValue *luaH_set(lua_State *L, Table *t, const TValue *key); #define invalidateTMcache(t) ((t)->flags &= ~maskflags)
LUAI_FUNC Table *luaH_new(lua_State *L);
LUAI_FUNC void luaH_resize(lua_State *L, Table *t, int nasize, int nhsize);
LUAI_FUNC void luaH_resizearray(lua_State *L, Table *t, int nasize); /* true when 't' is using 'dummynode' as its hash part */
LUAI_FUNC void luaH_free(lua_State *L, Table *t); #define isdummy(t) ((t)->lastfree == NULL)
LUAI_FUNC int luaH_next(lua_State *L, Table *t, StkId key);
LUAI_FUNC int luaH_getn(Table *t);
/* allocated size for hash nodes */
#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t))
/* returns the Node, given the value of a table entry */
#define nodefromval(v) cast(Node *, (v))
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
TValue *value);
LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key);
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_set (lua_State *L, Table *t, const TValue *key,
TValue *value);
LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key,
const TValue *slot, TValue *value);
LUAI_FUNC Table *luaH_new (lua_State *L);
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
unsigned int nhsize);
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize);
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
LUAI_FUNC lua_Unsigned luaH_getn (Table *t);
LUAI_FUNC unsigned int luaH_realasize (const 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(Node *n);
#endif #endif

View file

@ -1,112 +1,174 @@
/* /*
** $Id: ltablib.c,v 1.65 2013/03/07 18:17:24 roberto Exp $ ** $Id: ltablib.c $
** Library for Table Manipulation ** Library for Table Manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <limits.h>
#include <stddef.h>
#define ltablib_c #define ltablib_c
#define LUA_LIB #define LUA_LIB
#include "lprefix.h"
#include <limits.h>
#include <stddef.h>
#include <string.h>
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lualib.h" #include "lualib.h"
#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) /*
** Operations that an object must define to mimic a table
** (some functions only need some of them)
*/
#define TAB_R 1 /* read */
#define TAB_W 2 /* write */
#define TAB_L 4 /* length */
#define TAB_RW (TAB_R | TAB_W) /* read/write */
#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
#if defined(LUA_COMPAT_MAXN)
static int maxn(lua_State *L) {
lua_Number max = 0;
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushnil(L); /* first key */
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;
}
#endif
static int tinsert(lua_State *L) { static int checkfield (lua_State *L, const char *key, int n) {
int e = aux_getn(L, 1) + 1; /* first empty element */ lua_pushstring(L, key);
int pos; /* where to insert new element */ return (lua_rawget(L, -n) != LUA_TNIL);
switch (lua_gettop(L)) {
case 2: { /* called with only 2 arguments */
pos = e; /* insert new element at the end */
break;
}
case 3: {
int i;
pos = luaL_checkint(L, 2); /* 2nd argument is the position */
luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
for (i = e; i > pos; i--) { /* move up elements */
lua_rawgeti(L, 1, i - 1);
lua_rawseti(L, 1, i); /* t[i] = t[i-1] */
}
break;
}
default: {
return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
}
}
lua_rawseti(L, 1, pos); /* t[pos] = v */
return 0;
} }
static int tremove(lua_State *L) { /*
int size = aux_getn(L, 1); ** Check that 'arg' either is a table or can behave like one (that is,
int pos = luaL_optint(L, 2, size); ** has a metatable with the required metamethods)
if (pos != size) /* validate 'pos' if given */ */
luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); static void checktab (lua_State *L, int arg, int what) {
lua_rawgeti(L, 1, pos); /* result = t[pos] */ if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */
for (; pos < size; pos++) { int n = 1; /* number of elements to pop */
lua_rawgeti(L, 1, pos + 1); if (lua_getmetatable(L, arg) && /* must have metatable */
lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
(!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
(!(what & TAB_L) || checkfield(L, "__len", ++n))) {
lua_pop(L, n); /* pop metatable and tested metamethods */
} }
lua_pushnil(L); else
lua_rawseti(L, 1, pos); /* t[pos] = nil */ luaL_checktype(L, arg, LUA_TTABLE); /* force an error */
return 1; }
} }
static void addfield(lua_State *L, luaL_Buffer *b, int i) { static int tinsert (lua_State *L) {
lua_rawgeti(L, 1, i); lua_Integer pos; /* where to insert new element */
if (!lua_isstring(L, -1)) lua_Integer e = aux_getn(L, 1, TAB_RW);
luaL_error(L, "invalid value (%s) at index %d in table for " e = luaL_intop(+, e, 1); /* first empty element */
LUA_QL("concat"), luaL_typename(L, -1), i); switch (lua_gettop(L)) {
luaL_addvalue(b); case 2: { /* called with only 2 arguments */
pos = e; /* insert new element at the end */
break;
}
case 3: {
lua_Integer i;
pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
/* check whether 'pos' is in [1, e] */
luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2,
"position out of bounds");
for (i = e; i > pos; i--) { /* move up elements */
lua_geti(L, 1, i - 1);
lua_seti(L, 1, i); /* t[i] = t[i - 1] */
}
break;
}
default: {
return luaL_error(L, "wrong number of arguments to 'insert'");
}
}
lua_seti(L, 1, pos); /* t[pos] = v */
return 0;
} }
static int tconcat(lua_State *L) { static int tremove (lua_State *L) {
luaL_Buffer b; lua_Integer size = aux_getn(L, 1, TAB_RW);
size_t lsep; lua_Integer pos = luaL_optinteger(L, 2, size);
int i, last; if (pos != size) /* validate 'pos' if given */
const char *sep = luaL_optlstring(L, 2, "", &lsep); /* check whether 'pos' is in [1, size + 1] */
luaL_checktype(L, 1, LUA_TTABLE); luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2,
i = luaL_optint(L, 3, 1); "position out of bounds");
last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); lua_geti(L, 1, pos); /* result = t[pos] */
luaL_buffinit(L, &b); for ( ; pos < size; pos++) {
for (; i < last; i++) { lua_geti(L, 1, pos + 1);
addfield(L, &b, i); lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
luaL_addlstring(&b, sep, lsep); }
lua_pushnil(L);
lua_seti(L, 1, pos); /* remove entry t[pos] */
return 1;
}
/*
** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
** possible, copy in increasing order, which is better for rehashing.
** "possible" means destination after original range, or smaller
** than origin, or copying to another table.
*/
static int tmove (lua_State *L) {
lua_Integer f = luaL_checkinteger(L, 2);
lua_Integer e = luaL_checkinteger(L, 3);
lua_Integer t = luaL_checkinteger(L, 4);
int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
checktab(L, 1, TAB_R);
checktab(L, tt, TAB_W);
if (e >= f) { /* otherwise, nothing to move */
lua_Integer n, i;
luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3,
"too many elements to move");
n = e - f + 1; /* number of elements to move */
luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
"destination wrap around");
if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
for (i = 0; i < n; i++) {
lua_geti(L, 1, f + i);
lua_seti(L, tt, t + i);
}
} }
if (i == last) /* add last value (if interval was not empty) */ else {
addfield(L, &b, i); for (i = n - 1; i >= 0; i--) {
luaL_pushresult(&b); lua_geti(L, 1, f + i);
return 1; lua_seti(L, tt, t + i);
}
}
}
lua_pushvalue(L, tt); /* return destination table */
return 1;
}
static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
lua_geti(L, 1, i);
if (l_unlikely(!lua_isstring(L, -1)))
luaL_error(L, "invalid value (%s) at index %I in table for 'concat'",
luaL_typename(L, -1), (LUAI_UACINT)i);
luaL_addvalue(b);
}
static int tconcat (lua_State *L) {
luaL_Buffer b;
lua_Integer last = aux_getn(L, 1, TAB_R);
size_t lsep;
const char *sep = luaL_optlstring(L, 2, "", &lsep);
lua_Integer i = luaL_optinteger(L, 3, 1);
last = luaL_optinteger(L, 4, last);
luaL_buffinit(L, &b);
for (; i < last; i++) {
addfield(L, &b, i);
luaL_addlstring(&b, sep, lsep);
}
if (i == last) /* add last value (if interval was not empty) */
addfield(L, &b, i);
luaL_pushresult(&b);
return 1;
} }
@ -116,40 +178,33 @@ static int tconcat(lua_State *L) {
** ======================================================= ** =======================================================
*/ */
static int pack(lua_State *L) { static int tpack (lua_State *L) {
int n = lua_gettop(L); /* number of elements to pack */ int i;
lua_createtable(L, n, 1); /* create result table */ int n = lua_gettop(L); /* number of elements to pack */
lua_pushinteger(L, n); lua_createtable(L, n, 1); /* create result table */
lua_setfield(L, -2, "n"); /* t.n = number of elements */ lua_insert(L, 1); /* put it at index 1 */
if (n > 0) { /* at least one element? */ for (i = n; i >= 1; i--) /* assign elements */
int i; lua_seti(L, 1, i);
lua_pushvalue(L, 1); lua_pushinteger(L, n);
lua_rawseti(L, -2, 1); /* insert first element */ lua_setfield(L, 1, "n"); /* t.n = number of elements */
lua_replace(L, 1); /* move table into index 1 */ return 1; /* return table */
for (i = n; i >= 2; i--) /* assign other elements */
lua_rawseti(L, 1, i);
}
return 1; /* return table */
} }
static int unpack(lua_State *L) { static int tunpack (lua_State *L) {
int i, e; lua_Unsigned n;
unsigned int n; lua_Integer i = luaL_optinteger(L, 2, 1);
luaL_checktype(L, 1, LUA_TTABLE); lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
i = luaL_optint(L, 2, 1); if (i > e) return 0; /* empty range */
e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
if (i > e) return 0; /* empty range */ if (l_unlikely(n >= (unsigned int)INT_MAX ||
!lua_checkstack(L, (int)(++n))))
n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */ return luaL_error(L, "too many results to unpack");
for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n)) lua_geti(L, 1, i);
return luaL_error(L, "too many results to unpack"); }
lua_geti(L, 1, e); /* push last element */
lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ return (int)n;
while (i++ < e) /* push arg[i + 1...e] */
lua_rawgeti(L, 1, i);
return n;
} }
/* }====================================================== */ /* }====================================================== */
@ -159,132 +214,217 @@ static int unpack(lua_State *L) {
/* /*
** {====================================================== ** {======================================================
** Quicksort ** Quicksort
** (based on `Algorithms in MODULA-3', Robert Sedgewick; ** (based on 'Algorithms in MODULA-3', Robert Sedgewick;
** Addison-Wesley, 1993.) ** Addison-Wesley, 1993.)
** ======================================================= ** =======================================================
*/ */
static void set2(lua_State *L, int i, int j) { /* type for array indices */
lua_rawseti(L, 1, i); typedef unsigned int IdxT;
lua_rawseti(L, 1, j);
/*
** Produce a "random" 'unsigned int' to randomize pivot choice. This
** 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
** good choice.)
*/
#if !defined(l_randomizePivot) /* { */
#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;
} }
static int sort_comp(lua_State *L, int a, int b) { #endif /* } */
if (!lua_isnil(L, 2)) { /* function? */
int res;
lua_pushvalue(L, 2); /* arrays larger than 'RANLIMIT' may use randomized pivots */
lua_pushvalue(L, a - 1); /* -1 to compensate function */ #define RANLIMIT 100u
lua_pushvalue(L, b - 2); /* -2 to compensate function and `a' */
lua_call(L, 2, 1);
res = lua_toboolean(L, -1); static void set2 (lua_State *L, IdxT i, IdxT j) {
lua_pop(L, 1); lua_seti(L, 1, i);
return res; lua_seti(L, 1, j);
} else /* a < b? */
return lua_compare(L, a, b, LUA_OPLT);
} }
static void auxsort(lua_State *L, int l, int u) {
while (l < u) { /* for tail recursion */ /*
int i, j; ** Return true iff value at stack index 'a' is less than the value at
/* sort elements a[l], a[(l+u)/2] and a[u] */ ** index 'b' (according to the order of the sort).
lua_rawgeti(L, 1, l); */
lua_rawgeti(L, 1, u); static int sort_comp (lua_State *L, int a, int b) {
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ if (lua_isnil(L, 2)) /* no function? */
set2(L, l, u); /* swap a[l] - a[u] */ return lua_compare(L, a, b, LUA_OPLT); /* a < b */
else else { /* function */
lua_pop(L, 2); int res;
if (u - l == 1) break; /* only 2 elements */ lua_pushvalue(L, 2); /* push function */
i = (l + u) / 2; lua_pushvalue(L, a-1); /* -1 to compensate function */
lua_rawgeti(L, 1, i); lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
lua_rawgeti(L, 1, l); lua_call(L, 2, 1); /* call function */
if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ res = lua_toboolean(L, -1); /* get result */
set2(L, i, l); lua_pop(L, 1); /* pop result */
else { return res;
lua_pop(L, 1); /* remove a[l] */ }
lua_rawgeti(L, 1, u);
if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
set2(L, i, u);
else
lua_pop(L, 2);
}
if (u - l == 2) break; /* only 3 elements */
lua_rawgeti(L, 1, i); /* Pivot */
lua_pushvalue(L, -1);
lua_rawgeti(L, 1, u - 1);
set2(L, i, u - 1);
/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
i = l;
j = u - 1;
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
/* repeat ++i until a[i] >= P */
while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
if (i >= u) luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[i] */
}
/* repeat --j until a[j] <= P */
while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
if (j <= l) luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[j] */
}
if (j < i) {
lua_pop(L, 3); /* pop pivot, a[i], a[j] */
break;
}
set2(L, i, j);
}
lua_rawgeti(L, 1, u - 1);
lua_rawgeti(L, 1, i);
set2(L, u - 1, i); /* swap pivot (a[u-1]) with a[i] */
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
/* adjust so that smaller half is in [j..i] and larger one in [l..u] */
if (i - l < u - i) {
j = l;
i = i - 1;
l = i + 2;
} else {
j = i + 1;
i = u;
u = j - 2;
}
auxsort(L, j, i); /* call recursively the smaller one */
} /* repeat the routine for the larger one */
} }
static int sort(lua_State *L) {
int n = aux_getn(L, 1); /*
luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ ** Does the partition: Pivot P is at the top of the stack.
** precondition: a[lo] <= P == a[up-1] <= a[up],
** so it only needs to do the partition from lo + 1 to up - 2.
** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up]
** returns 'i'.
*/
static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
IdxT i = lo; /* will be incremented before first use */
IdxT j = up - 1; /* will be decremented before first use */
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
for (;;) {
/* next loop: repeat ++i while a[i] < P */
while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */
luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[i] */
}
/* after the loop, a[i] >= P and a[lo .. i - 1] < P */
/* next loop: repeat --j while P < a[j] */
while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */
luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[j] */
}
/* after the loop, a[j] <= P and a[j + 1 .. up] >= P */
if (j < i) { /* no elements out of place? */
/* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */
lua_pop(L, 1); /* pop a[j] */
/* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */
set2(L, up - 1, i);
return i;
}
/* otherwise, swap a[i] - a[j] to restore invariant and repeat */
set2(L, i, j);
}
}
/*
** Choose an element in the middle (2nd-3th quarters) of [lo,up]
** "randomized" by 'rnd'
*/
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
IdxT r4 = (up - lo) / 4; /* range/4 */
IdxT p = rnd % (r4 * 2) + (lo + r4);
lua_assert(lo + r4 <= p && p <= up - r4);
return p;
}
/*
** Quicksort algorithm (recursive function)
*/
static void auxsort (lua_State *L, IdxT lo, IdxT up,
unsigned int rnd) {
while (lo < up) { /* loop for tail recursion */
IdxT p; /* Pivot index */
IdxT n; /* to be used later */
/* sort elements 'lo', 'p', and 'up' */
lua_geti(L, 1, lo);
lua_geti(L, 1, up);
if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
set2(L, lo, up); /* swap a[lo] - a[up] */
else
lua_pop(L, 2); /* remove both values */
if (up - lo == 1) /* only 2 elements? */
return; /* already sorted */
if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */
p = (lo + up)/2; /* middle element is a good pivot */
else /* for larger intervals, it is worth a random pivot */
p = choosePivot(lo, up, rnd);
lua_geti(L, 1, p);
lua_geti(L, 1, lo);
if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
set2(L, p, lo); /* swap a[p] - a[lo] */
else {
lua_pop(L, 1); /* remove a[lo] */
lua_geti(L, 1, up);
if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
set2(L, p, up); /* swap a[up] - a[p] */
else
lua_pop(L, 2);
}
if (up - lo == 2) /* only 3 elements? */
return; /* already sorted */
lua_geti(L, 1, p); /* get middle element (Pivot) */
lua_pushvalue(L, -1); /* push Pivot */
lua_geti(L, 1, up - 1); /* push a[up - 1] */
set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
p = partition(L, lo, up);
/* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
if (p - lo < up - p) { /* lower interval is smaller? */
auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */
n = p - lo; /* size of smaller interval */
lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */
}
else {
auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */
n = up - p; /* size of smaller interval */
up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
}
if ((up - lo) / 128 > n) /* partition too imbalanced? */
rnd = l_randomizePivot(); /* try a new randomization */
} /* tail call auxsort(L, lo, up, rnd) */
}
static int sort (lua_State *L) {
lua_Integer n = aux_getn(L, 1, TAB_RW);
if (n > 1) { /* non-trivial interval? */
luaL_argcheck(L, n < INT_MAX, 1, "array too big");
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
luaL_checktype(L, 2, LUA_TFUNCTION); luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */
lua_settop(L, 2); /* make sure there is two arguments */ lua_settop(L, 2); /* make sure there are two arguments */
auxsort(L, 1, n); auxsort(L, 1, (IdxT)n, 0);
return 0; }
return 0;
} }
/* }====================================================== */ /* }====================================================== */
static const luaL_Reg tab_funcs[] = { static const luaL_Reg tab_funcs[] = {
{"concat", tconcat}, {"concat", tconcat},
#if defined(LUA_COMPAT_MAXN) {"insert", tinsert},
{"maxn", maxn}, {"pack", tpack},
#endif {"unpack", tunpack},
{"insert", tinsert}, {"remove", tremove},
{"pack", pack}, {"move", tmove},
{"unpack", unpack}, {"sort", sort},
{"remove", tremove}, {NULL, NULL}
{"sort", sort},
{NULL, NULL}
}; };
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) return 1;
/* _G.unpack = table.unpack */
lua_getfield(L, -1, "unpack");
lua_setglobal(L, "unpack");
#endif
return 1;
} }

View file

@ -1,47 +1,55 @@
/* /*
** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 roberto Exp $ ** $Id: ltm.c $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <string.h>
#define ltm_c #define ltm_c
#define LUA_CORE #define LUA_CORE
#include "lprefix.h"
#include <string.h>
#include "lua.h" #include "lua.h"
#include "ldebug.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"
#include "ltable.h" #include "ltable.h"
#include "ltm.h" #include "ltm.h"
#include "lvm.h"
static const char udatatypename[] = "userdata"; static const char udatatypename[] = "userdata";
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
"no value", "no value",
"nil", "boolean", udatatypename, "number", "nil", "boolean", udatatypename, "number",
"string", "table", "function", udatatypename, "thread", "string", "table", "function", udatatypename, "thread",
"proto", "upval" /* these last two cases are used for tests only */ "upvalue", "proto" /* these last cases are used for tests only */
}; };
void luaT_init(lua_State *L) { void luaT_init (lua_State *L) {
static const char *const luaT_eventname[] = { /* ORDER TM */ static const char *const luaT_eventname[] = { /* ORDER TM */
"__index", "__newindex", "__index", "__newindex",
"__gc", "__mode", "__len", "__eq", "__gc", "__mode", "__len", "__eq",
"__add", "__sub", "__mul", "__div", "__mod", "__add", "__sub", "__mul", "__mod", "__pow",
"__pow", "__unm", "__lt", "__le", "__div", "__idiv",
"__concat", "__call" "__band", "__bor", "__bxor", "__shl", "__shr",
}; "__unm", "__bnot", "__lt", "__le",
int i; "__concat", "__call", "__close"
for (i = 0; i < TM_N; i++) { };
G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); int i;
luaS_fix(G(L)->tmname[i]); /* never collect these names */ for (i=0; i<TM_N; i++) {
} G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
}
} }
@ -49,28 +57,215 @@ void luaT_init(lua_State *L) {
** function to be used with macro "fasttm": optimized for absence of ** function to be used with macro "fasttm": optimized for absence of
** 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_getstr(events, ename); const TValue *tm = luaH_getshortstr(events, ename);
lua_assert(event <= TM_EQ); lua_assert(event <= TM_EQ);
if (ttisnil(tm)) { /* no tag method? */ if (notm(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;
} else return tm; }
else return tm;
} }
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 (ttypenv(o)) { switch (ttype(o)) {
case LUA_TTABLE: case LUA_TTABLE:
mt = hvalue(o)->metatable; mt = hvalue(o)->metatable;
break; break;
case LUA_TUSERDATA: case LUA_TUSERDATA:
mt = uvalue(o)->metatable; mt = uvalue(o)->metatable;
break; break;
default: default:
mt = G(L)->mt[ttypenv(o)]; mt = G(L)->mt[ttype(o)];
}
return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
}
/*
** Return the name of the type of an object. For tables and userdata
** with metatable, use their '__name' metafield, if present.
*/
const char *luaT_objtypename (lua_State *L, const TValue *o) {
Table *mt;
if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
(ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
if (ttisstring(name)) /* is '__name' a string? */
return getstr(tsvalue(name)); /* use it as type name */
}
return ttypename(ttype(o)); /* else use standard type name */
}
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, const TValue *p3) {
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 */
setobj2s(L, func + 3, p3); /* 3rd argument */
L->top.p = func + 4;
/* metamethod may yield only when called from Lua code */
if (isLuacode(L->ci))
luaD_call(L, func, 0);
else
luaD_callnoyield(L, func, 0);
}
void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, StkId res) {
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 */
}
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 */
if (notm(tm))
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
if (notm(tm)) return 0;
luaT_callTMres(L, tm, p1, p2, res);
return 1;
}
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event) {
if (l_unlikely(!callbinTM(L, p1, p2, res, event))) {
switch (event) {
case TM_BAND: case TM_BOR: case TM_BXOR:
case TM_SHL: case TM_SHR: case TM_BNOT: {
if (ttisnumber(p1) && ttisnumber(p2))
luaG_tointerror(L, p1, p2);
else
luaG_opinterror(L, p1, p2, "perform bitwise operation on");
}
/* calls never return, but to avoid warnings: *//* FALLTHROUGH */
default:
luaG_opinterror(L, p1, p2, "perform arithmetic on");
} }
return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); }
}
void luaT_tryconcatTM (lua_State *L) {
StkId top = L->top.p;
if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2,
TM_CONCAT)))
luaG_concaterror(L, s2v(top - 2), s2v(top - 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.
** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
** behavior: if there is no '__le', try '__lt', based on l <= r iff
** !(r < l) (assuming a total order). If the metamethod yields during
** this substitution, the continuation has to know about it (to negate
** the result of r<l); bit CIST_LEQ in the call status keeps that
** information.
*/
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
TMS event) {
if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */
return !l_isfalse(s2v(L->top.p));
#if defined(LUA_COMPAT_LT_LE)
else if (event == TM_LE) {
/* try '!(p2 < p1)' for '(p1 <= p2)' */
L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
if (callbinTM(L, p2, p1, L->top.p, TM_LT)) {
L->ci->callstatus ^= CIST_LEQ; /* clear mark */
return l_isfalse(s2v(L->top.p));
}
/* else error will remove this 'ci'; no need to clear mark */
}
#endif
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
setivalue(&aux, v2);
if (flip) { /* arguments were exchanged? */
p2 = p1; p1 = &aux; /* correct them */
}
else
p2 = &aux;
return luaT_callorderTM(L, p1, p2, event);
}
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
const Proto *p) {
int i;
int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */
int nextra = actual - nfixparams; /* number of extra arguments */
ci->u.l.nextraargs = nextra;
luaD_checkstack(L, p->maxstacksize + 1);
/* copy function to the top of the stack */
setobjs2s(L, L->top.p++, ci->func.p);
/* move fixed parameters to the top of the stack */
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 += actual + 1;
ci->top.p += actual + 1;
lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
}
void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
int i;
int nextra = ci->u.l.nextraargs;
if (wanted < 0) {
wanted = nextra; /* get all extra arguments available */
checkstackGCp(L, nextra, where); /* ensure stack space */
L->top.p = where + nextra; /* next instruction will need top */
}
for (i = 0; i < wanted && i < nextra; i++)
setobjs2s(L, where + i, ci->func.p - nextra + i);
for (; i < wanted; i++) /* complete required results with nil */
setnilvalue(s2v(where + i));
} }

View file

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.h,v 2.11 2011/02/28 17:32:10 roberto Exp $ ** $Id: ltm.h $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,45 +13,91 @@
/* /*
* WARNING: if you change the order of this enumeration, * WARNING: if you change the order of this enumeration,
* grep "ORDER TM" * grep "ORDER TM" and "ORDER OP"
*/ */
typedef enum { typedef enum {
TM_INDEX, TM_INDEX,
TM_NEWINDEX, TM_NEWINDEX,
TM_GC, TM_GC,
TM_MODE, TM_MODE,
TM_LEN, TM_LEN,
TM_EQ, /* last tag method with `fast' access */ TM_EQ, /* last tag method with fast access */
TM_ADD, TM_ADD,
TM_SUB, TM_SUB,
TM_MUL, TM_MUL,
TM_DIV, TM_MOD,
TM_MOD, TM_POW,
TM_POW, TM_DIV,
TM_UNM, TM_IDIV,
TM_LT, TM_BAND,
TM_LE, TM_BOR,
TM_CONCAT, TM_BXOR,
TM_CALL, TM_SHL,
TM_N /* number of elements in the enum */ TM_SHR,
TM_UNM,
TM_BNOT,
TM_LT,
TM_LE,
TM_CONCAT,
TM_CALL,
TM_CLOSE,
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 7 of the flag is used for
** 'isrealasize'.)
*/
#define maskflags (~(~0u << (TM_EQ + 1)))
/*
** Test whether there is no tagmethod.
** (Because tagmethods use raw accesses, the result may be an "empty" nil.)
*/
#define notm(tm) ttisnil(tm)
#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ #define gfasttm(g,et,e) ((et) == NULL ? NULL : \
((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
#define fasttm(l,et,e) gfasttm(G(l), et, e) #define fasttm(l,et,e) gfasttm(G(l), et, e)
#define ttypename(x) luaT_typenames_[(x) + 1] #define ttypename(x) luaT_typenames_[(x) + 1]
#define objtypename(x) ttypename(ttypenv(x))
LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];)
LUAI_FUNC const TValue *luaT_gettm(Table *events, TMS event, TString *ename); LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
LUAI_FUNC const TValue *luaT_gettmbyobj(lua_State *L, const TValue *o,
TMS event); LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
LUAI_FUNC void luaT_init(lua_State *L); LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
TMS event);
LUAI_FUNC void luaT_init (lua_State *L);
LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, const TValue *p3);
LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f,
const TValue *p1, const TValue *p2, StkId p3);
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
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,
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, int nfixparams,
struct CallInfo *ci, const Proto *p);
LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci,
StkId where, int wanted);
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.285.1.3 2014/05/07 14:15:55 roberto Exp roberto $ ** $Id: lua.h $
** Lua - A Scripting Language ** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** See Copyright Notice at the end of this file
@ -16,87 +16,74 @@
#include "luaconf.h" #include "luaconf.h"
#define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MAJOR "5"
#define LUA_VERSION_MINOR "2" #define LUA_VERSION_MINOR "4"
#define LUA_VERSION_NUM 502 #define LUA_VERSION_RELEASE "7"
#define LUA_VERSION_RELEASE "4"
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_VERSION_NUM 504
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE #define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 7)
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2015 Lua.org, PUC-Rio"
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" #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-2024 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 "\033Lua" #define LUA_SIGNATURE "\x1bLua"
/* option for multiple returns in 'lua_pcall' and 'lua_call' */ /* option for multiple returns in 'lua_pcall' and 'lua_call' */
#define LUA_MULTRET (-1) #define LUA_MULTRET (-1)
/* /*
** pseudo-indices ** Pseudo-indices
** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty
** space after that to help overflow detection)
*/ */
#define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX #define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000)
#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
/* thread status */ /* thread status */
#define LUA_OK 0 #define LUA_OK 0
#define LUA_YIELD 1 #define LUA_YIELD 1
#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_ERRGCMM 5 #define LUA_ERRERR 5
#define LUA_ERRERR 6
typedef struct lua_State lua_State; typedef struct lua_State lua_State;
typedef int (*lua_CFunction)(lua_State *L);
/*
** functions that read/write blocks when loading/dumping Lua chunks
*/
typedef const char *(*lua_Reader)(lua_State *L, void *ud, size_t *sz);
typedef int (*lua_Writer)(lua_State *L, const void *p, size_t sz, void *ud);
/*
** prototype for memory-allocation functions
*/
typedef void *(*lua_Alloc)(void *ud, void *ptr, size_t osize, size_t nsize);
/* /*
** basic types ** basic types
*/ */
#define LUA_TNONE (-1) #define LUA_TNONE (-1)
#define LUA_TNIL 0 #define LUA_TNIL 0
#define LUA_TBOOLEAN 1 #define LUA_TBOOLEAN 1
#define LUA_TLIGHTUSERDATA 2 #define LUA_TLIGHTUSERDATA 2
#define LUA_TNUMBER 3 #define LUA_TNUMBER 3
#define LUA_TSTRING 4 #define LUA_TSTRING 4
#define LUA_TTABLE 5 #define LUA_TTABLE 5
#define LUA_TFUNCTION 6 #define LUA_TFUNCTION 6
#define LUA_TUSERDATA 7 #define LUA_TUSERDATA 7
#define LUA_TTHREAD 8 #define LUA_TTHREAD 8
#define LUA_NUMTAGS 9 #define LUA_NUMTYPES 9
/* minimum Lua stack available to a C function */ /* minimum Lua stack available to a C function */
#define LUA_MINSTACK 20 #define LUA_MINSTACK 20
/* predefined values in the registry */ /* predefined values in the registry */
#define LUA_RIDX_MAINTHREAD 1 #define LUA_RIDX_MAINTHREAD 1
#define LUA_RIDX_GLOBALS 2 #define LUA_RIDX_GLOBALS 2
#define LUA_RIDX_LAST LUA_RIDX_GLOBALS #define LUA_RIDX_LAST LUA_RIDX_GLOBALS
/* type of numbers in Lua */ /* type of numbers in Lua */
@ -109,6 +96,51 @@ typedef LUA_INTEGER lua_Integer;
/* unsigned integer type */ /* unsigned integer type */
typedef LUA_UNSIGNED lua_Unsigned; typedef LUA_UNSIGNED lua_Unsigned;
/* type for continuation-function contexts */
typedef LUA_KCONTEXT lua_KContext;
/*
** Type for C functions registered with Lua
*/
typedef int (*lua_CFunction) (lua_State *L);
/*
** Type for continuation functions
*/
typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
/*
** Type for functions that read/write blocks when loading/dumping Lua chunks
*/
typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud);
/*
** Type for memory-allocation functions
*/
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);
/* /*
@ -128,225 +160,273 @@ extern const char lua_ident[];
/* /*
** state manipulation ** state manipulation
*/ */
LUA_API lua_State *(lua_newstate)(lua_Alloc f, void *ud); 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 int (lua_resetthread) (lua_State *L); /* Deprecated! */
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 const lua_Number *(lua_version)(lua_State *L); LUA_API lua_Number (lua_version) (lua_State *L);
/* /*
** basic stack manipulation ** basic stack manipulation
*/ */
LUA_API int (lua_absindex)(lua_State *L, int idx); LUA_API int (lua_absindex) (lua_State *L, int idx);
LUA_API int (lua_gettop)(lua_State *L); LUA_API int (lua_gettop) (lua_State *L);
LUA_API void (lua_settop)(lua_State *L, int idx); LUA_API void (lua_settop) (lua_State *L, int idx);
LUA_API void (lua_pushvalue)(lua_State *L, int idx); LUA_API void (lua_pushvalue) (lua_State *L, int idx);
LUA_API void (lua_remove)(lua_State *L, int idx); LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
LUA_API void (lua_insert)(lua_State *L, int idx); LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
LUA_API void (lua_replace)(lua_State *L, int idx); LUA_API int (lua_checkstack) (lua_State *L, int n);
LUA_API void (lua_copy)(lua_State *L, int fromidx, int toidx);
LUA_API int (lua_checkstack)(lua_State *L, int sz);
LUA_API void (lua_xmove)(lua_State *from, lua_State *to, int n); LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
/* /*
** access functions (stack -> C) ** access functions (stack -> C)
*/ */
LUA_API int (lua_isnumber)(lua_State *L, int idx); LUA_API int (lua_isnumber) (lua_State *L, int idx);
LUA_API int (lua_isstring)(lua_State *L, int idx); LUA_API int (lua_isstring) (lua_State *L, int idx);
LUA_API int (lua_iscfunction)(lua_State *L, int idx); LUA_API int (lua_iscfunction) (lua_State *L, int idx);
LUA_API int (lua_isuserdata)(lua_State *L, int idx); LUA_API int (lua_isinteger) (lua_State *L, int idx);
LUA_API int (lua_type)(lua_State *L, int idx); LUA_API int (lua_isuserdata) (lua_State *L, int idx);
LUA_API const char *(lua_typename)(lua_State *L, int tp); LUA_API int (lua_type) (lua_State *L, int idx);
LUA_API const char *(lua_typename) (lua_State *L, int tp);
LUA_API lua_Number(lua_tonumberx)(lua_State *L, int idx, int *isnum); 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 lua_Unsigned(lua_tounsignedx)(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); LUA_API const void *(lua_topointer) (lua_State *L, int idx);
LUA_API const void *(lua_topointer)(lua_State *L, int idx);
/* /*
** Comparison and arithmetic functions ** Comparison and arithmetic functions
*/ */
#define LUA_OPADD 0 /* ORDER TM */ #define LUA_OPADD 0 /* ORDER TM, ORDER OP */
#define LUA_OPSUB 1 #define LUA_OPSUB 1
#define LUA_OPMUL 2 #define LUA_OPMUL 2
#define LUA_OPDIV 3 #define LUA_OPMOD 3
#define LUA_OPMOD 4 #define LUA_OPPOW 4
#define LUA_OPPOW 5 #define LUA_OPDIV 5
#define LUA_OPUNM 6 #define LUA_OPIDIV 6
#define LUA_OPBAND 7
#define LUA_OPBOR 8
#define LUA_OPBXOR 9
#define LUA_OPSHL 10
#define LUA_OPSHR 11
#define LUA_OPUNM 12
#define LUA_OPBNOT 13
LUA_API void (lua_arith)(lua_State *L, int op); LUA_API void (lua_arith) (lua_State *L, int op);
#define LUA_OPEQ 0 #define LUA_OPEQ 0
#define LUA_OPLT 1 #define LUA_OPLT 1
#define LUA_OPLE 2 #define LUA_OPLE 2
LUA_API int (lua_rawequal)(lua_State *L, int idx1, int idx2); LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
LUA_API int (lua_compare)(lua_State *L, int idx1, int idx2, int op); LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op);
/* /*
** push functions (C -> stack) ** push functions (C -> stack)
*/ */
LUA_API void (lua_pushnil)(lua_State *L); 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 void (lua_pushunsigned)(lua_State *L, lua_Unsigned 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 l); 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); LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
LUA_API const char *(lua_pushfstring)(lua_State *L, const char *fmt, ...); LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
LUA_API void (lua_pushcclosure)(lua_State *L, lua_CFunction fn, int n); LUA_API void (lua_pushboolean) (lua_State *L, int b);
LUA_API void (lua_pushboolean)(lua_State *L, int b); LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
LUA_API void (lua_pushlightuserdata)(lua_State *L, void *p); LUA_API int (lua_pushthread) (lua_State *L);
LUA_API int (lua_pushthread)(lua_State *L);
/* /*
** get functions (Lua -> stack) ** get functions (Lua -> stack)
*/ */
LUA_API void (lua_getglobal)(lua_State *L, const char *var); LUA_API int (lua_getglobal) (lua_State *L, const char *name);
LUA_API void (lua_gettable)(lua_State *L, int idx); LUA_API int (lua_gettable) (lua_State *L, int idx);
LUA_API void (lua_getfield)(lua_State *L, int idx, const char *k); LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
LUA_API void (lua_rawget)(lua_State *L, int idx); LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n);
LUA_API void (lua_rawgeti)(lua_State *L, int idx, int n); LUA_API int (lua_rawget) (lua_State *L, int idx);
LUA_API void (lua_rawgetp)(lua_State *L, int idx, const void *p); LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
LUA_API void (lua_createtable)(lua_State *L, int narr, int nrec); LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
LUA_API void *(lua_newuserdata)(lua_State *L, size_t sz);
LUA_API int (lua_getmetatable)(lua_State *L, int objindex); LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
LUA_API void (lua_getuservalue)(lua_State *L, int idx); LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n);
/* /*
** set functions (stack -> Lua) ** set functions (stack -> Lua)
*/ */
LUA_API void (lua_setglobal)(lua_State *L, const char *var); LUA_API void (lua_setglobal) (lua_State *L, const char *name);
LUA_API void (lua_settable)(lua_State *L, int idx); LUA_API void (lua_settable) (lua_State *L, int idx);
LUA_API void (lua_setfield)(lua_State *L, int idx, const char *k); LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
LUA_API void (lua_rawset)(lua_State *L, int idx); LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n);
LUA_API void (lua_rawseti)(lua_State *L, int idx, int n); LUA_API void (lua_rawset) (lua_State *L, int idx);
LUA_API void (lua_rawsetp)(lua_State *L, int idx, const void *p); LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n);
LUA_API int (lua_setmetatable)(lua_State *L, int objindex); LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
LUA_API void (lua_setuservalue)(lua_State *L, int idx); LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n);
/* /*
** 'load' and 'call' functions (load and run Lua code) ** 'load' and 'call' functions (load and run Lua code)
*/ */
LUA_API void (lua_callk)(lua_State *L, int nargs, int nresults, int ctx, LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults,
lua_CFunction k); lua_KContext ctx, lua_KFunction k);
#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
LUA_API int (lua_getctx)(lua_State *L, int *ctx); LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
lua_KContext ctx, lua_KFunction k);
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
LUA_API int (lua_pcallk)(lua_State *L, int nargs, int nresults, int errfunc, LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
int ctx, lua_CFunction k); const char *chunkname, const char *mode);
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
LUA_API int (lua_load)(lua_State *L, lua_Reader reader, void *dt, LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
const char *chunkname,
const char *mode);
LUA_API int (lua_dump)(lua_State *L, lua_Writer writer, void *data);
/* /*
** coroutine functions ** coroutine functions
*/ */
LUA_API int (lua_yieldk)(lua_State *L, int nresults, int ctx, LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
lua_CFunction k); lua_KFunction k);
#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) 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);
#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
/*
** Warning-related functions
*/
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 function and options ** garbage-collection function and options
*/ */
#define LUA_GCSTOP 0 #define LUA_GCSTOP 0
#define LUA_GCRESTART 1 #define LUA_GCRESTART 1
#define LUA_GCCOLLECT 2 #define LUA_GCCOLLECT 2
#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_GCSETPAUSE 6 #define LUA_GCSETPAUSE 6
#define LUA_GCSETSTEPMUL 7 #define LUA_GCSETSTEPMUL 7
#define LUA_GCSETMAJORINC 8 #define LUA_GCISRUNNING 9
#define LUA_GCISRUNNING 9 #define LUA_GCGEN 10
#define LUA_GCGEN 10 #define LUA_GCINC 11
#define LUA_GCINC 11
LUA_API int (lua_gc)(lua_State *L, int what, int data); LUA_API int (lua_gc) (lua_State *L, int what, ...);
/* /*
** miscellaneous functions ** miscellaneous functions
*/ */
LUA_API int (lua_error)(lua_State *L); LUA_API int (lua_error) (lua_State *L);
LUA_API int (lua_next)(lua_State *L, int idx); 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);
LUA_API lua_Alloc(lua_getallocf)(lua_State *L, void **ud); LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
LUA_API void (lua_setallocf)(lua_State *L, lua_Alloc f, 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_toclose) (lua_State *L, int idx);
LUA_API void (lua_closeslot) (lua_State *L, int idx);
/* /*
** =============================================================== ** {==============================================================
** some useful macros ** some useful macros
** =============================================================== ** ===============================================================
*/ */
#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL) #define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE))
#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL)
#define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL)
#define lua_pop(L,n) lua_settop(L, -(n)-1) #define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL)
#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL)
#define lua_newtable(L) lua_createtable(L, 0, 0) #define lua_pop(L,n) lua_settop(L, -(n)-1)
#define lua_register(L,n,f) \ #define lua_newtable(L) lua_createtable(L, 0, 0)
(lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) #define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
#define lua_pushliteral(L, s) \ #define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION)
lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) #define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
#define lua_pushglobaltable(L) \ #define lua_pushliteral(L, s) lua_pushstring(L, "" s)
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) #define lua_pushglobaltable(L) \
((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS))
#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
#define lua_insert(L,idx) lua_rotate(L, (idx), 1)
#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1))
#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1))
/* }============================================================== */
/*
** {==============================================================
** compatibility macros
** ===============================================================
*/
#if defined(LUA_COMPAT_APIINTCASTS)
#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is))
#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
#endif
#define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1)
#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1)
#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1)
#define LUA_NUMTAGS LUA_NUMTYPES
/* }============================================================== */
/* /*
** {====================================================================== ** {======================================================================
@ -358,68 +438,66 @@ LUA_API void (lua_setallocf)(lua_State *L, lua_Alloc f, void *ud);
/* /*
** Event codes ** Event codes
*/ */
#define LUA_HOOKCALL 0 #define LUA_HOOKCALL 0
#define LUA_HOOKRET 1 #define LUA_HOOKRET 1
#define LUA_HOOKLINE 2 #define LUA_HOOKLINE 2
#define LUA_HOOKCOUNT 3 #define LUA_HOOKCOUNT 3
#define LUA_HOOKTAILCALL 4 #define LUA_HOOKTAILCALL 4
/* /*
** Event masks ** Event masks
*/ */
#define LUA_MASKCALL (1 << LUA_HOOKCALL) #define LUA_MASKCALL (1 << LUA_HOOKCALL)
#define LUA_MASKRET (1 << LUA_HOOKRET) #define LUA_MASKRET (1 << LUA_HOOKRET)
#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 */ LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
typedef void (*lua_Hook)(lua_State *L, lua_Debug *ar); LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n);
LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n);
LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n);
LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n);
LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n);
LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1,
int fidx2, int n2);
LUA_API int (lua_getstack)(lua_State *L, int level, lua_Debug *ar); LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count);
LUA_API int (lua_getinfo)(lua_State *L, const char *what, lua_Debug *ar); LUA_API lua_Hook (lua_gethook) (lua_State *L);
LUA_API const char *(lua_getlocal)(lua_State *L, const lua_Debug *ar, int n); LUA_API int (lua_gethookmask) (lua_State *L);
LUA_API const char *(lua_setlocal)(lua_State *L, const lua_Debug *ar, int n); LUA_API int (lua_gethookcount) (lua_State *L);
LUA_API const char *(lua_getupvalue)(lua_State *L, int funcindex, int n);
LUA_API const char *(lua_setupvalue)(lua_State *L, int funcindex, int n);
LUA_API void *(lua_upvalueid)(lua_State *L, int fidx, int n);
LUA_API void (lua_upvaluejoin)(lua_State *L, int fidx1, int n1,
int fidx2, int n2);
LUA_API int (lua_sethook)(lua_State *L, lua_Hook func, int mask, int count);
LUA_API lua_Hook(lua_gethook)(lua_State *L);
LUA_API int (lua_gethookmask)(lua_State *L);
LUA_API int (lua_gethookcount)(lua_State *L);
LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit);
struct lua_Debug { struct lua_Debug {
int event; int event;
const char *name; /* (n) */ const char *name; /* (n) */
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) */
int currentline; /* (l) */ size_t srclen; /* (S) */
int linedefined; /* (S) */ int currentline; /* (l) */
int lastlinedefined; /* (S) */ int linedefined; /* (S) */
unsigned char nups; /* (u) number of upvalues */ int lastlinedefined; /* (S) */
unsigned char nparams;/* (u) number of parameters */ unsigned char nups; /* (u) number of upvalues */
char isvararg; /* (u) */ unsigned char nparams;/* (u) number of parameters */
char istailcall; /* (t) */ char isvararg; /* (u) */
char short_src[LUA_IDSIZE]; /* (S) */ char istailcall; /* (t) */
/* private part */ unsigned short ftransfer; /* (r) index of first value transferred */
struct CallInfo *i_ci; /* active function */ unsigned short ntransfer; /* (r) number of transferred values */
char short_src[LUA_IDSIZE]; /* (S) */
/* private part */
struct CallInfo *i_ci; /* active function */
}; };
/* }====================================================================== */ /* }====================================================================== */
/****************************************************************************** /******************************************************************************
* Copyright (C) 1994-2015 Lua.org, PUC-Rio. * Copyright (C) 1994-2024 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

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lualib.h,v 1.43 2011/12/08 12:11:37 roberto Exp $ ** $Id: lualib.h $
** Lua standard libraries ** Lua standard libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,45 +11,42 @@
#include "lua.h" #include "lua.h"
/* version suffix for environment variable names */
#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
LUAMOD_API int (luaopen_base)(lua_State *L);
#define LUA_COLIBNAME "coroutine" LUAMOD_API int (luaopen_base) (lua_State *L);
LUAMOD_API int (luaopen_coroutine)(lua_State *L);
#define LUA_TABLIBNAME "table" #define LUA_COLIBNAME "coroutine"
LUAMOD_API int (luaopen_table)(lua_State *L); LUAMOD_API int (luaopen_coroutine) (lua_State *L);
#define LUA_IOLIBNAME "io" #define LUA_TABLIBNAME "table"
LUAMOD_API int (luaopen_io)(lua_State *L); LUAMOD_API int (luaopen_table) (lua_State *L);
#define LUA_OSLIBNAME "os" #define LUA_IOLIBNAME "io"
LUAMOD_API int (luaopen_os)(lua_State *L); LUAMOD_API int (luaopen_io) (lua_State *L);
#define LUA_STRLIBNAME "string" #define LUA_OSLIBNAME "os"
LUAMOD_API int (luaopen_string)(lua_State *L); LUAMOD_API int (luaopen_os) (lua_State *L);
#define LUA_BITLIBNAME "bit32" #define LUA_STRLIBNAME "string"
LUAMOD_API int (luaopen_bit32)(lua_State *L); LUAMOD_API int (luaopen_string) (lua_State *L);
#define LUA_MATHLIBNAME "math" #define LUA_UTF8LIBNAME "utf8"
LUAMOD_API int (luaopen_math)(lua_State *L); LUAMOD_API int (luaopen_utf8) (lua_State *L);
#define LUA_DBLIBNAME "debug" #define LUA_MATHLIBNAME "math"
LUAMOD_API int (luaopen_debug)(lua_State *L); LUAMOD_API int (luaopen_math) (lua_State *L);
#define LUA_LOADLIBNAME "package" #define LUA_DBLIBNAME "debug"
LUAMOD_API int (luaopen_package)(lua_State *L); LUAMOD_API int (luaopen_debug) (lua_State *L);
#define LUA_LOADLIBNAME "package"
LUAMOD_API int (luaopen_package) (lua_State *L);
/* open all previous libraries */ /* open all previous libraries */
LUALIB_API void (luaL_openlibs)(lua_State *L); LUALIB_API void (luaL_openlibs) (lua_State *L);
#if !defined(lua_assert)
#define lua_assert(x) ((void)0)
#endif
#endif #endif

View file

@ -1,14 +1,18 @@
/* /*
** $Id: lundump.c,v 2.22 2012/05/08 13:53:33 roberto Exp $ ** $Id: lundump.c $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <string.h>
#define lundump_c #define lundump_c
#define LUA_CORE #define LUA_CORE
#include "lprefix.h"
#include <limits.h>
#include <string.h>
#include "lua.h" #include "lua.h"
#include "ldebug.h" #include "ldebug.h"
@ -20,221 +24,312 @@
#include "lundump.h" #include "lundump.h"
#include "lzio.h" #include "lzio.h"
typedef struct {
lua_State *L;
ZIO *Z;
Mbuffer *b;
const char *name;
} LoadState;
static l_noret error(LoadState *S, const char *why) {
luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
luaD_throw(S->L, LUA_ERRSYNTAX);
}
#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
#define LoadByte(S) (lu_byte)LoadChar(S)
#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
#if !defined(luai_verifycode) #if !defined(luai_verifycode)
#define luai_verifycode(L,b,f) /* empty */ #define luai_verifycode(L,f) /* empty */
#endif #endif
static void LoadBlock(LoadState *S, void *b, size_t size) {
if (luaZ_read(S->Z, b, size) != 0) error(S, "truncated"); typedef struct {
lua_State *L;
ZIO *Z;
const char *name;
} LoadState;
static l_noret error (LoadState *S, const char *why) {
luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why);
luaD_throw(S->L, LUA_ERRSYNTAX);
} }
static int LoadChar(LoadState *S) {
char x;
LoadVar(S, x);
return x;
}
static int LoadInt(LoadState *S) {
int x;
LoadVar(S, x);
if (x < 0) error(S, "corrupted");
return x;
}
static lua_Number LoadNumber(LoadState *S) {
lua_Number x;
LoadVar(S, x);
return x;
}
static TString *LoadString(LoadState *S) {
size_t size;
LoadVar(S, size);
if (size == 0)
return NULL;
else {
char *s = luaZ_openspace(S->L, S->b, size);
LoadBlock(S, s, size * sizeof(char));
return luaS_newlstr(S->L, s, size - 1); /* remove trailing '\0' */
}
}
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, sizeof(Instruction));
}
static void LoadFunction(LoadState *S, Proto *f);
static void LoadConstants(LoadState *S, Proto *f) {
int i, n;
n = LoadInt(S);
f->k = luaM_newvector(S->L, n, TValue);
f->sizek = n;
for (i = 0; i < n; i++) setnilvalue(&f->k[i]);
for (i = 0; i < n; i++) {
TValue *o = &f->k[i];
int t = LoadChar(S);
switch (t) {
case LUA_TNIL:
setnilvalue(o);
break;
case LUA_TBOOLEAN:
setbvalue(o, LoadChar(S));
break;
case LUA_TNUMBER:
setnvalue(o, LoadNumber(S));
break;
case LUA_TSTRING:
setsvalue2n(S->L, o, LoadString(S));
break;
default:
lua_assert(0);
}
}
n = LoadInt(S);
f->p = luaM_newvector(S->L, n, Proto *);
f->sizep = n;
for (i = 0; i < n; i++) f->p[i] = NULL;
for (i = 0; i < n; i++) {
f->p[i] = luaF_newproto(S->L);
LoadFunction(S, f->p[i]);
}
}
static void LoadUpvalues(LoadState *S, Proto *f) {
int i, n;
n = LoadInt(S);
f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
f->sizeupvalues = n;
for (i = 0; i < n; i++) f->upvalues[i].name = NULL;
for (i = 0; i < n; i++) {
f->upvalues[i].instack = LoadByte(S);
f->upvalues[i].idx = LoadByte(S);
}
}
static void LoadDebug(LoadState *S, Proto *f) {
int i, n;
f->source = LoadString(S);
n = LoadInt(S);
f->lineinfo = luaM_newvector(S->L, n, int);
f->sizelineinfo = n;
LoadVector(S, f->lineinfo, n, sizeof(int));
n = LoadInt(S);
f->locvars = luaM_newvector(S->L, n, LocVar);
f->sizelocvars = n;
for (i = 0; i < n; i++) f->locvars[i].varname = NULL;
for (i = 0; i < n; i++) {
f->locvars[i].varname = LoadString(S);
f->locvars[i].startpc = LoadInt(S);
f->locvars[i].endpc = LoadInt(S);
}
n = LoadInt(S);
for (i = 0; i < n; i++) f->upvalues[i].name = LoadString(S);
}
static void LoadFunction(LoadState *S, Proto *f) {
f->linedefined = LoadInt(S);
f->lastlinedefined = LoadInt(S);
f->numparams = LoadByte(S);
f->is_vararg = LoadByte(S);
f->maxstacksize = LoadByte(S);
LoadCode(S, f);
LoadConstants(S, f);
LoadUpvalues(S, f);
LoadDebug(S, f);
}
/* the code below must be consistent with the code in luaU_header */
#define N0 LUAC_HEADERSIZE
#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
#define N2 N1+2
#define N3 N2+6
static void LoadHeader(LoadState *S) {
lu_byte h[LUAC_HEADERSIZE];
lu_byte s[LUAC_HEADERSIZE];
luaU_header(h);
memcpy(s, h, sizeof(char)); /* first char already read */
LoadBlock(S, s + sizeof(char), LUAC_HEADERSIZE - sizeof(char));
if (memcmp(h, s, N0) == 0) return;
if (memcmp(h, s, N1) != 0) error(S, "not a");
if (memcmp(h, s, N2) != 0) error(S, "version mismatch in");
if (memcmp(h, s, N3) != 0) error(S, "incompatible");
else error(S, "corrupted");
}
/* /*
** load precompiled chunk ** All high-level loads go through loadVector; you can change it to
** adapt to the endianness of the input
*/ */
Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, const char *name) { #define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0]))
LoadState S;
Closure *cl; static void loadBlock (LoadState *S, void *b, size_t size) {
if (*name == '@' || *name == '=') if (luaZ_read(S->Z, b, size) != 0)
S.name = name + 1; error(S, "truncated chunk");
else if (*name == LUA_SIGNATURE[0]) }
S.name = "binary string";
else
S.name = name; #define loadVar(S,x) loadVector(S,&x,1)
S.L = L;
S.Z = Z;
S.b = buff; static lu_byte loadByte (LoadState *S) {
LoadHeader(&S); int b = zgetc(S->Z);
cl = luaF_newLclosure(L, 1); if (b == EOZ)
setclLvalue(L, L->top, cl); error(S, "truncated chunk");
incr_top(L); return cast_byte(b);
cl->l.p = luaF_newproto(L); }
LoadFunction(&S, cl->l.p);
if (cl->l.p->sizeupvalues != 1) {
Proto *p = cl->l.p; static size_t loadUnsigned (LoadState *S, size_t limit) {
cl = luaF_newLclosure(L, cl->l.p->sizeupvalues); size_t x = 0;
cl->l.p = p; int b;
setclLvalue(L, L->top - 1, cl); limit >>= 7;
} do {
luai_verifycode(L, buff, cl->l.p); b = loadByte(S);
return cl; if (x >= limit)
error(S, "integer overflow");
x = (x << 7) | (b & 0x7f);
} while ((b & 0x80) == 0);
return x;
}
static size_t loadSize (LoadState *S) {
return loadUnsigned(S, MAX_SIZET);
}
static int loadInt (LoadState *S) {
return cast_int(loadUnsigned(S, INT_MAX));
}
static lua_Number loadNumber (LoadState *S) {
lua_Number x;
loadVar(S, x);
return x;
}
static lua_Integer loadInteger (LoadState *S) {
lua_Integer x;
loadVar(S, x);
return x;
} }
#define MYINT(s) (s[0]-'0')
#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
#define FORMAT 0 /* this is the official format */
/* /*
* make header for precompiled chunks ** Load a nullable string into prototype 'p'.
* if you change the code below be sure to update LoadHeader and FORMAT above
* and LUAC_HEADERSIZE in lundump.h
*/ */
void luaU_header(lu_byte *h) { static TString *loadStringN (LoadState *S, Proto *p) {
int x = 1; lua_State *L = S->L;
memcpy(h, LUA_SIGNATURE, sizeof(LUA_SIGNATURE) - sizeof(char)); TString *ts;
h += sizeof(LUA_SIGNATURE) - sizeof(char); size_t size = loadSize(S);
*h++ = cast_byte(VERSION); if (size == 0) /* no string? */
*h++ = cast_byte(FORMAT); return NULL;
*h++ = cast_byte(*(char *)&x); /* endianness */ else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
*h++ = cast_byte(sizeof(int)); char buff[LUAI_MAXSHORTLEN];
*h++ = cast_byte(sizeof(size_t)); loadVector(S, buff, size); /* load string into buffer */
*h++ = cast_byte(sizeof(Instruction)); ts = luaS_newlstr(L, buff, size); /* create string */
*h++ = cast_byte(sizeof(lua_Number)); }
*h++ = cast_byte(((lua_Number)0.5) == 0); /* is lua_Number integral? */ else { /* long string */
memcpy(h, LUAC_TAIL, sizeof(LUAC_TAIL) - sizeof(char)); ts = luaS_createlngstrobj(L, size); /* create string */
setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */
luaD_inctop(L);
loadVector(S, getlngstr(ts), size); /* load directly in final place */
L->top.p--; /* pop string */
}
luaC_objbarrier(L, p, ts);
return ts;
} }
/*
** Load a non-nullable string into prototype 'p'.
*/
static TString *loadString (LoadState *S, Proto *p) {
TString *st = loadStringN(S, p);
if (st == NULL)
error(S, "bad format for constant string");
return st;
}
static void loadCode (LoadState *S, Proto *f) {
int n = loadInt(S);
f->code = luaM_newvectorchecked(S->L, n, Instruction);
f->sizecode = n;
loadVector(S, f->code, n);
}
static void loadFunction(LoadState *S, Proto *f, TString *psource);
static void loadConstants (LoadState *S, Proto *f) {
int i;
int n = loadInt(S);
f->k = luaM_newvectorchecked(S->L, n, TValue);
f->sizek = n;
for (i = 0; i < n; i++)
setnilvalue(&f->k[i]);
for (i = 0; i < n; i++) {
TValue *o = &f->k[i];
int t = loadByte(S);
switch (t) {
case LUA_VNIL:
setnilvalue(o);
break;
case LUA_VFALSE:
setbfvalue(o);
break;
case LUA_VTRUE:
setbtvalue(o);
break;
case LUA_VNUMFLT:
setfltvalue(o, loadNumber(S));
break;
case LUA_VNUMINT:
setivalue(o, loadInteger(S));
break;
case LUA_VSHRSTR:
case LUA_VLNGSTR:
setsvalue2n(S->L, o, loadString(S, f));
break;
default: lua_assert(0);
}
}
}
static void loadProtos (LoadState *S, Proto *f) {
int i;
int n = loadInt(S);
f->p = luaM_newvectorchecked(S->L, n, Proto *);
f->sizep = n;
for (i = 0; i < n; i++)
f->p[i] = NULL;
for (i = 0; i < n; i++) {
f->p[i] = luaF_newproto(S->L);
luaC_objbarrier(S->L, f, f->p[i]);
loadFunction(S, f->p[i], f->source);
}
}
/*
** Load the upvalues for a function. The names must be filled first,
** because the filling of the other fields can raise read errors and
** the creation of the error message can call an emergency collection;
** in that case all prototypes must be consistent for the GC.
*/
static void loadUpvalues (LoadState *S, Proto *f) {
int i, n;
n = loadInt(S);
f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
f->sizeupvalues = n;
for (i = 0; i < n; i++) /* make array valid for GC */
f->upvalues[i].name = NULL;
for (i = 0; i < n; i++) { /* following calls can raise errors */
f->upvalues[i].instack = loadByte(S);
f->upvalues[i].idx = loadByte(S);
f->upvalues[i].kind = loadByte(S);
}
}
static void loadDebug (LoadState *S, Proto *f) {
int i, n;
n = loadInt(S);
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
f->sizelineinfo = n;
loadVector(S, f->lineinfo, n);
n = loadInt(S);
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
f->sizeabslineinfo = n;
for (i = 0; i < n; i++) {
f->abslineinfo[i].pc = loadInt(S);
f->abslineinfo[i].line = loadInt(S);
}
n = loadInt(S);
f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
f->sizelocvars = n;
for (i = 0; i < n; i++)
f->locvars[i].varname = NULL;
for (i = 0; i < n; i++) {
f->locvars[i].varname = loadStringN(S, f);
f->locvars[i].startpc = loadInt(S);
f->locvars[i].endpc = 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++)
f->upvalues[i].name = loadStringN(S, f);
}
static void loadFunction (LoadState *S, Proto *f, TString *psource) {
f->source = loadStringN(S, f);
if (f->source == NULL) /* no source in dump? */
f->source = psource; /* reuse parent's source */
f->linedefined = loadInt(S);
f->lastlinedefined = loadInt(S);
f->numparams = loadByte(S);
f->is_vararg = loadByte(S);
f->maxstacksize = loadByte(S);
loadCode(S, f);
loadConstants(S, f);
loadUpvalues(S, f);
loadProtos(S, f);
loadDebug(S, f);
}
static void checkliteral (LoadState *S, const char *s, const char *msg) {
char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
size_t len = strlen(s);
loadVector(S, buff, len);
if (memcmp(s, buff, len) != 0)
error(S, msg);
}
static void fchecksize (LoadState *S, size_t size, const char *tname) {
if (loadByte(S) != size)
error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
}
#define checksize(S,t) fchecksize(S,sizeof(t),#t)
static void checkHeader (LoadState *S) {
/* skip 1st char (already read and checked) */
checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
if (loadByte(S) != LUAC_VERSION)
error(S, "version mismatch");
if (loadByte(S) != LUAC_FORMAT)
error(S, "format mismatch");
checkliteral(S, LUAC_DATA, "corrupted chunk");
checksize(S, Instruction);
checksize(S, lua_Integer);
checksize(S, lua_Number);
if (loadInteger(S) != LUAC_INT)
error(S, "integer format mismatch");
if (loadNumber(S) != LUAC_NUM)
error(S, "float format mismatch");
}
/*
** Load precompiled chunk.
*/
LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
LoadState S;
LClosure *cl;
if (*name == '@' || *name == '=')
S.name = name + 1;
else if (*name == LUA_SIGNATURE[0])
S.name = "binary string";
else
S.name = name;
S.L = L;
S.Z = Z;
checkHeader(&S);
cl = luaF_newLclosure(L, loadByte(&S));
setclLvalue2s(L, L->top.p, cl);
luaD_inctop(L);
cl->p = luaF_newproto(L);
luaC_objbarrier(L, cl, cl->p);
loadFunction(&S, cl->p, NULL);
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
luai_verifycode(L, cl->p);
return cl;
}

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.h,v 1.39 2012/05/08 13:53:33 roberto Exp $ ** $Id: lundump.h $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -7,22 +7,29 @@
#ifndef lundump_h #ifndef lundump_h
#define lundump_h #define lundump_h
#include "llimits.h"
#include "lobject.h" #include "lobject.h"
#include "lzio.h" #include "lzio.h"
/* load one chunk; from lundump.c */
LUAI_FUNC Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, const char *name);
/* make header; from lundump.c */
LUAI_FUNC void luaU_header(lu_byte *h);
/* dump one chunk; from ldump.c */
LUAI_FUNC int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, int strip);
/* data to catch conversion errors */ /* data to catch conversion errors */
#define LUAC_TAIL "\x19\x93\r\n\x1a\n" #define LUAC_DATA "\x19\x93\r\n\x1a\n"
/* size in bytes of header of binary files */ #define LUAC_INT 0x5678
#define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) #define LUAC_NUM cast_num(370.5)
/*
** Encode major-minor version in one byte, one nibble for each
*/
#define LUAC_VERSION (((LUA_VERSION_NUM / 100) * 16) + LUA_VERSION_NUM % 100)
#define LUAC_FORMAT 0 /* this is the official format */
/* load one chunk; from lundump.c */
LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name);
/* dump one chunk; from ldump.c */
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
void* data, int strip);
#endif #endif

View file

@ -0,0 +1,291 @@
/*
** $Id: lutf8lib.c $
** Standard library for UTF-8 manipulation
** See Copyright Notice in lua.h
*/
#define lutf8lib_c
#define LUA_LIB
#include "lprefix.h"
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#define MAXUNICODE 0x10FFFFu
#define MAXUTF 0x7FFFFFFFu
#define MSGInvalid "invalid UTF-8 code"
/*
** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
*/
#if (UINT_MAX >> 30) >= 1
typedef unsigned int utfint;
#else
typedef unsigned long utfint;
#endif
#define iscont(c) (((c) & 0xC0) == 0x80)
#define iscontp(p) iscont(*(p))
/* from strlib */
/* translate a relative string position: negative means back from end */
static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
if (pos >= 0) return pos;
else if (0u - (size_t)pos > len) return 0;
else return (lua_Integer)len + pos + 1;
}
/*
** Decode one UTF-8 sequence, returning NULL if byte sequence is
** 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, utfint *val, int strict) {
static const utfint limits[] =
{~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
unsigned int c = (unsigned char)s[0];
utfint res = 0; /* final result */
if (c < 0x80) /* ascii? */
res = c;
else {
int count = 0; /* to count number of continuation bytes */
for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */
unsigned int cc = (unsigned char)s[++count]; /* read next byte */
if (!iscont(cc)) /* not a continuation byte? */
return NULL; /* invalid byte sequence */
res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
}
res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */
if (count > 5 || res > MAXUTF || res < limits[count])
return NULL; /* invalid byte sequence */
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;
return s + 1; /* +1 to include first byte */
}
/*
** utf8len(s [, i [, j [, lax]]]) --> number of characters that
** start in the range [i,j], or nil + current position if 's' is not
** well formed in that interval
*/
static int utflen (lua_State *L) {
lua_Integer n = 0; /* counter for the number of characters */
size_t len; /* string length in bytes */
const char *s = luaL_checklstring(L, 1, &len);
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 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,
"initial position out of bounds");
luaL_argcheck(L, --posj < (lua_Integer)len, 3,
"final position out of bounds");
while (posi <= posj) {
const char *s1 = utf8_decode(s + posi, NULL, !lax);
if (s1 == NULL) { /* conversion error? */
luaL_pushfail(L); /* return fail ... */
lua_pushinteger(L, posi + 1); /* ... and current position */
return 2;
}
posi = s1 - s;
n++;
}
lua_pushinteger(L, n);
return 1;
}
/*
** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
** characters that start in the range [i,j]
*/
static int codepoint (lua_State *L) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
int lax = lua_toboolean(L, 4);
int n;
const char *se;
luaL_argcheck(L, posi >= 1, 2, "out of bounds");
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
if (posi > pose) return 0; /* empty interval; return no values */
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
return luaL_error(L, "string slice too long");
n = (int)(pose - posi) + 1; /* upper bound for number of returns */
luaL_checkstack(L, n, "string slice too long");
n = 0; /* count the number of returns */
se = s + pose; /* string end */
for (s += posi - 1; s < se;) {
utfint code;
s = utf8_decode(s, &code, !lax);
if (s == NULL)
return luaL_error(L, MSGInvalid);
lua_pushinteger(L, code);
n++;
}
return n;
}
static void pushutfchar (lua_State *L, int arg) {
lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
lua_pushfstring(L, "%U", (long)code);
}
/*
** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
*/
static int utfchar (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
if (n == 1) /* optimize common case of single char */
pushutfchar(L, 1);
else {
int i;
luaL_Buffer b;
luaL_buffinit(L, &b);
for (i = 1; i <= n; i++) {
pushutfchar(L, i);
luaL_addvalue(&b);
}
luaL_pushresult(&b);
}
return 1;
}
/*
** offset(s, n, [i]) -> index where n-th character counting from
** position 'i' starts; 0 means character at 'i'.
*/
static int byteoffset (lua_State *L) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
lua_Integer n = luaL_checkinteger(L, 2);
lua_Integer posi = (n >= 0) ? 1 : len + 1;
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
"position out of bounds");
if (n == 0) {
/* find beginning of current byte sequence */
while (posi > 0 && iscontp(s + posi)) posi--;
}
else {
if (iscontp(s + posi))
return luaL_error(L, "initial position is a continuation byte");
if (n < 0) {
while (n < 0 && posi > 0) { /* move back */
do { /* find beginning of previous character */
posi--;
} while (posi > 0 && iscontp(s + posi));
n++;
}
}
else {
n--; /* do not move for 1st character */
while (n > 0 && posi < (lua_Integer)len) {
do { /* find beginning of next character */
posi++;
} while (iscontp(s + posi)); /* (cannot pass final '\0') */
n--;
}
}
}
if (n == 0) /* did it find given character? */
lua_pushinteger(L, posi + 1);
else /* no such character */
luaL_pushfail(L);
return 1;
}
static int iter_aux (lua_State *L, int strict) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
if (n < len) {
while (iscontp(s + n)) n++; /* go to next character */
}
if (n >= len) /* (also handles original 'n' being negative) */
return 0; /* no more codepoints */
else {
utfint code;
const char *next = utf8_decode(s + n, &code, strict);
if (next == NULL || iscontp(next))
return luaL_error(L, MSGInvalid);
lua_pushinteger(L, n + 1);
lua_pushinteger(L, code);
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) {
int lax = lua_toboolean(L, 2);
const char *s = luaL_checkstring(L, 1);
luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
lua_pushvalue(L, 1);
lua_pushinteger(L, 0);
return 3;
}
/* pattern to match a single UTF-8 character */
#define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
static const luaL_Reg funcs[] = {
{"offset", byteoffset},
{"codepoint", codepoint},
{"char", utfchar},
{"len", utflen},
{"codes", iter_codes},
/* placeholders */
{"charpattern", NULL},
{NULL, NULL}
};
LUAMOD_API int luaopen_utf8 (lua_State *L) {
luaL_newlib(L, funcs);
lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
lua_setfield(L, -2, "charpattern");
return 1;
}

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.h,v 2.18 2013/01/08 14:06:55 roberto Exp $ ** $Id: lvm.h $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,32 +13,129 @@
#include "ltm.h" #include "ltm.h"
#define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) #if !defined(LUA_NOCVTN2S)
#define cvt2str(o) ttisnumber(o)
#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) #else
#define cvt2str(o) 0 /* no conversion from numbers to strings */
#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) #endif
#define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2)
/* not to called directly */ #if !defined(LUA_NOCVTS2N)
LUAI_FUNC int luaV_equalobj_(lua_State *L, const TValue *t1, const TValue *t2); #define cvt2num(o) ttisstring(o)
#else
#define cvt2num(o) 0 /* no conversion from strings to numbers */
#endif
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); ** You can define LUA_FLOORN2I if you want to convert floats to integers
LUAI_FUNC const TValue *luaV_tonumber(const TValue *obj, TValue *n); ** by flooring them (instead of raising an error if they are not
LUAI_FUNC int luaV_tostring(lua_State *L, StkId obj); ** integral values)
LUAI_FUNC void luaV_gettable(lua_State *L, const TValue *t, TValue *key, */
StkId val); #if !defined(LUA_FLOORN2I)
LUAI_FUNC void luaV_settable(lua_State *L, const TValue *t, TValue *key, #define LUA_FLOORN2I F2Ieq
StkId val); #endif
LUAI_FUNC void luaV_finishOp(lua_State *L);
LUAI_FUNC void luaV_execute(lua_State *L);
LUAI_FUNC void luaV_concat(lua_State *L, int total); /*
LUAI_FUNC void luaV_arith(lua_State *L, StkId ra, const TValue *rb, ** Rounding modes for float->integer coercion
const TValue *rc, TMS op); */
LUAI_FUNC void luaV_objlen(lua_State *L, StkId ra, const TValue *rb); typedef enum {
F2Ieq, /* no rounding; accepts only integral values */
F2Ifloor, /* takes the floor of the number */
F2Iceil /* takes the ceil of the number */
} F2Imod;
/* convert an object to a float (including string coercion) */
#define 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) \
(l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \
: 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 luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
/*
** fast track for 'gettable': if 't' is a table and 't[k]' is present,
** return 1 with 'slot' pointing to 't[k]' (position of final result).
** Otherwise, return 0 (meaning it will have to check metamethod)
** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL
** (otherwise). 'f' is the raw get function to use.
*/
#define luaV_fastget(L,t,k,slot,f) \
(!ttistable(t) \
? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
: (slot = f(hvalue(t), k), /* else, do raw access */ \
!isempty(slot))) /* result not empty? */
/*
** Special case of 'luaV_fastget' for integers, inlining the fast case
** of 'luaH_getint'.
*/
#define luaV_fastgeti(L,t,k,slot) \
(!ttistable(t) \
? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
: (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \
? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \
!isempty(slot))) /* result not empty? */
/*
** Finish a fast set operation (when fast get succeeds). In that case,
** 'slot' points to the place to put the value.
*/
#define luaV_finishfastset(L,t,slot,v) \
{ setobj2t(L, cast(TValue *,slot), 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))
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_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_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode);
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
F2Imod mode);
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode);
LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
StkId val, const TValue *slot);
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
TValue *val, const TValue *slot);
LUAI_FUNC void luaV_finishOp (lua_State *L);
LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci);
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_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 void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
#endif #endif

View file

@ -1,15 +1,17 @@
/* /*
** $Id: lzio.c,v 1.35 2012/05/14 13:34:18 roberto Exp $ ** $Id: lzio.c $
** Buffered streams ** Buffered streams
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <string.h>
#define lzio_c #define lzio_c
#define LUA_CORE #define LUA_CORE
#include "lprefix.h"
#include <string.h>
#include "lua.h" #include "lua.h"
#include "llimits.h" #include "llimits.h"
@ -18,59 +20,49 @@
#include "lzio.h" #include "lzio.h"
int luaZ_fill(ZIO *z) { int luaZ_fill (ZIO *z) {
size_t size; size_t size;
lua_State *L = z->L; lua_State *L = z->L;
const char *buff; const char *buff;
lua_unlock(L); lua_unlock(L);
buff = z->reader(L, z->data, &size); buff = z->reader(L, z->data, &size);
lua_lock(L); lua_lock(L);
if (buff == NULL || size == 0) if (buff == NULL || size == 0)
return EOZ; return EOZ;
z->n = size - 1; /* discount char being returned */ z->n = size - 1; /* discount char being returned */
z->p = buff; z->p = buff;
return cast_uchar(*(z->p++)); return cast_uchar(*(z->p++));
} }
void luaZ_init(lua_State *L, ZIO *z, lua_Reader reader, void *data) { void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
z->L = L; z->L = L;
z->reader = reader; z->reader = reader;
z->data = data; z->data = data;
z->n = 0; z->n = 0;
z->p = NULL; z->p = NULL;
} }
/* --------------------------------------------------------------- read --- */ /* --------------------------------------------------------------- read --- */
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 (z->n == 0) { /* no bytes in buffer? */ if (z->n == 0) { /* no bytes in buffer? */
if (luaZ_fill(z) == EOZ) /* try to read more */ if (luaZ_fill(z) == EOZ) /* try to read more */
return n; /* no more input; return number of missing bytes */ return n; /* no more input; return number of missing bytes */
else { else {
z->n++; /* luaZ_fill consumed first byte; put it back */ z->n++; /* luaZ_fill consumed first byte; put it back */
z->p--; z->p--;
} }
}
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
memcpy(b, z->p, m);
z->n -= m;
z->p += m;
b = (char *)b + m;
n -= m;
} }
return 0; m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
memcpy(b, z->p, m);
z->n -= m;
z->p += m;
b = (char *)b + m;
n -= m;
}
return 0;
} }
/* ------------------------------------------------------------------------ */
char *luaZ_openspace(lua_State *L, Mbuffer *buff, size_t n) {
if (n > buff->buffsize) {
if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
luaZ_resizebuffer(L, buff, n);
}
return buff->buffer;
}

View file

@ -1,5 +1,5 @@
/* /*
** $Id: lzio.h,v 1.26 2011/07/15 12:48:03 roberto Exp $ ** $Id: lzio.h $
** Buffered streams ** Buffered streams
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,7 +13,7 @@
#include "lmem.h" #include "lmem.h"
#define EOZ (-1) /* end of stream */ #define EOZ (-1) /* end of stream */
typedef struct Zio ZIO; typedef struct Zio ZIO;
@ -21,45 +21,46 @@ typedef struct Zio ZIO;
typedef struct Mbuffer { typedef struct Mbuffer {
char *buffer; char *buffer;
size_t n; size_t n;
size_t buffsize; size_t buffsize;
} Mbuffer; } Mbuffer;
#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
#define luaZ_buffer(buff) ((buff)->buffer) #define luaZ_buffer(buff) ((buff)->buffer)
#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 -= (i))
#define luaZ_resetbuffer(buff) ((buff)->n = 0) #define luaZ_resetbuffer(buff) ((buff)->n = 0)
#define luaZ_resizebuffer(L, buff, size) \ #define luaZ_resizebuffer(L, buff, size) \
(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \
(buff)->buffsize = size) (buff)->buffsize, size), \
(buff)->buffsize = size)
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
LUAI_FUNC char *luaZ_openspace(lua_State *L, Mbuffer *buff, size_t n); LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
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 */
/* --------- Private Part ------------------ */ /* --------- Private Part ------------------ */
struct Zio { struct Zio {
size_t n; /* bytes still unread */ size_t n; /* bytes still unread */
const char *p; /* current position in buffer */ const char *p; /* current position in buffer */
lua_Reader reader; /* reader function */ lua_Reader reader; /* reader function */
void *data; /* additional data */ void *data; /* additional data */
lua_State *L; /* Lua state (for reader) */ lua_State *L; /* Lua state (for reader) */
}; };
LUAI_FUNC int luaZ_fill(ZIO *z); LUAI_FUNC int luaZ_fill (ZIO *z);
#endif #endif

View file

@ -7,6 +7,7 @@ add_library(pm3rrg_rdv4_lua STATIC
liblua/ldump.c liblua/ldump.c
liblua/lfunc.c liblua/lfunc.c
liblua/lgc.c liblua/lgc.c
liblua/linit.c
liblua/llex.c liblua/llex.c
liblua/lmem.c liblua/lmem.c
liblua/lobject.c liblua/lobject.c
@ -21,16 +22,15 @@ add_library(pm3rrg_rdv4_lua STATIC
liblua/lzio.c liblua/lzio.c
liblua/lauxlib.c liblua/lauxlib.c
liblua/lbaselib.c liblua/lbaselib.c
liblua/lbitlib.c
liblua/lcorolib.c liblua/lcorolib.c
liblua/ldblib.c liblua/ldblib.c
liblua/liolib.c liblua/liolib.c
liblua/lmathlib.c liblua/lmathlib.c
liblua/loadlib.c
liblua/loslib.c liblua/loslib.c
liblua/lstrlib.c liblua/lstrlib.c
liblua/ltablib.c liblua/ltablib.c
liblua/loadlib.c liblua/lutf8lib.c
liblua/linit.c
) )
target_compile_definitions(pm3rrg_rdv4_lua PRIVATE LUA_COMPAT_ALL) target_compile_definitions(pm3rrg_rdv4_lua PRIVATE LUA_COMPAT_ALL)

View file

@ -1,8 +1,8 @@
#!/bin/bash #!/bin/bash
# pm3.so somewhere in default LUA_CPATH : # pm3.so somewhere in default LUA_CPATH :
# /usr/local/lib/lua/5.2/pm3.so # /usr/local/lib/lua/5.4/pm3.so
# /usr/lib/lua/5.2/pm3.so # /usr/lib/lua/5.4/pm3.so
# ./pm3.so # ./pm3.so
./test.lua ./test.lua

View file

@ -3,8 +3,6 @@ local tostring = tostring
local setmetatable = setmetatable local setmetatable = setmetatable
local schar = string.char local schar = string.char
module 'ansicolors'
local colormt = {} local colormt = {}
function colormt:__tostring() function colormt:__tostring()
@ -57,7 +55,10 @@ local colors = {
onwhite = 47, onwhite = 47,
} }
local Ansicolors = {}
for c, v in pairs(colors) do for c, v in pairs(colors) do
_M[c] = makecolor(v) Ansicolors[c] = makecolor(v)
end end
return Ansicolors

View file

@ -1,23 +1,23 @@
-- Module options: -- Module options:
local always_try_using_lpeg = true local always_use_lpeg = false
local register_global_module_table = false local register_global_module_table = false
local global_module_name = 'json' local global_module_name = 'json'
--[==[ --[==[
David Kolf's JSON module for Lua 5.1/5.2 David Kolf's JSON module for Lua 5.1 - 5.4
Version 2.5 Version 2.8
For the documentation see the corresponding readme.txt or visit For the documentation see the corresponding readme.txt or visit
<http://dkolf.de/src/dkjson-lua.fsl/>. <http://dkolf.de/dkjson-lua/>.
You can contact the author by sending an e-mail to 'david' at the You can contact the author by sending an e-mail to 'david' at the
domain 'dkolf.de'. domain 'dkolf.de'.
Copyright (C) 2010-2013 David Heiko Kolf Copyright (C) 2010-2024 David Heiko Kolf
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
@ -42,8 +42,8 @@ SOFTWARE.
--]==] --]==]
-- global dependencies: -- global dependencies:
local pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset = local pairs, type, tostring, tonumber, getmetatable, setmetatable =
pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset pairs, type, tostring, tonumber, getmetatable, setmetatable
local error, require, pcall, select = error, require, pcall, select local error, require, pcall, select = error, require, pcall, select
local floor, huge = math.floor, math.huge local floor, huge = math.floor, math.huge
local strrep, gsub, strsub, strbyte, strchar, strfind, strlen, strformat = local strrep, gsub, strsub, strbyte, strchar, strfind, strlen, strformat =
@ -52,13 +52,19 @@ local strrep, gsub, strsub, strbyte, strchar, strfind, strlen, strformat =
local strmatch = string.match local strmatch = string.match
local concat = table.concat local concat = table.concat
local json = { version = "dkjson 2.5" } local json = { version = "dkjson 2.8" }
local jsonlpeg = {}
if register_global_module_table then if register_global_module_table then
_G[global_module_name] = json if always_use_lpeg then
_G[global_module_name] = jsonlpeg
else
_G[global_module_name] = json
end
end end
local _ENV = nil -- blocking globals in Lua 5.2 local _ENV = nil -- blocking globals in Lua 5.2 and later
pcall (function() pcall (function()
-- Enable access to blocked metatables. -- Enable access to blocked metatables.
@ -219,6 +225,10 @@ local function addpair (key, value, prev, indent, level, buffer, buflen, tables,
if indent then if indent then
buflen = addnewline2 (level, buffer, buflen) buflen = addnewline2 (level, buffer, buflen)
end end
-- When Lua is compiled with LUA_NOCVTN2S this will fail when
-- numbers are mixed into the keys of the table. JSON keys are always
-- strings, so this would be an implicit conversion too and the failure
-- is intentional.
buffer[buflen+1] = quotestring (key) buffer[buflen+1] = quotestring (key)
buffer[buflen+2] = ":" buffer[buflen+2] = ":"
return encode2 (value, indent, level, buffer, buflen + 2, tables, globalorder, state) return encode2 (value, indent, level, buffer, buflen + 2, tables, globalorder, state)
@ -319,24 +329,25 @@ encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, s
for i = 1, n do for i = 1, n do
local k = order[i] local k = order[i]
local v = value[k] local v = value[k]
if v then if v ~= nil then
used[k] = true used[k] = true
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state) buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state)
prev = true -- add a separator before the next element if not buflen then return nil, msg end
prev = true -- add a seperator before the next element
end end
end end
for k,v in pairs (value) do for k,v in pairs (value) do
if not used[k] then if not used[k] then
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state) buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state)
if not buflen then return nil, msg end if not buflen then return nil, msg end
prev = true -- add a separator before the next element prev = true -- add a seperator before the next element
end end
end end
else -- unordered else -- unordered
for k,v in pairs (value) do for k,v in pairs (value) do
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state) buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state)
if not buflen then return nil, msg end if not buflen then return nil, msg end
prev = true -- add a separator before the next element prev = true -- add a seperator before the next element
end end
end end
if indent then if indent then
@ -385,7 +396,7 @@ local function loc (str, where)
break break
end end
end end
return "line " .. line .. ", column " .. (where - linepos) return strformat ("line %d, column %d", line, where - linepos)
end end
local function unterminated (str, what, where) local function unterminated (str, what, where)
@ -504,7 +515,6 @@ end
local scanvalue -- forward declaration local scanvalue -- forward declaration
local function scantable (what, closechar, str, startpos, nullval, objectmeta, arraymeta) local function scantable (what, closechar, str, startpos, nullval, objectmeta, arraymeta)
local len = strlen (str)
local tbl, n = {}, 0 local tbl, n = {}, 0
local pos = startpos + 1 local pos = startpos + 1
if what == 'object' then if what == 'object' then
@ -600,7 +610,7 @@ end
function json.use_lpeg () function json.use_lpeg ()
local g = require ("lpeg") local g = require ("lpeg")
if g.version() == "0.11" then if type(g.version) == 'function' and g.version() == "0.11" then
error "due to a bug in LPeg 0.11, it cannot be used for JSON matching" error "due to a bug in LPeg 0.11, it cannot be used for JSON matching"
end end
@ -619,10 +629,18 @@ function json.use_lpeg ()
return g.Cmt (g.Cc (msg) * g.Carg (2), ErrorCall) return g.Cmt (g.Cc (msg) * g.Carg (2), ErrorCall)
end end
local function ErrorUnterminatedCall (str, pos, what, state)
return ErrorCall (str, pos - 1, "unterminated " .. what, state)
end
local SingleLineComment = P"//" * (1 - S"\n\r")^0 local SingleLineComment = P"//" * (1 - S"\n\r")^0
local MultiLineComment = P"/*" * (1 - P"*/")^0 * P"*/" local MultiLineComment = P"/*" * (1 - P"*/")^0 * P"*/"
local Space = (S" \n\r\t" + P"\239\187\191" + SingleLineComment + MultiLineComment)^0 local Space = (S" \n\r\t" + P"\239\187\191" + SingleLineComment + MultiLineComment)^0
local function ErrUnterminated (what)
return g.Cmt (g.Cc (what) * g.Carg (2), ErrorUnterminatedCall)
end
local PlainChar = 1 - S"\"\\\n\r" local PlainChar = 1 - S"\"\\\n\r"
local EscapeSequence = (P"\\" * g.C (S"\"\\/bfnrt" + Err "unsupported escape sequence")) / escapechars local EscapeSequence = (P"\\" * g.C (S"\"\\/bfnrt" + Err "unsupported escape sequence")) / escapechars
local HexDigit = R("09", "af", "AF") local HexDigit = R("09", "af", "AF")
@ -640,7 +658,7 @@ function json.use_lpeg ()
local U16Sequence = (P"\\u" * g.C (HexDigit * HexDigit * HexDigit * HexDigit)) local U16Sequence = (P"\\u" * g.C (HexDigit * HexDigit * HexDigit * HexDigit))
local UnicodeEscape = g.Cmt (U16Sequence * U16Sequence, UTF16Surrogate) + U16Sequence/UTF16BMP local UnicodeEscape = g.Cmt (U16Sequence * U16Sequence, UTF16Surrogate) + U16Sequence/UTF16BMP
local Char = UnicodeEscape + EscapeSequence + PlainChar local Char = UnicodeEscape + EscapeSequence + PlainChar
local String = P"\"" * g.Cs (Char ^ 0) * (P"\"" + Err "unterminated string") local String = P"\"" * (g.Cs (Char ^ 0) * P"\"" + ErrUnterminated "string")
local Integer = P"-"^(-1) * (P"0" + (R"19" * R"09"^0)) local Integer = P"-"^(-1) * (P"0" + (R"19" * R"09"^0))
local Fractal = P"." * R"09"^0 local Fractal = P"." * R"09"^0
local Exponent = (S"eE") * (S"+-")^(-1) * R"09"^1 local Exponent = (S"eE") * (S"+-")^(-1) * R"09"^1
@ -653,41 +671,62 @@ function json.use_lpeg ()
-- at a time and store them directly to avoid hitting the LPeg limits. -- at a time and store them directly to avoid hitting the LPeg limits.
local function parsearray (str, pos, nullval, state) local function parsearray (str, pos, nullval, state)
local obj, cont local obj, cont
local start = pos
local npos local npos
local t, nt = {}, 0 local t, nt = {}, 0
repeat repeat
obj, cont, npos = pegmatch (ArrayContent, str, pos, nullval, state) obj, cont, npos = pegmatch (ArrayContent, str, pos, nullval, state)
if not npos then break end if cont == 'end' then
return ErrorUnterminatedCall (str, start, "array", state)
end
pos = npos pos = npos
nt = nt + 1 if cont == 'cont' or cont == 'last' then
t[nt] = obj nt = nt + 1
until cont == 'last' t[nt] = obj
end
until cont ~= 'cont'
return pos, setmetatable (t, state.arraymeta) return pos, setmetatable (t, state.arraymeta)
end end
local function parseobject (str, pos, nullval, state) local function parseobject (str, pos, nullval, state)
local obj, key, cont local obj, key, cont
local start = pos
local npos local npos
local t = {} local t = {}
repeat repeat
key, obj, cont, npos = pegmatch (ObjectContent, str, pos, nullval, state) key, obj, cont, npos = pegmatch (ObjectContent, str, pos, nullval, state)
if not npos then break end if cont == 'end' then
return ErrorUnterminatedCall (str, start, "object", state)
end
pos = npos pos = npos
t[key] = obj if cont == 'cont' or cont == 'last' then
until cont == 'last' t[key] = obj
end
until cont ~= 'cont'
return pos, setmetatable (t, state.objectmeta) return pos, setmetatable (t, state.objectmeta)
end end
local Array = P"[" * g.Cmt (g.Carg(1) * g.Carg(2), parsearray) * Space * (P"]" + Err "']' expected") local Array = P"[" * g.Cmt (g.Carg(1) * g.Carg(2), parsearray)
local Object = P"{" * g.Cmt (g.Carg(1) * g.Carg(2), parseobject) * Space * (P"}" + Err "'}' expected") local Object = P"{" * g.Cmt (g.Carg(1) * g.Carg(2), parseobject)
local Value = Space * (Array + Object + SimpleValue) local Value = Space * (Array + Object + SimpleValue)
local ExpectedValue = Value + Space * Err "value expected" local ExpectedValue = Value + Space * Err "value expected"
ArrayContent = Value * Space * (P"," * g.Cc'cont' + g.Cc'last') * g.Cp() local ExpectedKey = String + Err "key expected"
local Pair = g.Cg (Space * String * Space * (P":" + Err "colon expected") * ExpectedValue) local End = P(-1) * g.Cc'end'
ObjectContent = Pair * Space * (P"," * g.Cc'cont' + g.Cc'last') * g.Cp() local ErrInvalid = Err "invalid JSON"
ArrayContent = (Value * Space * (P"," * g.Cc'cont' + P"]" * g.Cc'last'+ End + ErrInvalid) + g.Cc(nil) * (P"]" * g.Cc'empty' + End + ErrInvalid)) * g.Cp()
local Pair = g.Cg (Space * ExpectedKey * Space * (P":" + Err "colon expected") * ExpectedValue)
ObjectContent = (g.Cc(nil) * g.Cc(nil) * P"}" * g.Cc'empty' + End + (Pair * Space * (P"," * g.Cc'cont' + P"}" * g.Cc'last' + End + ErrInvalid) + ErrInvalid)) * g.Cp()
local DecodeValue = ExpectedValue * g.Cp () local DecodeValue = ExpectedValue * g.Cp ()
function json.decode (str, pos, nullval, ...) jsonlpeg.version = json.version
jsonlpeg.encode = json.encode
jsonlpeg.null = json.null
jsonlpeg.quotestring = json.quotestring
jsonlpeg.addnewline = json.addnewline
jsonlpeg.encodeexception = json.encodeexception
jsonlpeg.using_lpeg = true
function jsonlpeg.decode (str, pos, nullval, ...)
local state = {} local state = {}
state.objectmeta, state.arraymeta = optionalmetatables(...) state.objectmeta, state.arraymeta = optionalmetatables(...)
local obj, retpos = pegmatch (DecodeValue, str, pos, nullval, state) local obj, retpos = pegmatch (DecodeValue, str, pos, nullval, state)
@ -698,16 +737,15 @@ function json.use_lpeg ()
end end
end end
-- use this function only once: -- cache result of this function:
json.use_lpeg = function () return json end json.use_lpeg = function () return jsonlpeg end
jsonlpeg.use_lpeg = json.use_lpeg
json.using_lpeg = true return jsonlpeg
return json -- so you can get the module using json = require "dkjson".use_lpeg()
end end
if always_try_using_lpeg then if always_use_lpeg then
pcall (json.use_lpeg) return json.use_lpeg()
end end
return json return json

View file

@ -119,7 +119,7 @@ local function display_results(keys)
print('|---|----------------|---|----------------|---|') print('|---|----------------|---|----------------|---|')
for sector = 0, #keys do for sector = 0, #keys do
succA, succB, keyA, keyB = unpack(keys[sector]) succA, succB, keyA, keyB = table.unpack(keys[sector])
print(('|%03d| %s | %s | %s | %s |'):format(sector, keyA, succA, keyB, succB)) print(('|%03d| %s | %s | %s | %s |'):format(sector, keyA, succA, keyB, succB))
end end
print('|---|----------------|---|----------------|---|') print('|---|----------------|---|----------------|---|')
@ -180,7 +180,7 @@ local function dumptofile(uid, keys)
--for sector,_ in pairs(keys) do --for sector,_ in pairs(keys) do
for sector = 0, #keys do for sector = 0, #keys do
local succA, succB, keyA, keyB = unpack(keys[sector]) local succA, succB, keyA, keyB = table.unpack(keys[sector])
key_a = key_a .. bin.pack('H', keyA); key_a = key_a .. bin.pack('H', keyA);
key_b = key_b .. bin.pack('H', keyB); key_b = key_b .. bin.pack('H', keyB);
end end
@ -222,7 +222,7 @@ local function perform_check(uid, numsectors)
local targetblock = tonumber(get_blockno(sector), 16) local targetblock = tonumber(get_blockno(sector), 16)
local succA, succB, keyA, keyB = unpack(keys[sector]) local succA, succB, keyA, keyB = table.unpack(keys[sector])
local keyA = checkBlock(targetblock, keylist, 0) local keyA = checkBlock(targetblock, keylist, 0)
if keyA then succA = 1; keylist = placeFirst(keyA, keylist) end if keyA then succA = 1; keylist = placeFirst(keyA, keylist) end

View file

@ -94,7 +94,7 @@ local function dumptofile(uid, keys)
local key_b = '' local key_b = ''
for sector = 0, #keys do for sector = 0, #keys do
local keyA, keyB = unpack(keys[sector]) local keyA, keyB = table.unpack(keys[sector])
key_a = key_a .. bin.pack('H', keyA); key_a = key_a .. bin.pack('H', keyA);
key_b = key_b .. bin.pack('H', keyB); key_b = key_b .. bin.pack('H', keyB);
end end
@ -124,7 +124,7 @@ local function printKeys(keys)
print('|sec|key A |res|key B |res|') print('|sec|key A |res|key B |res|')
print('|---|----------------|---|----------------|---|') print('|---|----------------|---|----------------|---|')
for sector = 0, #keys do for sector = 0, #keys do
local keyA, keyB = unpack(keys[sector]) local keyA, keyB = table.unpack(keys[sector])
print(('|%03d| %s | %s | %s | %s |'):format(sector, keyA, 1, keyB, 1)) print(('|%03d| %s | %s | %s | %s |'):format(sector, keyA, 1, keyB, 1))
end end
print('|---|----------------|---|----------------|---|') print('|---|----------------|---|----------------|---|')

View file

@ -97,7 +97,7 @@ local function dumptofile(uid, keys)
local key_b = '' local key_b = ''
for sector = 0, #keys do for sector = 0, #keys do
local keyA, keyB = unpack(keys[sector]) local keyA, keyB = table.unpack(keys[sector])
key_a = key_a .. bin.pack('H', keyA); key_a = key_a .. bin.pack('H', keyA);
key_b = key_b .. bin.pack('H', keyB); key_b = key_b .. bin.pack('H', keyB);
end end
@ -115,6 +115,7 @@ end
-- create key -- create key
local function calckey(uid, xorkey, keytype) local function calckey(uid, xorkey, keytype)
local p1,p2,p3,p4,p5,p6 local p1,p2,p3,p4,p5,p6
print("calckey()")
if keytype == 'A' then if keytype == 'A' then
p1 = bxor( uid[1], xorkey[1]) p1 = bxor( uid[1], xorkey[1])
p2 = bxor( uid[2], xorkey[2]) p2 = bxor( uid[2], xorkey[2])
@ -139,7 +140,7 @@ local function printKeys(keys)
print('|sec|key A |res|key B |res|') print('|sec|key A |res|key B |res|')
print('|---|----------------|---|----------------|---|') print('|---|----------------|---|----------------|---|')
for sector = 0, #keys do for sector = 0, #keys do
local keyA, keyB = unpack(keys[sector]) local keyA, keyB = table.unpack(keys[sector])
print(('|%03d| %s | %s | %s | %s |'):format(sector, keyA, 1, keyB, 1)) print(('|%03d| %s | %s | %s | %s |'):format(sector, keyA, 1, keyB, 1))
end end
print('|---|----------------|---|----------------|---|') print('|---|----------------|---|----------------|---|')

View file

@ -2,49 +2,44 @@
** $Id: lbitlib.c,v 1.18 2013/03/19 13:19:12 roberto Exp $ ** $Id: lbitlib.c,v 1.18 2013/03/19 13:19:12 roberto Exp $
** Standard library for bitwise operations ** Standard library for bitwise operations
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
**
** Patched to provide a compatibility layer with Lua5.3+ where
** bit32 library was removed.
*/ */
#define lbitlib_c #include "lua_bitlib.h"
#define LUA_LIB
#include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lualib.h"
/* number of bits to consider in a number */ /* number of bits to consider in a number */
#if !defined(LUA_NBITS) #define LUA_BITLIB_NBITS 32
#define LUA_NBITS 32
#endif
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_BITLIB_NBITS - 1)) << 1))
/* macro to trim extra bits */ /* macro to trim extra bits */
#define trim(x) ((x) & ALLONES) #define trim(x) ((x) & ALLONES)
/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ /* builds a number with 'n' ones (1 <= n <= LUA_BITLIB_NBITS) */
#define mask(n) (~((ALLONES << 1) << ((n) - 1))) #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
typedef lua_Unsigned b_uint; typedef lua_Unsigned b_uint;
static b_uint andaux(lua_State *L) { static b_uint andaux(lua_State *L) {
int i, n = lua_gettop(L); int i, n = lua_gettop(L);
b_uint r = ~(b_uint)0; b_uint r = ~(b_uint)0;
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
r &= luaL_checkunsigned(L, i); r &= (b_uint)luaL_checkinteger(L, i);
return trim(r); return trim(r);
} }
static int b_and(lua_State *L) { static int b_and(lua_State *L) {
b_uint r = andaux(L); b_uint r = andaux(L);
lua_pushunsigned(L, r); lua_pushinteger(L, r);
return 1; return 1;
} }
@ -60,8 +55,8 @@ static int b_or(lua_State *L) {
int i, n = lua_gettop(L); int i, n = lua_gettop(L);
b_uint r = 0; b_uint r = 0;
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
r |= luaL_checkunsigned(L, i); r |= (b_uint) luaL_checkinteger(L, i);
lua_pushunsigned(L, trim(r)); lua_pushinteger(L, trim(r));
return 1; return 1;
} }
@ -70,15 +65,15 @@ static int b_xor(lua_State *L) {
int i, n = lua_gettop(L); int i, n = lua_gettop(L);
b_uint r = 0; b_uint r = 0;
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
r ^= luaL_checkunsigned(L, i); r ^= (b_uint) luaL_checkinteger(L, i);
lua_pushunsigned(L, trim(r)); lua_pushinteger(L, trim(r));
return 1; return 1;
} }
static int b_not(lua_State *L) { static int b_not(lua_State *L) {
b_uint r = ~luaL_checkunsigned(L, 1); b_uint r = ~((b_uint)luaL_checkinteger(L, 1));
lua_pushunsigned(L, trim(r)); lua_pushinteger(L, trim(r));
return 1; return 1;
} }
@ -87,62 +82,62 @@ static int b_shift(lua_State *L, b_uint r, int i) {
if (i < 0) { /* shift right? */ if (i < 0) { /* shift right? */
i = -i; i = -i;
r = trim(r); r = trim(r);
if (i >= LUA_NBITS) r = 0; if (i >= LUA_BITLIB_NBITS) r = 0;
else r >>= i; else r >>= i;
} else { /* shift left */ } else { /* shift left */
if (i >= LUA_NBITS) r = 0; if (i >= LUA_BITLIB_NBITS) r = 0;
else r <<= i; else r <<= i;
r = trim(r); r = trim(r);
} }
lua_pushunsigned(L, r); lua_pushinteger(L, r);
return 1; return 1;
} }
static int b_lshift(lua_State *L) { static int b_lshift(lua_State *L) {
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); return b_shift(L, (b_uint)luaL_checkinteger(L, 1), luaL_checkinteger(L, 2));
} }
static int b_rshift(lua_State *L) { static int b_rshift(lua_State *L) {
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); return b_shift(L, (b_uint)luaL_checkinteger(L, 1), -luaL_checkinteger(L, 2));
} }
static int b_arshift(lua_State *L) { static int b_arshift(lua_State *L) {
b_uint r = luaL_checkunsigned(L, 1); b_uint r = (b_uint)luaL_checkinteger(L, 1);
int i = luaL_checkint(L, 2); int i = luaL_checkinteger(L, 2);
if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) if (i < 0 || !(r & ((b_uint)1 << (LUA_BITLIB_NBITS - 1))))
return b_shift(L, r, -i); return b_shift(L, r, -i);
else { /* arithmetic shift for 'negative' number */ else { /* arithmetic shift for 'negative' number */
if (i >= LUA_NBITS) r = ALLONES; if (i >= LUA_BITLIB_NBITS) r = ALLONES;
else else
r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
lua_pushunsigned(L, r); lua_pushinteger(L, r);
return 1; return 1;
} }
} }
static int b_rot(lua_State *L, int i) { static int b_rot(lua_State *L, int i) {
b_uint r = luaL_checkunsigned(L, 1); b_uint r = (b_uint)luaL_checkinteger(L, 1);
i &= (LUA_NBITS - 1); /* i = i % NBITS */ i &= (LUA_BITLIB_NBITS - 1); /* i = i % NBITS */
r = trim(r); r = trim(r);
if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ if (i != 0) /* avoid undefined shift of LUA_BITLIB_NBITS when i == 0 */
r = (r << i) | (r >> (LUA_NBITS - i)); r = (r << i) | (r >> (LUA_BITLIB_NBITS - i));
lua_pushunsigned(L, trim(r)); lua_pushinteger(L, trim(r));
return 1; return 1;
} }
static int b_lrot(lua_State *L) { static int b_lrot(lua_State *L) {
return b_rot(L, luaL_checkint(L, 2)); return b_rot(L, luaL_checkinteger(L, 2));
} }
static int b_rrot(lua_State *L) { static int b_rrot(lua_State *L) {
return b_rot(L, -luaL_checkint(L, 2)); return b_rot(L, -luaL_checkinteger(L, 2));
} }
@ -153,11 +148,11 @@ static int b_rrot(lua_State *L) {
** 'width' being used uninitialized.) ** 'width' being used uninitialized.)
*/ */
static int fieldargs(lua_State *L, int farg, int *width) { static int fieldargs(lua_State *L, int farg, int *width) {
int f = luaL_checkint(L, farg); int f = luaL_checkinteger(L, farg);
int w = luaL_optint(L, farg + 1, 1); int w = luaL_optinteger(L, farg + 1, 1);
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
if (f + w > LUA_NBITS) if (f + w > LUA_BITLIB_NBITS)
luaL_error(L, "trying to access non-existent bits"); luaL_error(L, "trying to access non-existent bits");
*width = w; *width = w;
return f; return f;
@ -166,28 +161,29 @@ static int fieldargs(lua_State *L, int farg, int *width) {
static int b_extract(lua_State *L) { static int b_extract(lua_State *L) {
int w; int w;
b_uint r = luaL_checkunsigned(L, 1); b_uint r = (b_uint)luaL_checkinteger(L, 1);
int f = fieldargs(L, 2, &w); int f = fieldargs(L, 2, &w);
r = (r >> f) & mask(w); r = (r >> f) & mask(w);
lua_pushunsigned(L, r); lua_pushinteger(L, r);
return 1; return 1;
} }
static int b_replace(lua_State *L) { static int b_replace(lua_State *L) {
int w; int w;
b_uint r = luaL_checkunsigned(L, 1); b_uint r = (b_uint)luaL_checkinteger(L, 1);
b_uint v = luaL_checkunsigned(L, 2); b_uint v = (b_uint)luaL_checkinteger(L, 2);
int f = fieldargs(L, 3, &w); int f = fieldargs(L, 3, &w);
int m = mask(w); int m = mask(w);
v &= m; /* erase bits outside given width */ v &= m; /* erase bits outside given width */
r = (r & ~(m << f)) | (v << f); r = (r & ~(m << f)) | (v << f);
lua_pushunsigned(L, r); lua_pushinteger(L, r);
return 1; return 1;
} }
static const luaL_Reg bitlib[] = { void register_bit32_lib(lua_State *L) {
static const luaL_Reg bitlib[] = {
{"arshift", b_arshift}, {"arshift", b_arshift},
{"band", b_and}, {"band", b_and},
{"bnot", b_not}, {"bnot", b_not},
@ -201,12 +197,9 @@ static const luaL_Reg bitlib[] = {
{"rrotate", b_rrot}, {"rrotate", b_rrot},
{"rshift", b_rshift}, {"rshift", b_rshift},
{NULL, NULL} {NULL, NULL}
}; };
luaL_newlib(L, bitlib);
lua_setfield(L, -2, "bit32");
LUAMOD_API int luaopen_bit32(lua_State *L) {
luaL_newlib(L, bitlib);
return 1;
} }

25
client/src/lua_bitlib.h Normal file
View file

@ -0,0 +1,25 @@
//-----------------------------------------------------------------------------
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
// Bitwise manipulation library which got deprecated in lua5.3
//-----------------------------------------------------------------------------
#ifndef LUA_BITLIB_H__
#define LUA_BITLIB_H__
#include <lua.h>
void register_bit32_lib(lua_State *L);
#endif

File diff suppressed because it is too large Load diff

View file

@ -22,6 +22,7 @@
#include <errno.h> #include <errno.h>
#include "lauxlib.h" #include "lauxlib.h"
#include "lua_bitlib.h"
#include "cmdmain.h" #include "cmdmain.h"
#include "proxmark3.h" #include "proxmark3.h"
#include "comms.h" #include "comms.h"
@ -320,7 +321,7 @@ static int l_GetFromFlashMemSpiffs(lua_State *L) {
} }
lua_pushlstring(L, (const char *)data, len); lua_pushlstring(L, (const char *)data, len);
lua_pushunsigned(L, len); lua_pushinteger(L, len);
free(data); free(data);
return 2; return 2;
} }
@ -344,11 +345,11 @@ static int l_WaitForResponseTimeout(lua_State *L) {
// extract first param. cmd byte to look for // extract first param. cmd byte to look for
if (n >= 1) if (n >= 1)
cmd = luaL_checkunsigned(L, 1); cmd = (uint32_t)luaL_checkinteger(L, 1);
// extract second param. timeout value // extract second param. timeout value
if (n >= 2) if (n >= 2)
ms_timeout = luaL_checkunsigned(L, 2); ms_timeout = luaL_checkinteger(L, 2);
PacketResponseNG resp; PacketResponseNG resp;
if (WaitForResponseTimeout(cmd, &resp, ms_timeout) == false) { if (WaitForResponseTimeout(cmd, &resp, ms_timeout) == false) {
@ -643,7 +644,7 @@ static int l_crc8legic(lua_State *L) {
size_t size; size_t size;
const char *p_hexstr = luaL_checklstring(L, 1, &size); const char *p_hexstr = luaL_checklstring(L, 1, &size);
uint16_t retval = CRC8Legic((uint8_t *)p_hexstr, size); uint16_t retval = CRC8Legic((uint8_t *)p_hexstr, size);
lua_pushunsigned(L, retval); lua_pushinteger(L, retval);
return 1; return 1;
} }
@ -659,7 +660,7 @@ static int l_crc16legic(lua_State *L) {
init_table(CRC_LEGIC_16); init_table(CRC_LEGIC_16);
uint16_t retval = crc16_legic((uint8_t *)p_hexstr, hexsize, uidcrc); uint16_t retval = crc16_legic((uint8_t *)p_hexstr, hexsize, uidcrc);
lua_pushunsigned(L, retval); lua_pushinteger(L, retval);
return 1; return 1;
} }
@ -669,7 +670,7 @@ static int l_crc16(lua_State *L) {
const char *p_str = luaL_checklstring(L, 1, &size); const char *p_str = luaL_checklstring(L, 1, &size);
uint16_t checksum = Crc16ex(CRC_CCITT, (uint8_t *) p_str, size); uint16_t checksum = Crc16ex(CRC_CCITT, (uint8_t *) p_str, size);
lua_pushunsigned(L, checksum); lua_pushinteger(L, checksum);
return 1; return 1;
} }
@ -735,7 +736,7 @@ static int l_reveng_models(lua_State *L) {
#define NMODELS 106 #define NMODELS 106
int count = 0; int count = 0;
uint8_t in_width = luaL_checkunsigned(L, 1); uint8_t in_width = (uint8_t)luaL_checkinteger(L, 1);
if (in_width > 89) if (in_width > 89)
return returnToLuaWithError(L, "Width cannot exceed 89, got %d", in_width); return returnToLuaWithError(L, "Width cannot exceed 89, got %d", in_width);
@ -916,8 +917,8 @@ static int l_keygen_algoB(lua_State *L) {
uint32_t pwd = ul_ev1_pwdgenB(uid); uint32_t pwd = ul_ev1_pwdgenB(uid);
uint16_t pack = ul_ev1_packgenB(uid); uint16_t pack = ul_ev1_packgenB(uid);
lua_pushunsigned(L, pwd); lua_pushinteger(L, pwd);
lua_pushunsigned(L, pack); lua_pushinteger(L, pack);
return 2; return 2;
} }
@ -949,8 +950,8 @@ static int l_keygen_algoD(lua_State *L) {
uint32_t pwd = ul_ev1_pwdgenD(uid); uint32_t pwd = ul_ev1_pwdgenD(uid);
uint16_t pack = ul_ev1_packgenD(uid); uint16_t pack = ul_ev1_packgenD(uid);
lua_pushunsigned(L, pwd); lua_pushinteger(L, pwd);
lua_pushunsigned(L, pack); lua_pushinteger(L, pack);
return 2; return 2;
} }
@ -1037,7 +1038,7 @@ static int l_T55xx_readblock(lua_State *L) {
return returnToLuaWithError(L, "Failed to get actual data"); return returnToLuaWithError(L, "Failed to get actual data");
} }
lua_pushunsigned(L, blockData); lua_pushinteger(L, blockData);
return 1; return 1;
} }
@ -1436,16 +1437,12 @@ int set_pm3_libraries(lua_State *L) {
}; };
lua_pushglobaltable(L); lua_pushglobaltable(L);
// Core library is in this table. Contains '
// this is 'pm3' table
lua_newtable(L);
// put the function into the hash table. // bit32 compatibility shim
for (int i = 0; libs[i].name; i++) { register_bit32_lib(L);
lua_pushcfunction(L, libs[i].func);
lua_setfield(L, -2, libs[i].name); // set the name, pop stack // Core module
} luaL_newlib(L, libs);
// Name of 'core'
lua_setfield(L, -2, "core"); lua_setfield(L, -2, "core");
// remove the global environment table from the stack // remove the global environment table from the stack