mirror of
https://github.com/lua/lua
synced 2026-06-08 15:34:49 +00:00
Compare commits
215 Commits
v4.0-alpha
...
v4.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 523c5d8e1c | |||
| 40a4c76773 | |||
| 1385d81d20 | |||
| 906d5dcc41 | |||
| 93a5649d40 | |||
| df416661cc | |||
| 67c1afff59 | |||
| 03770ecfc9 | |||
| 7409678b5d | |||
| d1c689af40 | |||
| 37e9c2e744 | |||
| e42a219eeb | |||
| b3959d58ff | |||
| f379d06e24 | |||
| 728ff20701 | |||
| 2cbbf3933a | |||
| e2b6b7de1b | |||
| 563b1f5704 | |||
| 4670476584 | |||
| 89f98c0995 | |||
| b892f0a877 | |||
| aadc35449e | |||
| cdc8139e29 | |||
| e833bd47c9 | |||
| b7ffb128cb | |||
| 64eecc0b82 | |||
| 8b88ab07f7 | |||
| 2779ceeb12 | |||
| e93c4547fe | |||
| 0ffc676ce7 | |||
| 18fb3ddb89 | |||
| 2bddbe6603 | |||
| e323338fd0 | |||
| 46b543ebef | |||
| 79909a92e1 | |||
| d6232a0b2e | |||
| ae63a0e692 | |||
| cd3d446957 | |||
| f01e6c6f1d | |||
| ad3816d0d1 | |||
| 046a3d6173 | |||
| 001f2bdd0e | |||
| cd2ddaded9 | |||
| d68209e822 | |||
| 1088cde03c | |||
| 6759f3ec5e | |||
| f6834f4393 | |||
| 78bc8e553d | |||
| dad808a73a | |||
| ca7fd50a4e | |||
| 282ab366f4 | |||
| 444d6a106b | |||
| 13635f7de7 | |||
| d8a442206d | |||
| c9c6f9747c | |||
| c2aa7bd72d | |||
| f9dd50cefc | |||
| dbde9f8817 | |||
| 857a1204d8 | |||
| d742b88fa1 | |||
| eb822c314a | |||
| ab7aceb980 | |||
| 72b12e0956 | |||
| 5ed3bcd4ea | |||
| 7508fed673 | |||
| aab4fde468 | |||
| 6ec4a0ef76 | |||
| 42ca3105ac | |||
| 6030d5fed4 | |||
| 91c003dcc2 | |||
| 620d22f2a0 | |||
| f45cba42b5 | |||
| 2628a42480 | |||
| 0b3b6850c9 | |||
| 5d033f33eb | |||
| e143fed484 | |||
| 01ce1ce48c | |||
| 024ed4218f | |||
| c1f725ba4a | |||
| 5d69960734 | |||
| d1c351857d | |||
| 4ef14ba280 | |||
| b8d57092eb | |||
| 0f59e3d7a7 | |||
| 55c3bc2bcb | |||
| 8060193702 | |||
| 2779e81fbb | |||
| 30f6e658d2 | |||
| 787a78f83e | |||
| 70c8a31092 | |||
| 2018380e9f | |||
| 6e80c1cde1 | |||
| f67f324377 | |||
| eebc9729e4 | |||
| 6990b06467 | |||
| cad91499dd | |||
| ebdd4e8451 | |||
| 9a21e81907 | |||
| f0b3cd1d6f | |||
| fb5e6d5ac4 | |||
| d63afba9d3 | |||
| 100bfec39a | |||
| a290b84c67 | |||
| 1022b3c85e | |||
| a1d3e001b9 | |||
| 10b4799ce5 | |||
| a0de89d62a | |||
| 21dc77b2af | |||
| 2d3ebba537 | |||
| a97f29f154 | |||
| 4135f4f586 | |||
| 808525e417 | |||
| 48a968e6b5 | |||
| 58adbde216 | |||
| 9e74ca62d7 | |||
| d396562b5e | |||
| 9d60598260 | |||
| 4e56c0d514 | |||
| ac12f4db4b | |||
| b691d4344b | |||
| 9fdf73bc9a | |||
| f1fd9b5c2c | |||
| 3723d790ae | |||
| b800c38b69 | |||
| 5c0e79847c | |||
| e662e0f1cd | |||
| c85162be27 | |||
| bd39db46ed | |||
| 90fb2e18e8 | |||
| 4365c31c89 | |||
| 44eb7d91ac | |||
| ddc8d94a08 | |||
| 5d9cbdadfb | |||
| 58453dc1e1 | |||
| f555e493f0 | |||
| d1ffd1a1c5 | |||
| 9e84bf18db | |||
| daf09c476f | |||
| e238efc536 | |||
| fdd04e7a7f | |||
| fb55c96706 | |||
| c39bec81dc | |||
| 5f22f8961c | |||
| a7c1390ffa | |||
| 0802a9df9e | |||
| f90bc248b3 | |||
| d9e61e8cea | |||
| 397905ef86 | |||
| 435f587ed0 | |||
| ae55f3eead | |||
| cfba572076 | |||
| aa01d2568d | |||
| 014a09c509 | |||
| b622282973 | |||
| 7f867eb0b9 | |||
| 5362426ffa | |||
| 906434011f | |||
| c07cebbbf6 | |||
| afef009fce | |||
| b69e712713 | |||
| f517759507 | |||
| 52062684e1 | |||
| 83e3ec0117 | |||
| feb2083730 | |||
| 8d2baf719c | |||
| 88657809ac | |||
| 2e1b32d873 | |||
| 89a3ec08f3 | |||
| e3ea307434 | |||
| 18b0e8270d | |||
| 094880ef9e | |||
| 1de5587184 | |||
| 8ca9534d04 | |||
| 8bcf622876 | |||
| dbbf6c073b | |||
| 292c953018 | |||
| c542aac0b9 | |||
| dbfb810267 | |||
| 9c14b5e416 | |||
| 6b9bf49265 | |||
| 989ad7232a | |||
| 190c3be739 | |||
| 7e30900def | |||
| f63d7753b8 | |||
| 50a82ec1b9 | |||
| ed1751bc32 | |||
| 8e617985fa | |||
| c39345fba3 | |||
| ed94a85f0c | |||
| 58fbdc76d5 | |||
| a301304612 | |||
| 677313da32 | |||
| ef62b340e0 | |||
| 5c2dd7a9e0 | |||
| 93d93a0bfb | |||
| 9e1f94fc1c | |||
| 8714cc02d6 | |||
| 69b45bb4e9 | |||
| 843f84f4ce | |||
| b803c0600e | |||
| 8ac0bbf64b | |||
| dc1e4f5073 | |||
| 330e51bed3 | |||
| 44b71ca816 | |||
| bad6365540 | |||
| 91f34fb05c | |||
| 11a7022067 | |||
| 35a6ed2838 | |||
| cfe6ca819b | |||
| fe5c37ae95 | |||
| c31f02948f | |||
| c9f91c6fe3 | |||
| 66d620674c | |||
| 84790bb953 | |||
| 534c3a64d3 |
@@ -170,3 +170,68 @@ in the following platforms:
|
||||
* IRIX - cc, cc-purify
|
||||
* Windows - Visual C++ (.c e .cpp, warning level=4)
|
||||
|
||||
|
||||
** lstrlib.c
|
||||
Tue May 2 15:27:58 EST 2000
|
||||
>> `strfind' gets wrong subject length when there is an offset
|
||||
(by Jon Kleiser; since 4.0a)
|
||||
|
||||
** lparser.c
|
||||
Fri May 12 15:11:12 EST 2000
|
||||
>> first element in a list constructor is not adjusted to one value
|
||||
>> (e.g. «a = {gsub('a','a','')}»)
|
||||
(by Tomas; since 4.0a)
|
||||
|
||||
** lparser.c
|
||||
Wed May 24 14:50:16 EST 2000
|
||||
>> record-constructor starting with an upvalue name gets an error
|
||||
>> (e.g. «local a; function f() x = {a=1} end»)
|
||||
(by Edgar Toernig; since 3.1)
|
||||
|
||||
** lparser.c
|
||||
Tue Aug 29 15:56:05 EST 2000
|
||||
>> error message for `for' uses `while'
|
||||
(since 4.0a; already corrected)
|
||||
|
||||
** lgc.c
|
||||
Tue Aug 29 15:57:41 EST 2000
|
||||
>> gc tag method for nil could call line hook
|
||||
(by ry; since ?)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0 Beta
|
||||
|
||||
** liolib.c
|
||||
Fri Sep 22 15:12:37 EST 2000
|
||||
>> `read("*w")' should return nil at EOF
|
||||
(by roberto; since 4.0b)
|
||||
|
||||
** lvm.c
|
||||
Mon Sep 25 11:47:48 EST 2000
|
||||
>> lua_gettable does not get key from stack top
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lgc.c
|
||||
Mon Sep 25 11:50:48 EST 2000
|
||||
>> GC may crash when checking locked C closures
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lapi.c
|
||||
Wed Sep 27 09:50:19 EST 2000
|
||||
>> lua_tag should return LUA_NOTAG for non-valid indices
|
||||
(by Paul Hankin; since 4.0b)
|
||||
|
||||
** llex.h / llex.c / lparser.c
|
||||
Wed Sep 27 13:39:45 EST 2000
|
||||
>> parser overwrites semantic information when looking ahead
|
||||
>> (e.g. «a = {print'foo'}»)
|
||||
(by Edgar Toernig; since 4.0b, deriving from previous bug)
|
||||
|
||||
** liolib.c
|
||||
Thu Oct 26 10:50:46 EDT 2000
|
||||
>> in function `read_file', realloc() doesn't free the buffer if it can't
|
||||
>> allocate new memory
|
||||
(by Mauro Vezzosi; since 4.0b)
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 1.77 2000/03/29 20:19:20 roberto Exp roberto $
|
||||
** $Id: lapi.c,v 1.109 2000/10/26 12:47:05 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,21 +7,18 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lref.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
@@ -30,364 +27,468 @@ const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
||||
|
||||
|
||||
|
||||
void luaA_checkCargs (lua_State *L, int nargs) {
|
||||
if (nargs > L->top-L->Cstack.base)
|
||||
luaL_verror(L, "Lua API error - "
|
||||
"expected at least %d arguments in C2lua stack", nargs);
|
||||
#define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
|
||||
|
||||
#define api_incr_top(L) incr_top
|
||||
|
||||
|
||||
|
||||
|
||||
TObject *luaA_index (lua_State *L, int index) {
|
||||
return Index(L, index);
|
||||
}
|
||||
|
||||
|
||||
lua_Object luaA_putluaObject (lua_State *L, const TObject *o) {
|
||||
luaD_openstack(L, L->Cstack.base);
|
||||
*L->Cstack.base++ = *o;
|
||||
return L->Cstack.base-1;
|
||||
}
|
||||
|
||||
|
||||
lua_Object luaA_putObjectOnTop (lua_State *L) {
|
||||
luaD_openstack(L, L->Cstack.base);
|
||||
*L->Cstack.base++ = *(--L->top);
|
||||
return L->Cstack.base-1;
|
||||
}
|
||||
|
||||
|
||||
static void top2LC (lua_State *L, int n) {
|
||||
/* Put the `n' elements on the top as the Lua2C contents */
|
||||
L->Cstack.base = L->top; /* new base */
|
||||
L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */
|
||||
L->Cstack.num = n; /* number of results */
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_pop (lua_State *L) {
|
||||
luaA_checkCargs(L, 1);
|
||||
return luaA_putObjectOnTop(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
|
||||
** `number' must be 1 to get the first parameter.
|
||||
*/
|
||||
lua_Object lua_lua2C (lua_State *L, int number) {
|
||||
if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
|
||||
return L->Cstack.lua2C+number-1;
|
||||
}
|
||||
|
||||
|
||||
int lua_callfunction (lua_State *L, lua_Object function) {
|
||||
if (function == LUA_NOOBJECT)
|
||||
return 1;
|
||||
else {
|
||||
luaD_openstack(L, L->Cstack.base);
|
||||
*L->Cstack.base = *function;
|
||||
return luaD_protectedrun(L);
|
||||
static TObject *luaA_indexAcceptable (lua_State *L, int index) {
|
||||
if (index >= 0) {
|
||||
TObject *o = L->Cbase+(index-1);
|
||||
if (o >= L->top) return NULL;
|
||||
else return o;
|
||||
}
|
||||
else return L->top+index;
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
|
||||
return luaA_putluaObject(L, luaT_gettagmethod(L, tag, event));
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
|
||||
TObject *method;
|
||||
luaA_checkCargs(L, 1);
|
||||
method = L->top-1;
|
||||
if ((ttype(method) != TAG_NIL) && (*lua_type(L, method) != 'f'))
|
||||
lua_error(L, "Lua API error - tag method must be a function or nil");
|
||||
luaT_settagmethod(L, tag, event, method);
|
||||
return luaA_putObjectOnTop(L);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_gettable (lua_State *L) {
|
||||
luaA_checkCargs(L, 2);
|
||||
luaV_gettable(L, L->top--);
|
||||
return luaA_putObjectOnTop(L);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_rawgettable (lua_State *L) {
|
||||
lua_Object res;
|
||||
luaA_checkCargs(L, 2);
|
||||
if (ttype(L->top-2) != TAG_TABLE)
|
||||
lua_error(L, "indexed expression not a table in rawgettable");
|
||||
res = luaA_putluaObject(L, luaH_get(L, avalue(L->top-2), L->top-1));
|
||||
L->top -= 2;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
void lua_settable (lua_State *L) {
|
||||
StkId top;
|
||||
luaA_checkCargs(L, 3);
|
||||
top = L->top;
|
||||
luaV_settable(L, top-3, top);
|
||||
L->top = top-3; /* pop table, index, and value */
|
||||
}
|
||||
|
||||
|
||||
void lua_rawsettable (lua_State *L) {
|
||||
luaA_checkCargs(L, 3);
|
||||
luaV_rawsettable(L, L->top-3);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_createtable (lua_State *L) {
|
||||
TObject o;
|
||||
luaC_checkGC(L);
|
||||
avalue(&o) = luaH_new(L, 0);
|
||||
ttype(&o) = TAG_TABLE;
|
||||
return luaA_putluaObject(L, &o);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_getglobal (lua_State *L, const char *name) {
|
||||
luaV_getglobal(L, luaS_assertglobalbyname(L, name), L->top++);
|
||||
return luaA_putObjectOnTop(L);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
|
||||
GlobalVar *gv = luaS_assertglobalbyname(L, name);
|
||||
return luaA_putluaObject(L, &gv->value);
|
||||
}
|
||||
|
||||
|
||||
void lua_setglobal (lua_State *L, const char *name) {
|
||||
luaA_checkCargs(L, 1);
|
||||
luaV_setglobal(L, luaS_assertglobalbyname(L, name), L->top--);
|
||||
}
|
||||
|
||||
|
||||
void lua_rawsetglobal (lua_State *L, const char *name) {
|
||||
GlobalVar *gv = luaS_assertglobalbyname(L, name);
|
||||
luaA_checkCargs(L, 1);
|
||||
gv->value = *(--L->top);
|
||||
}
|
||||
|
||||
|
||||
const char *lua_type (lua_State *L, lua_Object o) {
|
||||
UNUSED(L);
|
||||
return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(o);
|
||||
}
|
||||
|
||||
int lua_isnil (lua_State *L, lua_Object o) {
|
||||
UNUSED(L);
|
||||
return (o != LUA_NOOBJECT) && (ttype(o) == TAG_NIL);
|
||||
}
|
||||
|
||||
int lua_istable (lua_State *L, lua_Object o) {
|
||||
UNUSED(L);
|
||||
return (o != LUA_NOOBJECT) && (ttype(o) == TAG_TABLE);
|
||||
}
|
||||
|
||||
int lua_isuserdata (lua_State *L, lua_Object o) {
|
||||
UNUSED(L);
|
||||
return (o != LUA_NOOBJECT) && (ttype(o) == TAG_USERDATA);
|
||||
}
|
||||
|
||||
int lua_iscfunction (lua_State *L, lua_Object o) {
|
||||
UNUSED(L);
|
||||
return (o != LUA_NOOBJECT) && (ttype(o) == TAG_CCLOSURE);
|
||||
}
|
||||
|
||||
int lua_isnumber (lua_State *L, lua_Object o) {
|
||||
UNUSED(L);
|
||||
return (o != LUA_NOOBJECT) && (tonumber(o) == 0);
|
||||
}
|
||||
|
||||
int lua_isstring (lua_State *L, lua_Object o) {
|
||||
UNUSED(L);
|
||||
return (o != LUA_NOOBJECT && (ttype(o) == TAG_STRING ||
|
||||
ttype(o) == TAG_NUMBER));
|
||||
}
|
||||
|
||||
int lua_isfunction (lua_State *L, lua_Object o) {
|
||||
return *lua_type(L, o) == 'f';
|
||||
}
|
||||
|
||||
int lua_equal(lua_State *L, lua_Object o1, lua_Object o2) {
|
||||
UNUSED(L);
|
||||
if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT)
|
||||
return (o1 == o2);
|
||||
else return luaO_equalObj(o1, o2);
|
||||
}
|
||||
|
||||
|
||||
double lua_getnumber (lua_State *L, lua_Object obj) {
|
||||
UNUSED(L);
|
||||
if (obj == LUA_NOOBJECT || tonumber(obj))
|
||||
return 0.0;
|
||||
else return (nvalue(obj));
|
||||
}
|
||||
|
||||
const char *lua_getstring (lua_State *L, lua_Object obj) {
|
||||
luaC_checkGC(L); /* `tostring' may create a new string */
|
||||
if (obj == LUA_NOOBJECT || tostring(L, obj))
|
||||
return NULL;
|
||||
else return (svalue(obj));
|
||||
}
|
||||
|
||||
long lua_strlen (lua_State *L, lua_Object obj) {
|
||||
if (obj == LUA_NOOBJECT || tostring(L, obj))
|
||||
return 0L;
|
||||
else return (tsvalue(obj)->u.s.len);
|
||||
}
|
||||
|
||||
void *lua_getuserdata (lua_State *L, lua_Object obj) {
|
||||
UNUSED(L);
|
||||
if (obj == LUA_NOOBJECT || ttype(obj) != TAG_USERDATA)
|
||||
return NULL;
|
||||
else return tsvalue(obj)->u.d.value;
|
||||
}
|
||||
|
||||
lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) {
|
||||
if (!lua_iscfunction(L, obj))
|
||||
return NULL;
|
||||
else return clvalue(obj)->f.c;
|
||||
}
|
||||
|
||||
|
||||
void lua_pushnil (lua_State *L) {
|
||||
ttype(L->top) = TAG_NIL;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushnumber (lua_State *L, double n) {
|
||||
ttype(L->top) = TAG_NUMBER;
|
||||
nvalue(L->top) = n;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushlstring (lua_State *L, const char *s, long len) {
|
||||
tsvalue(L->top) = luaS_newlstr(L, s, len);
|
||||
ttype(L->top) = TAG_STRING;
|
||||
incr_top;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
void lua_pushstring (lua_State *L, const char *s) {
|
||||
if (s == NULL)
|
||||
lua_pushnil(L);
|
||||
else
|
||||
lua_pushlstring(L, s, strlen(s));
|
||||
}
|
||||
|
||||
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
if (fn == NULL)
|
||||
lua_error(L, "Lua API error - attempt to push a NULL Cfunction");
|
||||
luaA_checkCargs(L, n);
|
||||
luaV_Cclosure(L, fn, n);
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
|
||||
if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
|
||||
luaL_verror(L, "invalid tag for a userdata (%d)", tag);
|
||||
tsvalue(L->top) = luaS_createudata(L, u, tag);
|
||||
ttype(L->top) = TAG_USERDATA;
|
||||
incr_top;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
void luaA_pushobject (lua_State *L, const TObject *o) {
|
||||
*L->top = *o;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushobject (lua_State *L, lua_Object o) {
|
||||
if (o == LUA_NOOBJECT)
|
||||
lua_error(L, "Lua API error - attempt to push a NOOBJECT");
|
||||
*L->top = *o;
|
||||
incr_top;
|
||||
LUA_API int lua_stackspace (lua_State *L) {
|
||||
return (L->stack_last - L->top);
|
||||
}
|
||||
|
||||
|
||||
int lua_tag (lua_State *L, lua_Object o) {
|
||||
if (o == LUA_NOOBJECT)
|
||||
return TAG_NIL;
|
||||
else if (ttype(o) == TAG_USERDATA) /* to allow `old' tags (deprecated) */
|
||||
return o->value.ts->u.d.tag;
|
||||
|
||||
/*
|
||||
** basic stack manipulation
|
||||
*/
|
||||
|
||||
|
||||
LUA_API int lua_gettop (lua_State *L) {
|
||||
return (L->top - L->Cbase);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_settop (lua_State *L, int index) {
|
||||
if (index >= 0)
|
||||
luaD_adjusttop(L, L->Cbase, index);
|
||||
else
|
||||
return luaT_effectivetag(L, o);
|
||||
L->top = L->top+index+1; /* index is negative */
|
||||
}
|
||||
|
||||
|
||||
void lua_settag (lua_State *L, int tag) {
|
||||
luaA_checkCargs(L, 1);
|
||||
luaT_realtag(L, tag);
|
||||
switch (ttype(L->top-1)) {
|
||||
case TAG_TABLE:
|
||||
(L->top-1)->value.a->htag = tag;
|
||||
break;
|
||||
case TAG_USERDATA:
|
||||
(L->top-1)->value.ts->u.d.tag = tag;
|
||||
break;
|
||||
default:
|
||||
luaL_verror(L, "cannot change the tag of a %.20s",
|
||||
luaO_typename(L->top-1));
|
||||
}
|
||||
LUA_API void lua_remove (lua_State *L, int index) {
|
||||
StkId p = luaA_index(L, index);
|
||||
while (++p < L->top) *(p-1) = *p;
|
||||
L->top--;
|
||||
}
|
||||
|
||||
|
||||
GlobalVar *luaA_nextvar (lua_State *L, TString *ts) {
|
||||
GlobalVar *gv;
|
||||
if (ts == NULL)
|
||||
gv = L->rootglobal; /* first variable */
|
||||
else {
|
||||
/* check whether name is in global var list */
|
||||
luaL_arg_check(L, ts->u.s.gv, 1, "variable name expected");
|
||||
gv = ts->u.s.gv->next; /* get next */
|
||||
}
|
||||
while (gv && gv->value.ttype == TAG_NIL) /* skip globals with nil */
|
||||
gv = gv->next;
|
||||
if (gv) {
|
||||
ttype(L->top) = TAG_STRING; tsvalue(L->top) = gv->name;
|
||||
incr_top;
|
||||
luaA_pushobject(L, &gv->value);
|
||||
}
|
||||
return gv;
|
||||
LUA_API void lua_insert (lua_State *L, int index) {
|
||||
StkId p = luaA_index(L, index);
|
||||
StkId q;
|
||||
for (q = L->top; q>p; q--)
|
||||
*q = *(q-1);
|
||||
*p = *L->top;
|
||||
}
|
||||
|
||||
|
||||
const char *lua_nextvar (lua_State *L, const char *varname) {
|
||||
TString *ts = (varname == NULL) ? NULL : luaS_new(L, varname);
|
||||
GlobalVar *gv = luaA_nextvar(L, ts);
|
||||
if (gv) {
|
||||
top2LC(L, 2);
|
||||
return gv->name->str;
|
||||
}
|
||||
else {
|
||||
top2LC(L, 0);
|
||||
return NULL;
|
||||
LUA_API void lua_pushvalue (lua_State *L, int index) {
|
||||
*L->top = *luaA_index(L, index);
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** access functions (stack -> C)
|
||||
*/
|
||||
|
||||
|
||||
LUA_API int lua_type (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL) ? LUA_TNONE : ttype(o);
|
||||
}
|
||||
|
||||
LUA_API const char *lua_typename (lua_State *L, int t) {
|
||||
UNUSED(L);
|
||||
return (t == LUA_TNONE) ? "no value" : luaO_typenames[t];
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_iscfunction (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL) ? 0 : iscfunction(o);
|
||||
}
|
||||
|
||||
LUA_API int lua_isnumber (lua_State *L, int index) {
|
||||
TObject *o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL) ? 0 : (tonumber(o) == 0);
|
||||
}
|
||||
|
||||
LUA_API int lua_isstring (lua_State *L, int index) {
|
||||
int t = lua_type(L, index);
|
||||
return (t == LUA_TSTRING || t == LUA_TNUMBER);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_tag (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL) ? LUA_NOTAG : luaT_tag(o);
|
||||
}
|
||||
|
||||
LUA_API int lua_equal (lua_State *L, int index1, int index2) {
|
||||
StkId o1 = luaA_indexAcceptable(L, index1);
|
||||
StkId o2 = luaA_indexAcceptable(L, index2);
|
||||
if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */
|
||||
else return luaO_equalObj(o1, o2);
|
||||
}
|
||||
|
||||
LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
|
||||
StkId o1 = luaA_indexAcceptable(L, index1);
|
||||
StkId o2 = luaA_indexAcceptable(L, index2);
|
||||
if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */
|
||||
else return luaV_lessthan(L, o1, o2, L->top);
|
||||
}
|
||||
|
||||
|
||||
|
||||
LUA_API double lua_tonumber (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL || tonumber(o)) ? 0 : nvalue(o);
|
||||
}
|
||||
|
||||
LUA_API const char *lua_tostring (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL || tostring(L, o)) ? NULL : svalue(o);
|
||||
}
|
||||
|
||||
LUA_API size_t lua_strlen (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len;
|
||||
}
|
||||
|
||||
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
|
||||
}
|
||||
|
||||
LUA_API void *lua_touserdata (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL :
|
||||
tsvalue(o)->u.d.value;
|
||||
}
|
||||
|
||||
LUA_API const void *lua_topointer (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
if (o == NULL) return NULL;
|
||||
switch (ttype(o)) {
|
||||
case LUA_TTABLE:
|
||||
return hvalue(o);
|
||||
case LUA_TFUNCTION:
|
||||
return clvalue(o);
|
||||
default: return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaA_next (lua_State *L, const Hash *t, int i) {
|
||||
int tsize = t->size;
|
||||
for (; i<tsize; i++) {
|
||||
Node *n = node(t, i);
|
||||
if (ttype(val(n)) != TAG_NIL) {
|
||||
luaA_pushobject(L, key(n));
|
||||
luaA_pushobject(L, val(n));
|
||||
return i+1; /* index to be used next time */
|
||||
|
||||
/*
|
||||
** push functions (C -> stack)
|
||||
*/
|
||||
|
||||
|
||||
LUA_API void lua_pushnil (lua_State *L) {
|
||||
ttype(L->top) = LUA_TNIL;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushnumber (lua_State *L, double n) {
|
||||
nvalue(L->top) = n;
|
||||
ttype(L->top) = LUA_TNUMBER;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
||||
tsvalue(L->top) = luaS_newlstr(L, s, len);
|
||||
ttype(L->top) = LUA_TSTRING;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushstring (lua_State *L, const char *s) {
|
||||
if (s == NULL)
|
||||
lua_pushnil(L);
|
||||
else
|
||||
lua_pushlstring(L, s, strlen(s));
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
luaV_Cclosure(L, fn, n);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
|
||||
/* ORDER LUA_T */
|
||||
if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag)))
|
||||
luaO_verror(L, "invalid tag for a userdata (%d)", tag);
|
||||
tsvalue(L->top) = luaS_createudata(L, u, tag);
|
||||
ttype(L->top) = LUA_TUSERDATA;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
|
||||
|
||||
LUA_API void lua_getglobal (lua_State *L, const char *name) {
|
||||
StkId top = L->top;
|
||||
*top = *luaV_getglobal(L, luaS_new(L, name));
|
||||
L->top = top;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_gettable (lua_State *L, int index) {
|
||||
StkId t = Index(L, index);
|
||||
StkId top = L->top;
|
||||
*(top-1) = *luaV_gettable(L, t);
|
||||
L->top = top; /* tag method may change top */
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawget (lua_State *L, int index) {
|
||||
StkId t = Index(L, index);
|
||||
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
|
||||
*(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
|
||||
StkId o = Index(L, index);
|
||||
LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
|
||||
*L->top = *luaH_getnum(hvalue(o), n);
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_getglobals (lua_State *L) {
|
||||
hvalue(L->top) = L->gt;
|
||||
ttype(L->top) = LUA_TTABLE;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getref (lua_State *L, int ref) {
|
||||
if (ref == LUA_REFNIL)
|
||||
ttype(L->top) = LUA_TNIL;
|
||||
else if (0 <= ref && ref < L->refSize &&
|
||||
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
|
||||
*L->top = L->refArray[ref].o;
|
||||
else
|
||||
return 0;
|
||||
api_incr_top(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_newtable (lua_State *L) {
|
||||
hvalue(L->top) = luaH_new(L, 0);
|
||||
ttype(L->top) = LUA_TTABLE;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
|
||||
|
||||
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
||||
StkId top = L->top;
|
||||
luaV_setglobal(L, luaS_new(L, name));
|
||||
L->top = top-1; /* remove element from the top */
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_settable (lua_State *L, int index) {
|
||||
StkId t = Index(L, index);
|
||||
StkId top = L->top;
|
||||
luaV_settable(L, t, top-2);
|
||||
L->top = top-2; /* pop index and value */
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawset (lua_State *L, int index) {
|
||||
StkId t = Index(L, index);
|
||||
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
|
||||
*luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
|
||||
L->top -= 2;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawseti (lua_State *L, int index, int n) {
|
||||
StkId o = Index(L, index);
|
||||
LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
|
||||
*luaH_setint(L, hvalue(o), n) = *(L->top-1);
|
||||
L->top--;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_setglobals (lua_State *L) {
|
||||
StkId newtable = --L->top;
|
||||
LUA_ASSERT(ttype(newtable) == LUA_TTABLE, "table expected");
|
||||
L->gt = hvalue(newtable);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_ref (lua_State *L, int lock) {
|
||||
int ref;
|
||||
if (ttype(L->top-1) == LUA_TNIL)
|
||||
ref = LUA_REFNIL;
|
||||
else {
|
||||
if (L->refFree != NONEXT) { /* is there a free place? */
|
||||
ref = L->refFree;
|
||||
L->refFree = L->refArray[ref].st;
|
||||
}
|
||||
else { /* no more free places */
|
||||
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
|
||||
"reference table overflow", MAX_INT);
|
||||
L->nblocks += sizeof(struct Ref);
|
||||
ref = L->refSize++;
|
||||
}
|
||||
L->refArray[ref].o = *(L->top-1);
|
||||
L->refArray[ref].st = lock ? LOCK : HOLD;
|
||||
}
|
||||
return 0; /* no more elements */
|
||||
L->top--;
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
int lua_next (lua_State *L, lua_Object t, int i) {
|
||||
if (ttype(t) != TAG_TABLE)
|
||||
lua_error(L, "Lua API error - object is not a table in `lua_next'");
|
||||
i = luaA_next(L, avalue(t), i);
|
||||
top2LC(L, (i==0) ? 0 : 2);
|
||||
return i;
|
||||
/*
|
||||
** "do" functions (run Lua code)
|
||||
** (most of them are in ldo.c)
|
||||
*/
|
||||
|
||||
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
|
||||
luaD_call(L, L->top-(nargs+1), nresults);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage-collection functions
|
||||
*/
|
||||
|
||||
/* GC values are expressed in Kbytes: #bytes/2^10 */
|
||||
#define GCscale(x) ((int)((x)>>10))
|
||||
#define GCunscale(x) ((unsigned long)(x)<<10)
|
||||
|
||||
LUA_API int lua_getgcthreshold (lua_State *L) {
|
||||
return GCscale(L->GCthreshold);
|
||||
}
|
||||
|
||||
LUA_API int lua_getgccount (lua_State *L) {
|
||||
return GCscale(L->nblocks);
|
||||
}
|
||||
|
||||
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
|
||||
if (newthreshold > GCscale(ULONG_MAX))
|
||||
L->GCthreshold = ULONG_MAX;
|
||||
else
|
||||
L->GCthreshold = GCunscale(newthreshold);
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** miscellaneous functions
|
||||
*/
|
||||
|
||||
LUA_API void lua_settag (lua_State *L, int tag) {
|
||||
luaT_realtag(L, tag);
|
||||
switch (ttype(L->top-1)) {
|
||||
case LUA_TTABLE:
|
||||
hvalue(L->top-1)->htag = tag;
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
tsvalue(L->top-1)->u.d.tag = tag;
|
||||
break;
|
||||
default:
|
||||
luaO_verror(L, "cannot change the tag of a %.20s",
|
||||
luaO_typename(L->top-1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_unref (lua_State *L, int ref) {
|
||||
if (ref >= 0) {
|
||||
LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
|
||||
L->refArray[ref].st = L->refFree;
|
||||
L->refFree = ref;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_next (lua_State *L, int index) {
|
||||
StkId t = luaA_index(L, index);
|
||||
Node *n;
|
||||
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
|
||||
n = luaH_next(L, hvalue(t), luaA_index(L, -1));
|
||||
if (n) {
|
||||
*(L->top-1) = *key(n);
|
||||
*L->top = *val(n);
|
||||
api_incr_top(L);
|
||||
return 1;
|
||||
}
|
||||
else { /* no more elements */
|
||||
L->top -= 1; /* remove key */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getn (lua_State *L, int index) {
|
||||
Hash *h = hvalue(luaA_index(L, index));
|
||||
const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
|
||||
if (ttype(value) == LUA_TNUMBER)
|
||||
return (int)nvalue(value);
|
||||
else {
|
||||
Number max = 0;
|
||||
int i = h->size;
|
||||
Node *n = h->node;
|
||||
while (i--) {
|
||||
if (ttype(key(n)) == LUA_TNUMBER &&
|
||||
ttype(val(n)) != LUA_TNIL &&
|
||||
nvalue(key(n)) > max)
|
||||
max = nvalue(key(n));
|
||||
n++;
|
||||
}
|
||||
return (int)max;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_concat (lua_State *L, int n) {
|
||||
StkId top = L->top;
|
||||
luaV_strconc(L, n, top);
|
||||
L->top = top-(n-1);
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
|
||||
TString *ts = luaS_newudata(L, size, NULL);
|
||||
tsvalue(L->top) = ts;
|
||||
ttype(L->top) = LUA_TUSERDATA;
|
||||
api_incr_top(L);
|
||||
return ts->u.d.value;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.h,v 1.15 2000/03/10 18:37:44 roberto Exp roberto $
|
||||
** $Id: lapi.h,v 1.19 2000/08/28 17:57:04 roberto Exp roberto $
|
||||
** Auxiliary functions from Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,11 +11,7 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaA_checkCargs (lua_State *L, int nargs);
|
||||
TObject *luaA_index (lua_State *L, int index);
|
||||
void luaA_pushobject (lua_State *L, const TObject *o);
|
||||
GlobalVar *luaA_nextvar (lua_State *L, TString *g);
|
||||
int luaA_next (lua_State *L, const Hash *t, int i);
|
||||
lua_Object luaA_putluaObject (lua_State *L, const TObject *o);
|
||||
lua_Object luaA_putObjectOnTop (lua_State *L);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.26 2000/02/08 16:34:31 roberto Exp roberto $
|
||||
** $Id: lauxlib.c,v 1.42 2000/10/30 12:38:50 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -14,15 +14,14 @@
|
||||
** With care, these functions can be used by other libraries.
|
||||
*/
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
|
||||
int luaL_findstring (const char *name, const char *const list[]) {
|
||||
LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
|
||||
int i;
|
||||
for (i=0; list[i]; i++)
|
||||
if (strcmp(list[i], name) == 0)
|
||||
@@ -30,11 +29,10 @@ int luaL_findstring (const char *name, const char *const list[]) {
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
|
||||
LUALIB_API void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
|
||||
lua_Debug ar;
|
||||
lua_getstack(L, 0, &ar);
|
||||
lua_getinfo(L, "nu", &ar);
|
||||
narg -= ar.nups;
|
||||
lua_getinfo(L, "n", &ar);
|
||||
if (ar.name == NULL)
|
||||
ar.name = "?";
|
||||
luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)",
|
||||
@@ -42,78 +40,72 @@ void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
|
||||
}
|
||||
|
||||
|
||||
static void type_error (lua_State *L, int narg, const char *type_name,
|
||||
lua_Object o) {
|
||||
char buff[100];
|
||||
const char *otype = (o == LUA_NOOBJECT) ? "no value" : lua_type(L, o);
|
||||
sprintf(buff, "%.10s expected, got %.10s", type_name, otype);
|
||||
static void type_error (lua_State *L, int narg, int t) {
|
||||
char buff[50];
|
||||
sprintf(buff, "%.8s expected, got %.8s", lua_typename(L, t),
|
||||
lua_typename(L, lua_type(L, narg)));
|
||||
luaL_argerror(L, narg, buff);
|
||||
}
|
||||
|
||||
|
||||
static const char *checkstr (lua_State *L, lua_Object o, int narg, long *len) {
|
||||
const char *s = lua_getstring(L, o);
|
||||
if (!s) type_error(L, narg, "string", o);
|
||||
if (len) *len = lua_strlen(L, o);
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
|
||||
if (space > lua_stackspace(L))
|
||||
luaL_verror(L, "stack overflow (%.30s)", mes);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
|
||||
if (lua_type(L, narg) != t)
|
||||
type_error(L, narg, t);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkany (lua_State *L, int narg) {
|
||||
if (lua_type(L, narg) == LUA_TNONE)
|
||||
luaL_argerror(L, narg, "value expected");
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
|
||||
const char *s = lua_tostring(L, narg);
|
||||
if (!s) type_error(L, narg, LUA_TSTRING);
|
||||
if (len) *len = lua_strlen(L, narg);
|
||||
return s;
|
||||
}
|
||||
|
||||
const char *luaL_check_lstr (lua_State *L, int narg, long *len) {
|
||||
return checkstr(L, lua_getparam(L, narg), narg, len);
|
||||
}
|
||||
|
||||
const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, long *len) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (o == LUA_NOOBJECT) {
|
||||
if (len) *len = def ? strlen(def) : 0;
|
||||
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, size_t *len) {
|
||||
if (lua_isnull(L, narg)) {
|
||||
if (len)
|
||||
*len = (def ? strlen(def) : 0);
|
||||
return def;
|
||||
}
|
||||
else return checkstr(L, o, narg, len);
|
||||
}
|
||||
|
||||
double luaL_check_number (lua_State *L, int narg) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
|
||||
return lua_getnumber(L, o);
|
||||
else return luaL_check_lstr(L, narg, len);
|
||||
}
|
||||
|
||||
|
||||
double luaL_opt_number (lua_State *L, int narg, double def) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (o == LUA_NOOBJECT) return def;
|
||||
else {
|
||||
if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
|
||||
return lua_getnumber(L, o);
|
||||
}
|
||||
LUALIB_API double luaL_check_number (lua_State *L, int narg) {
|
||||
double d = lua_tonumber(L, narg);
|
||||
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
|
||||
type_error(L, narg, LUA_TNUMBER);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
lua_Object luaL_tablearg (lua_State *L, int narg) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (!lua_istable(L, o)) type_error(L, narg, "table", o);
|
||||
return o;
|
||||
LUALIB_API double luaL_opt_number (lua_State *L, int narg, double def) {
|
||||
if (lua_isnull(L, narg)) return def;
|
||||
else return luaL_check_number(L, narg);
|
||||
}
|
||||
|
||||
lua_Object luaL_functionarg (lua_State *L, int narg) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (!lua_isfunction(L, o)) type_error(L, narg, "function", o);
|
||||
return o;
|
||||
}
|
||||
|
||||
lua_Object luaL_nonnullarg (lua_State *L, int narg) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
luaL_arg_check(L, o != LUA_NOOBJECT, narg, "value expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
|
||||
int i;
|
||||
for (i=0; i<n; i++)
|
||||
lua_register(L, l[i].name, l[i].func);
|
||||
}
|
||||
|
||||
|
||||
void luaL_verror (lua_State *L, const char *fmt, ...) {
|
||||
LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...) {
|
||||
char buff[500];
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
@@ -123,29 +115,102 @@ void luaL_verror (lua_State *L, const char *fmt, ...) {
|
||||
}
|
||||
|
||||
|
||||
#define EXTRALEN sizeof("string \"...\"0")
|
||||
/*
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
void luaL_chunkid (char *out, const char *source, int len) {
|
||||
if (*source == '(') {
|
||||
strncpy(out, source+1, len-1); /* remove first char */
|
||||
out[len-1] = '\0'; /* make sure `out' has an end */
|
||||
out[strlen(out)-1] = '\0'; /* remove last char */
|
||||
}
|
||||
|
||||
#define buffempty(B) ((B)->p == (B)->buffer)
|
||||
#define bufflen(B) ((B)->p - (B)->buffer)
|
||||
#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
|
||||
|
||||
#define LIMIT (LUA_MINSTACK/2)
|
||||
|
||||
|
||||
static int emptybuffer (luaL_Buffer *B) {
|
||||
size_t l = bufflen(B);
|
||||
if (l == 0) return 0; /* put nothing on stack */
|
||||
else {
|
||||
len -= EXTRALEN;
|
||||
if (*source == '@')
|
||||
sprintf(out, "file `%.*s'", len, source+1);
|
||||
else {
|
||||
const char *b = strchr(source , '\n'); /* stop at first new line */
|
||||
int lim = (b && (b-source)<len) ? b-source : len;
|
||||
sprintf(out, "string \"%.*s\"", lim, source);
|
||||
strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
|
||||
lua_pushlstring(B->L, B->buffer, l);
|
||||
B->p = B->buffer;
|
||||
B->level++;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjuststack (luaL_Buffer *B) {
|
||||
if (B->level > 1) {
|
||||
lua_State *L = B->L;
|
||||
int toget = 1; /* number of levels to concat */
|
||||
size_t toplen = lua_strlen(L, -1);
|
||||
do {
|
||||
size_t l = lua_strlen(L, -(toget+1));
|
||||
if (B->level - toget + 1 >= LIMIT || toplen > l) {
|
||||
toplen += l;
|
||||
toget++;
|
||||
}
|
||||
else break;
|
||||
} while (toget < B->level);
|
||||
if (toget >= 2) {
|
||||
lua_concat(L, toget);
|
||||
B->level = B->level - toget + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaL_filesource (char *out, const char *filename, int len) {
|
||||
if (filename == NULL) filename = "(stdin)";
|
||||
sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */
|
||||
LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
|
||||
if (emptybuffer(B))
|
||||
adjuststack(B);
|
||||
return B->buffer;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
|
||||
while (l--)
|
||||
luaL_putchar(B, *s++);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
|
||||
luaL_addlstring(B, s, strlen(s));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
||||
emptybuffer(B);
|
||||
if (B->level == 0)
|
||||
lua_pushlstring(B->L, NULL, 0);
|
||||
else if (B->level > 1)
|
||||
lua_concat(B->L, B->level);
|
||||
B->level = 1;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
|
||||
lua_State *L = B->L;
|
||||
size_t vl = lua_strlen(L, -1);
|
||||
if (vl <= bufffree(B)) { /* fit into buffer? */
|
||||
memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
|
||||
B->p += vl;
|
||||
lua_pop(L, 1); /* remove from stack */
|
||||
}
|
||||
else {
|
||||
if (emptybuffer(B))
|
||||
lua_insert(L, -2); /* put buffer before new value */
|
||||
B->level++; /* add new value into B stack */
|
||||
adjuststack(B);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
|
||||
B->L = L;
|
||||
B->p = B->buffer;
|
||||
B->level = 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.16 1999/12/03 11:26:23 roberto Exp roberto $
|
||||
** $Id: lauxlib.h,v 1.29 2000/10/27 16:15:53 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,39 +9,38 @@
|
||||
#define lauxlib_h
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API extern
|
||||
#endif
|
||||
|
||||
|
||||
struct luaL_reg {
|
||||
const char *name;
|
||||
lua_CFunction func;
|
||||
};
|
||||
|
||||
|
||||
void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
|
||||
void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
|
||||
const char *luaL_check_lstr (lua_State *L, int numArg, long *len);
|
||||
const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def, long *len);
|
||||
double luaL_check_number (lua_State *L, int numArg);
|
||||
double luaL_opt_number (lua_State *L, int numArg, double def);
|
||||
lua_Object luaL_functionarg (lua_State *L, int arg);
|
||||
lua_Object luaL_tablearg (lua_State *L, int arg);
|
||||
lua_Object luaL_nonnullarg (lua_State *L, int numArg);
|
||||
void luaL_verror (lua_State *L, const char *fmt, ...);
|
||||
char *luaL_openspace (lua_State *L, int size);
|
||||
void luaL_resetbuffer (lua_State *L);
|
||||
void luaL_addchar (lua_State *L, int c);
|
||||
int luaL_getsize (lua_State *L);
|
||||
void luaL_addsize (lua_State *L, int n);
|
||||
int luaL_newbuffer (lua_State *L, int size);
|
||||
void luaL_oldbuffer (lua_State *L, int old);
|
||||
char *luaL_buffer (lua_State *L);
|
||||
int luaL_findstring (const char *name, const char *const list[]);
|
||||
void luaL_chunkid (char *out, const char *source, int len);
|
||||
void luaL_filesource (char *out, const char *filename, int len);
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
|
||||
LUALIB_API void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
|
||||
LUALIB_API const char *luaL_check_lstr (lua_State *L, int numArg, size_t *len);
|
||||
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def, size_t *len);
|
||||
LUALIB_API double luaL_check_number (lua_State *L, int numArg);
|
||||
LUALIB_API double luaL_opt_number (lua_State *L, int numArg, double def);
|
||||
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg);
|
||||
LUALIB_API void luaL_checktype (lua_State *L, int narg, int t);
|
||||
LUALIB_API void luaL_checkany (lua_State *L, int narg);
|
||||
|
||||
LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...);
|
||||
LUALIB_API int luaL_findstring (const char *name, const char *const list[]);
|
||||
|
||||
|
||||
#ifdef LUA_REENTRANT
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
@@ -59,47 +58,43 @@ void luaL_filesource (char *out, const char *filename, int len);
|
||||
#define luaL_opt_long(L,n,d) ((long)luaL_opt_number(L, n,d))
|
||||
#define luaL_openl(L,a) luaL_openlib(L, a, (sizeof(a)/sizeof(a[0])))
|
||||
|
||||
#else
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** Macros for single-state use
|
||||
** ===============================================================
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define luaL_arg_check(cond,numarg,extramsg) if (!(cond)) \
|
||||
luaL_argerror(numarg,extramsg)
|
||||
#define luaL_check_string(n) (luaL_check_lstr((n), NULL))
|
||||
#define luaL_opt_string(n, d) (luaL_opt_lstr((n), (d), NULL))
|
||||
#define luaL_check_int(n) ((int)luaL_check_number(n))
|
||||
#define luaL_check_long(n) ((long)luaL_check_number(n))
|
||||
#define luaL_opt_int(n,d) ((int)luaL_opt_number(n,d))
|
||||
#define luaL_opt_long(n,d) ((long)luaL_opt_number(n,d))
|
||||
#define luaL_openl(a) luaL_openlib(a, (sizeof(a)/sizeof(a[0])))
|
||||
|
||||
#ifndef LUAL_BUFFERSIZE
|
||||
#define LUAL_BUFFERSIZE BUFSIZ
|
||||
#endif
|
||||
|
||||
|
||||
#define luaL_openlib(l,n) (luaL_openlib)(lua_state,l,n)
|
||||
#define luaL_argerror(numarg,extramsg) \
|
||||
(luaL_argerror)(lua_state,numarg,extramsg)
|
||||
#define luaL_check_lstr(numArg,len) (luaL_check_lstr)(lua_state,numArg,len)
|
||||
#define luaL_opt_lstr(numArg,def,len) \
|
||||
(luaL_opt_lstr)(lua_state,numArg,def,len)
|
||||
#define luaL_check_number(numArg) (luaL_check_number)(lua_state,numArg)
|
||||
#define luaL_opt_number(numArg,def) (luaL_opt_number)(lua_state,numArg,def)
|
||||
#define luaL_functionarg(arg) (luaL_functionarg)(lua_state,arg)
|
||||
#define luaL_tablearg(arg) (luaL_tablearg)(lua_state,arg)
|
||||
#define luaL_nonnullarg(numArg) (luaL_nonnullarg)(lua_state,numArg)
|
||||
#define luaL_openspace(size) (luaL_openspace)(lua_state,size)
|
||||
#define luaL_resetbuffer() (luaL_resetbuffer)(lua_state)
|
||||
#define luaL_addchar(c) (luaL_addchar)(lua_state,c)
|
||||
#define luaL_getsize() (luaL_getsize)(lua_state)
|
||||
#define luaL_addsize(n) (luaL_addsize)(lua_state,n)
|
||||
#define luaL_newbuffer(size) (luaL_newbuffer)(lua_state,size)
|
||||
#define luaL_oldbuffer(old) (luaL_oldbuffer)(lua_state,old)
|
||||
#define luaL_buffer() (luaL_buffer)(lua_state)
|
||||
typedef struct luaL_Buffer {
|
||||
char *p; /* current position in buffer */
|
||||
int level;
|
||||
lua_State *L;
|
||||
char buffer[LUAL_BUFFERSIZE];
|
||||
} luaL_Buffer;
|
||||
|
||||
#define luaL_putchar(B,c) \
|
||||
((void)((B)->p < &(B)->buffer[LUAL_BUFFERSIZE] || luaL_prepbuffer(B)), \
|
||||
(*(B)->p++ = (char)(c)))
|
||||
|
||||
#define luaL_addsize(B,n) ((B)->p += (n))
|
||||
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B);
|
||||
LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B);
|
||||
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);
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
+651
@@ -0,0 +1,651 @@
|
||||
/*
|
||||
** $Id: lbaselib.c,v 1.16 2000/10/31 13:10:24 roberto Exp roberto $
|
||||
** Basic library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stderr', redefine this function, or
|
||||
** redefine _ERRORMESSAGE so that it won't need _ALERT.
|
||||
*/
|
||||
static int luaB__ALERT (lua_State *L) {
|
||||
fputs(luaL_check_string(L, 1), stderr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Basic implementation of _ERRORMESSAGE.
|
||||
** The library `liolib' redefines _ERRORMESSAGE for better error information.
|
||||
*/
|
||||
static int luaB__ERRORMESSAGE (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TSTRING);
|
||||
lua_getglobal(L, LUA_ALERT);
|
||||
if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
|
||||
lua_Debug ar;
|
||||
lua_pushstring(L, "error: ");
|
||||
lua_pushvalue(L, 1);
|
||||
if (lua_getstack(L, 1, &ar)) {
|
||||
lua_getinfo(L, "Sl", &ar);
|
||||
if (ar.source && ar.currentline > 0) {
|
||||
char buff[100];
|
||||
sprintf(buff, "\n <%.70s: line %d>", ar.short_src, ar.currentline);
|
||||
lua_pushstring(L, buff);
|
||||
lua_concat(L, 2);
|
||||
}
|
||||
}
|
||||
lua_pushstring(L, "\n");
|
||||
lua_concat(L, 3);
|
||||
lua_rawcall(L, 1, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stdout', you can just remove this function.
|
||||
** If you need, you can define your own `print' function, following this
|
||||
** model but changing `fputs' to put the strings at a proper place
|
||||
** (a console window or a log file, for instance).
|
||||
*/
|
||||
static int luaB_print (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
int i;
|
||||
lua_getglobal(L, "tostring");
|
||||
for (i=1; i<=n; i++) {
|
||||
const char *s;
|
||||
lua_pushvalue(L, -1); /* function to be called */
|
||||
lua_pushvalue(L, i); /* value to print */
|
||||
lua_rawcall(L, 1, 1);
|
||||
s = lua_tostring(L, -1); /* get result */
|
||||
if (s == NULL)
|
||||
lua_error(L, "`tostring' must return a string to `print'");
|
||||
if (i>1) fputs("\t", stdout);
|
||||
fputs(s, stdout);
|
||||
lua_pop(L, 1); /* pop result */
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tonumber (lua_State *L) {
|
||||
int base = luaL_opt_int(L, 2, 10);
|
||||
if (base == 10) { /* standard conversion */
|
||||
luaL_checkany(L, 1);
|
||||
if (lua_isnumber(L, 1)) {
|
||||
lua_pushnumber(L, lua_tonumber(L, 1));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const char *s1 = luaL_check_string(L, 1);
|
||||
char *s2;
|
||||
unsigned long n;
|
||||
luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
n = strtoul(s1, &s2, base);
|
||||
if (s1 != s2) { /* at least one valid digit? */
|
||||
while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
|
||||
if (*s2 == '\0') { /* no invalid trailing characters? */
|
||||
lua_pushnumber(L, n);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_pushnil(L); /* else not a number */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_error (lua_State *L) {
|
||||
lua_error(L, luaL_opt_string(L, 1, NULL));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
static int luaB_setglobal (lua_State *L) {
|
||||
luaL_checkany(L, 2);
|
||||
lua_setglobal(L, luaL_check_string(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int luaB_getglobal (lua_State *L) {
|
||||
lua_getglobal(L, luaL_check_string(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_tag (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushnumber(L, lua_tag(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_settag (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushvalue(L, 1); /* push table */
|
||||
lua_settag(L, luaL_check_int(L, 2));
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
static int luaB_newtag (lua_State *L) {
|
||||
lua_pushnumber(L, lua_newtag(L));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_copytagmethods (lua_State *L) {
|
||||
lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
|
||||
luaL_check_int(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_globals (lua_State *L) {
|
||||
lua_getglobals(L); /* value to be returned */
|
||||
if (!lua_isnull(L, 1)) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushvalue(L, 1); /* new table of globals */
|
||||
lua_setglobals(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_rawget (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checkany(L, 2);
|
||||
lua_rawget(L, -2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_rawset (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checkany(L, 2);
|
||||
luaL_checkany(L, 3);
|
||||
lua_rawset(L, -3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_settagmethod (lua_State *L) {
|
||||
int tag = luaL_check_int(L, 1);
|
||||
const char *event = luaL_check_string(L, 2);
|
||||
luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
|
||||
"function or nil expected");
|
||||
if (strcmp(event, "gc") == 0)
|
||||
lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
|
||||
lua_gettagmethod(L, tag, event);
|
||||
lua_pushvalue(L, 3);
|
||||
lua_settagmethod(L, tag, event);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_gettagmethod (lua_State *L) {
|
||||
int tag = luaL_check_int(L, 1);
|
||||
const char *event = luaL_check_string(L, 2);
|
||||
if (strcmp(event, "gc") == 0)
|
||||
lua_error(L, "deprecated use: cannot get the `gc' tag method from Lua");
|
||||
lua_gettagmethod(L, tag, event);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_gcinfo (lua_State *L) {
|
||||
lua_pushnumber(L, lua_getgccount(L));
|
||||
lua_pushnumber(L, lua_getgcthreshold(L));
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_collectgarbage (lua_State *L) {
|
||||
lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_type (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
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 passresults (lua_State *L, int status, int oldtop) {
|
||||
static const char *const errornames[] =
|
||||
{"ok", "run-time error", "file error", "syntax error",
|
||||
"memory error", "error in error handling"};
|
||||
if (status == 0) {
|
||||
int nresults = lua_gettop(L) - oldtop;
|
||||
if (nresults > 0)
|
||||
return nresults; /* results are already on the stack */
|
||||
else {
|
||||
lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else { /* error */
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, errornames[status]); /* error code */
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
static int luaB_dostring (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
if (*s == '\27') /* binary files start with ESC... */
|
||||
lua_error(L, "`dostring' cannot run pre-compiled code");
|
||||
return passresults(L, lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)), oldtop);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_dofile (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
const char *fname = luaL_opt_string(L, 1, NULL);
|
||||
return passresults(L, lua_dofile(L, fname), oldtop);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_call (lua_State *L) {
|
||||
int oldtop;
|
||||
const char *options = luaL_opt_string(L, 3, "");
|
||||
int err = 0; /* index of old error method */
|
||||
int i, status;
|
||||
int n;
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
n = lua_getn(L, 2);
|
||||
if (!lua_isnull(L, 4)) { /* set new error method */
|
||||
lua_getglobal(L, LUA_ERRORMESSAGE);
|
||||
err = lua_gettop(L); /* get index */
|
||||
lua_pushvalue(L, 4);
|
||||
lua_setglobal(L, LUA_ERRORMESSAGE);
|
||||
}
|
||||
oldtop = lua_gettop(L); /* top before function-call preparation */
|
||||
/* push function */
|
||||
lua_pushvalue(L, 1);
|
||||
luaL_checkstack(L, n, "too many arguments");
|
||||
for (i=0; i<n; i++) /* push arg[1...n] */
|
||||
lua_rawgeti(L, 2, i+1);
|
||||
status = lua_call(L, n, LUA_MULTRET);
|
||||
if (err != 0) { /* restore old error method */
|
||||
lua_pushvalue(L, err);
|
||||
lua_setglobal(L, LUA_ERRORMESSAGE);
|
||||
}
|
||||
if (status != 0) { /* error in call? */
|
||||
if (strchr(options, 'x'))
|
||||
lua_pushnil(L); /* return nil to signal the error */
|
||||
else
|
||||
lua_error(L, NULL); /* propagate error without additional messages */
|
||||
return 1;
|
||||
}
|
||||
if (strchr(options, 'p')) /* pack results? */
|
||||
lua_error(L, "deprecated option `p' in `call'");
|
||||
return lua_gettop(L) - oldtop; /* results are already on the stack */
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tostring (lua_State *L) {
|
||||
char buff[64];
|
||||
switch (lua_type(L, 1)) {
|
||||
case LUA_TNUMBER:
|
||||
lua_pushstring(L, lua_tostring(L, 1));
|
||||
return 1;
|
||||
case LUA_TSTRING:
|
||||
lua_pushvalue(L, 1);
|
||||
return 1;
|
||||
case LUA_TTABLE:
|
||||
sprintf(buff, "table: %p", lua_topointer(L, 1));
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
sprintf(buff, "function: %p", lua_topointer(L, 1));
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
sprintf(buff, "userdata(%d): %p", lua_tag(L, 1), lua_touserdata(L, 1));
|
||||
break;
|
||||
case LUA_TNIL:
|
||||
lua_pushstring(L, "nil");
|
||||
return 1;
|
||||
default:
|
||||
luaL_argerror(L, 1, "value expected");
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_foreachi (lua_State *L) {
|
||||
int n, i;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
n = lua_getn(L, 1);
|
||||
for (i=1; i<=n; i++) {
|
||||
lua_pushvalue(L, 2); /* function */
|
||||
lua_pushnumber(L, i); /* 1st argument */
|
||||
lua_rawgeti(L, 1, i); /* 2nd argument */
|
||||
lua_rawcall(L, 2, 1);
|
||||
if (!lua_isnil(L, -1))
|
||||
return 1;
|
||||
lua_pop(L, 1); /* remove nil result */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_foreach (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
lua_pushnil(L); /* first index */
|
||||
for (;;) {
|
||||
if (lua_next(L, 1) == 0)
|
||||
return 0;
|
||||
lua_pushvalue(L, 2); /* function */
|
||||
lua_pushvalue(L, -3); /* key */
|
||||
lua_pushvalue(L, -3); /* value */
|
||||
lua_rawcall(L, 2, 1);
|
||||
if (!lua_isnil(L, -1))
|
||||
return 1;
|
||||
lua_pop(L, 2); /* remove value and result */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_assert (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
if (lua_isnil(L, 1))
|
||||
luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getn (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushnumber(L, lua_getn(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tinsert (lua_State *L) {
|
||||
int v = lua_gettop(L); /* last argument: to be inserted */
|
||||
int n, pos;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = lua_getn(L, 1);
|
||||
if (v == 2) /* called with only 2 arguments */
|
||||
pos = n+1;
|
||||
else
|
||||
pos = luaL_check_int(L, 2); /* 2nd argument is the position */
|
||||
lua_pushstring(L, "n");
|
||||
lua_pushnumber(L, n+1);
|
||||
lua_rawset(L, 1); /* t.n = n+1 */
|
||||
for (; n>=pos; n--) {
|
||||
lua_rawgeti(L, 1, n);
|
||||
lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
|
||||
}
|
||||
lua_pushvalue(L, v);
|
||||
lua_rawseti(L, 1, pos); /* t[pos] = v */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tremove (lua_State *L) {
|
||||
int pos, n;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = lua_getn(L, 1);
|
||||
pos = luaL_opt_int(L, 2, n);
|
||||
if (n <= 0) return 0; /* table is "empty" */
|
||||
lua_rawgeti(L, 1, pos); /* result = t[pos] */
|
||||
for ( ;pos<n; pos++) {
|
||||
lua_rawgeti(L, 1, pos+1);
|
||||
lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
|
||||
}
|
||||
lua_pushstring(L, "n");
|
||||
lua_pushnumber(L, n-1);
|
||||
lua_rawset(L, 1); /* t.n = n-1 */
|
||||
lua_pushnil(L);
|
||||
lua_rawseti(L, 1, n); /* t[n] = nil */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Quicksort
|
||||
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
|
||||
** Addison-Wesley, 1993.)
|
||||
*/
|
||||
|
||||
|
||||
static void set2 (lua_State *L, int i, int j) {
|
||||
lua_rawseti(L, 1, i);
|
||||
lua_rawseti(L, 1, j);
|
||||
}
|
||||
|
||||
static int sort_comp (lua_State *L, int a, int b) {
|
||||
/* WARNING: the caller (auxsort) must ensure stack space */
|
||||
if (!lua_isnil(L, 2)) { /* function? */
|
||||
int res;
|
||||
lua_pushvalue(L, 2);
|
||||
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
||||
lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
|
||||
lua_rawcall(L, 2, 1);
|
||||
res = !lua_isnil(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
}
|
||||
else /* a < b? */
|
||||
return lua_lessthan(L, a, b);
|
||||
}
|
||||
|
||||
static void auxsort (lua_State *L, int l, int u) {
|
||||
while (l < u) { /* for tail recursion */
|
||||
int i, j;
|
||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||
lua_rawgeti(L, 1, l);
|
||||
lua_rawgeti(L, 1, u);
|
||||
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
|
||||
set2(L, l, u); /* swap a[l] - a[u] */
|
||||
else
|
||||
lua_pop(L, 2);
|
||||
if (u-l == 1) break; /* only 2 elements */
|
||||
i = (l+u)/2;
|
||||
lua_rawgeti(L, 1, i);
|
||||
lua_rawgeti(L, 1, l);
|
||||
if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
|
||||
set2(L, i, l);
|
||||
else {
|
||||
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) lua_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) lua_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 luaB_sort (lua_State *L) {
|
||||
int n;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = lua_getn(L, 1);
|
||||
if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
lua_settop(L, 2); /* make sure there is two arguments */
|
||||
auxsort(L, 1, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Deprecated functions to manipulate global environment.
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
#define num_deprecated 4
|
||||
|
||||
static const struct luaL_reg deprecated_names [num_deprecated] = {
|
||||
{"foreachvar", luaB_foreach},
|
||||
{"nextvar", luaB_next},
|
||||
{"rawgetglobal", luaB_rawget},
|
||||
{"rawsetglobal", luaB_rawset}
|
||||
};
|
||||
|
||||
|
||||
#ifdef LUA_DEPRECATEDFUNCS
|
||||
|
||||
/*
|
||||
** call corresponding function inserting `globals' as first argument
|
||||
*/
|
||||
static int deprecated_func (lua_State *L) {
|
||||
lua_insert(L, 1); /* upvalue is the function to be called */
|
||||
lua_getglobals(L);
|
||||
lua_insert(L, 2); /* table of globals is 1o argument */
|
||||
lua_rawcall(L, lua_gettop(L)-1, LUA_MULTRET);
|
||||
return lua_gettop(L); /* return all results */
|
||||
}
|
||||
|
||||
|
||||
static void deprecated_funcs (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<num_deprecated; i++) {
|
||||
lua_pushcfunction(L, deprecated_names[i].func);
|
||||
lua_pushcclosure(L, deprecated_func, 1);
|
||||
lua_setglobal(L, deprecated_names[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
||||
/*
|
||||
** gives an explicit error in any attempt to call a deprecated function
|
||||
*/
|
||||
static int deprecated_func (lua_State *L) {
|
||||
luaL_verror(L, "function `%.20s' is deprecated", lua_tostring(L, -1));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
static void deprecated_funcs (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<num_deprecated; i++) {
|
||||
lua_pushstring(L, deprecated_names[i].name);
|
||||
lua_pushcclosure(L, deprecated_func, 1);
|
||||
lua_setglobal(L, deprecated_names[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
static const struct luaL_reg base_funcs[] = {
|
||||
{LUA_ALERT, luaB__ALERT},
|
||||
{LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
|
||||
{"call", luaB_call},
|
||||
{"collectgarbage", luaB_collectgarbage},
|
||||
{"copytagmethods", luaB_copytagmethods},
|
||||
{"dofile", luaB_dofile},
|
||||
{"dostring", luaB_dostring},
|
||||
{"error", luaB_error},
|
||||
{"foreach", luaB_foreach},
|
||||
{"foreachi", luaB_foreachi},
|
||||
{"gcinfo", luaB_gcinfo},
|
||||
{"getglobal", luaB_getglobal},
|
||||
{"gettagmethod", luaB_gettagmethod},
|
||||
{"globals", luaB_globals},
|
||||
{"newtag", luaB_newtag},
|
||||
{"next", luaB_next},
|
||||
{"print", luaB_print},
|
||||
{"rawget", luaB_rawget},
|
||||
{"rawset", luaB_rawset},
|
||||
{"rawgettable", luaB_rawget}, /* for compatibility */
|
||||
{"rawsettable", luaB_rawset}, /* for compatibility */
|
||||
{"setglobal", luaB_setglobal},
|
||||
{"settag", luaB_settag},
|
||||
{"settagmethod", luaB_settagmethod},
|
||||
{"tag", luaB_tag},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
{"type", luaB_type},
|
||||
{"assert", luaB_assert},
|
||||
{"getn", luaB_getn},
|
||||
{"sort", luaB_sort},
|
||||
{"tinsert", luaB_tinsert},
|
||||
{"tremove", luaB_tremove}
|
||||
};
|
||||
|
||||
|
||||
|
||||
LUALIB_API void lua_baselibopen (lua_State *L) {
|
||||
luaL_openl(L, base_funcs);
|
||||
lua_pushstring(L, LUA_VERSION);
|
||||
lua_setglobal(L, "_VERSION");
|
||||
deprecated_funcs(L);
|
||||
}
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuffer.c,v 1.11 1999/11/22 13:12:07 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*-------------------------------------------------------
|
||||
** Auxiliary buffer
|
||||
-------------------------------------------------------*/
|
||||
|
||||
|
||||
/*
|
||||
** amount of extra space (pre)allocated when buffer is reallocated
|
||||
*/
|
||||
#define EXTRABUFF 32
|
||||
|
||||
|
||||
#define openspace(L, size) if (L->Mbuffnext+(size) > L->Mbuffsize) \
|
||||
Openspace(L, size)
|
||||
|
||||
static void Openspace (lua_State *L, int size) {
|
||||
L->Mbuffsize = (L->Mbuffnext+size+EXTRABUFF)*2;
|
||||
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
|
||||
}
|
||||
|
||||
|
||||
char *luaL_openspace (lua_State *L, int size) {
|
||||
openspace(L, size);
|
||||
return L->Mbuffer+L->Mbuffnext;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addchar (lua_State *L, int c) {
|
||||
openspace(L, 1);
|
||||
L->Mbuffer[L->Mbuffnext++] = (char)c;
|
||||
}
|
||||
|
||||
|
||||
void luaL_resetbuffer (lua_State *L) {
|
||||
L->Mbuffnext = L->Mbuffbase;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addsize (lua_State *L, int n) {
|
||||
L->Mbuffnext += n;
|
||||
}
|
||||
|
||||
int luaL_getsize (lua_State *L) {
|
||||
return L->Mbuffnext-L->Mbuffbase;
|
||||
}
|
||||
|
||||
int luaL_newbuffer (lua_State *L, int size) {
|
||||
int old = L->Mbuffbase;
|
||||
openspace(L, size);
|
||||
L->Mbuffbase = L->Mbuffnext;
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
void luaL_oldbuffer (lua_State *L, int old) {
|
||||
L->Mbuffnext = L->Mbuffbase;
|
||||
L->Mbuffbase = old;
|
||||
}
|
||||
|
||||
|
||||
char *luaL_buffer (lua_State *L) {
|
||||
return L->Mbuffer+L->Mbuffbase;
|
||||
}
|
||||
|
||||
-662
@@ -1,662 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuiltin.c,v 1.105 2000/04/14 17:44:20 roberto Exp roberto $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** =========================================================================
|
||||
** All built-in functions are public (i.e. not static) and are named luaB_f,
|
||||
** where f is the function name in Lua. So, if you do not need all these
|
||||
** functions, you may register manually only the ones that you need.
|
||||
** =========================================================================
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lbuiltin.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** function defined in ltests.c, to open some internal-test functions
|
||||
*/
|
||||
void luaB_opentests (lua_State *L);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Auxiliary functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static void pushtagstring (lua_State *L, TString *s) {
|
||||
ttype(L->top) = TAG_STRING;
|
||||
tsvalue(L->top) = s;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
static Number getsize (const Hash *h) {
|
||||
Number max = 0;
|
||||
int i = h->size;
|
||||
Node *n = h->node;
|
||||
while (i--) {
|
||||
if (ttype(key(n)) == TAG_NUMBER &&
|
||||
ttype(val(n)) != TAG_NIL &&
|
||||
nvalue(key(n)) > max)
|
||||
max = nvalue(key(n));
|
||||
n++;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
|
||||
static Number getnarg (lua_State *L, const Hash *a) {
|
||||
TObject index;
|
||||
const TObject *value;
|
||||
/* value = table.n */
|
||||
ttype(&index) = TAG_STRING;
|
||||
tsvalue(&index) = luaS_new(L, "n");
|
||||
value = luaH_get(L, a, &index);
|
||||
return (ttype(value) == TAG_NUMBER) ? nvalue(value) : getsize(a);
|
||||
}
|
||||
|
||||
|
||||
static Hash *gettable (lua_State *L, int arg) {
|
||||
return avalue(luaL_tablearg(L, arg));
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Functions that use only the official API
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stderr', redefine this function, or
|
||||
** redefine _ERRORMESSAGE so that it won't need _ALERT.
|
||||
*/
|
||||
void luaB__ALERT (lua_State *L) {
|
||||
fputs(luaL_check_string(L, 1), stderr);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Standard implementation of _ERRORMESSAGE.
|
||||
** The library `liolib' redefines _ERRORMESSAGE for better error information.
|
||||
*/
|
||||
void luaB__ERRORMESSAGE (lua_State *L) {
|
||||
lua_Object al = lua_rawgetglobal(L, "_ALERT");
|
||||
if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */
|
||||
const char *s = luaL_check_string(L, 1);
|
||||
char *buff = luaL_openspace(L, strlen(s)+sizeof("error: \n"));
|
||||
strcpy(buff, "error: "); strcat(buff, s); strcat(buff, "\n");
|
||||
lua_pushstring(L, buff);
|
||||
lua_callfunction(L, al);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stdout', you can just remove this function.
|
||||
** If you need, you can define your own `print' function, following this
|
||||
** model but changing `fputs' to put the strings at a proper place
|
||||
** (a console window or a log file, for instance).
|
||||
*/
|
||||
void luaB_print (lua_State *L) {
|
||||
lua_Object args[MAXPRINT];
|
||||
lua_Object obj;
|
||||
int n = 0;
|
||||
int i;
|
||||
while ((obj = lua_getparam(L, n+1)) != LUA_NOOBJECT) {
|
||||
luaL_arg_check(L, n < MAXPRINT, n+1, "too many arguments");
|
||||
args[n++] = obj;
|
||||
}
|
||||
for (i=0; i<n; i++) {
|
||||
lua_pushobject(L, args[i]);
|
||||
if (lua_call(L, "tostring"))
|
||||
lua_error(L, "error in `tostring' called by `print'");
|
||||
obj = lua_getresult(L, 1);
|
||||
if (!lua_isstring(L, obj))
|
||||
lua_error(L, "`tostring' must return a string to `print'");
|
||||
if (i>0) fputs("\t", stdout);
|
||||
fputs(lua_getstring(L, obj), stdout);
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
}
|
||||
|
||||
|
||||
void luaB_tonumber (lua_State *L) {
|
||||
int base = luaL_opt_int(L, 2, 10);
|
||||
if (base == 10) { /* standard conversion */
|
||||
lua_Object o = luaL_nonnullarg(L, 1);
|
||||
if (lua_isnumber(L, o)) {
|
||||
lua_pushnumber(L, lua_getnumber(L, o));
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const char *s1 = luaL_check_string(L, 1);
|
||||
char *s2;
|
||||
Number n;
|
||||
luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
n = strtoul(s1, &s2, base);
|
||||
if (s1 != s2) { /* at least one valid digit? */
|
||||
while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
|
||||
if (*s2 == '\0') { /* no invalid trailing characters? */
|
||||
lua_pushnumber(L, n);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_pushnil(L); /* else not a number */
|
||||
}
|
||||
|
||||
|
||||
void luaB_error (lua_State *L) {
|
||||
lua_error(L, luaL_opt_string(L, 1, NULL));
|
||||
}
|
||||
|
||||
void luaB_setglobal (lua_State *L) {
|
||||
const char *name = luaL_check_string(L, 1);
|
||||
lua_Object value = luaL_nonnullarg(L, 2);
|
||||
lua_pushobject(L, value);
|
||||
lua_setglobal(L, name);
|
||||
}
|
||||
|
||||
void luaB_rawsetglobal (lua_State *L) {
|
||||
const char *name = luaL_check_string(L, 1);
|
||||
lua_Object value = luaL_nonnullarg(L, 2);
|
||||
lua_pushobject(L, value);
|
||||
lua_rawsetglobal(L, name);
|
||||
}
|
||||
|
||||
void luaB_getglobal (lua_State *L) {
|
||||
lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1)));
|
||||
}
|
||||
|
||||
void luaB_rawgetglobal (lua_State *L) {
|
||||
lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1)));
|
||||
}
|
||||
|
||||
void luaB_tag (lua_State *L) {
|
||||
lua_pushnumber(L, lua_tag(L, luaL_nonnullarg(L, 1)));
|
||||
}
|
||||
|
||||
void luaB_settag (lua_State *L) {
|
||||
lua_Object o = luaL_tablearg(L, 1);
|
||||
lua_pushobject(L, o);
|
||||
lua_settag(L, luaL_check_int(L, 2));
|
||||
lua_pushobject(L, o); /* return first argument */
|
||||
}
|
||||
|
||||
void luaB_newtag (lua_State *L) {
|
||||
lua_pushnumber(L, lua_newtag(L));
|
||||
}
|
||||
|
||||
void luaB_copytagmethods (lua_State *L) {
|
||||
lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
|
||||
luaL_check_int(L, 2)));
|
||||
}
|
||||
|
||||
void luaB_rawgettable (lua_State *L) {
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 1));
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 2));
|
||||
lua_pushobject(L, lua_rawgettable(L));
|
||||
}
|
||||
|
||||
void luaB_rawsettable (lua_State *L) {
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 1));
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 2));
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 3));
|
||||
lua_rawsettable(L);
|
||||
}
|
||||
|
||||
void luaB_settagmethod (lua_State *L) {
|
||||
int tag = luaL_check_int(L, 1);
|
||||
const char *event = luaL_check_string(L, 2);
|
||||
lua_Object nf = luaL_nonnullarg(L, 3);
|
||||
luaL_arg_check(L, lua_isnil(L, nf) || lua_isfunction(L, nf), 3,
|
||||
"function or nil expected");
|
||||
if (strcmp(event, "gc") == 0 && tag != TAG_NIL)
|
||||
lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
|
||||
lua_pushobject(L, nf);
|
||||
lua_pushobject(L, lua_settagmethod(L, tag, event));
|
||||
}
|
||||
|
||||
void luaB_gettagmethod (lua_State *L) {
|
||||
lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1),
|
||||
luaL_check_string(L, 2)));
|
||||
}
|
||||
|
||||
|
||||
void luaB_collectgarbage (lua_State *L) {
|
||||
lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
|
||||
}
|
||||
|
||||
|
||||
void luaB_type (lua_State *L) {
|
||||
lua_Object o = luaL_nonnullarg(L, 1);
|
||||
lua_pushstring(L, lua_type(L, o));
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Functions that could use only the official API but
|
||||
** do not, for efficiency.
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static void passresults (lua_State *L) {
|
||||
L->Cstack.base = L->Cstack.lua2C; /* position of first result */
|
||||
if (L->Cstack.num == 0)
|
||||
lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
void luaB_dostring (lua_State *L) {
|
||||
long l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
if (*s == ID_CHUNK)
|
||||
lua_error(L, "`dostring' cannot run pre-compiled code");
|
||||
if (lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)) == 0)
|
||||
passresults(L);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
void luaB_dofile (lua_State *L) {
|
||||
const char *fname = luaL_opt_string(L, 1, NULL);
|
||||
if (lua_dofile(L, fname) == 0)
|
||||
passresults(L);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
void luaB_call (lua_State *L) {
|
||||
lua_Object f = luaL_nonnullarg(L, 1);
|
||||
const Hash *arg = gettable(L, 2);
|
||||
const char *options = luaL_opt_string(L, 3, "");
|
||||
lua_Object err = lua_getparam(L, 4);
|
||||
int narg = (int)getnarg(L, arg);
|
||||
int i, status;
|
||||
if (err != LUA_NOOBJECT) { /* set new error method */
|
||||
lua_Object oldem = lua_getglobal(L, "_ERRORMESSAGE");
|
||||
lua_pushobject(L, err);
|
||||
lua_setglobal(L, "_ERRORMESSAGE");
|
||||
err = oldem;
|
||||
}
|
||||
/* push arg[1...n] */
|
||||
luaD_checkstack(L, narg);
|
||||
for (i=0; i<narg; i++)
|
||||
*(L->top++) = *luaH_getint(L, arg, i+1);
|
||||
status = lua_callfunction(L, f);
|
||||
if (err != LUA_NOOBJECT) { /* restore old error method */
|
||||
lua_pushobject(L, err);
|
||||
lua_setglobal(L, "_ERRORMESSAGE");
|
||||
}
|
||||
if (status != 0) { /* error in call? */
|
||||
if (strchr(options, 'x')) {
|
||||
lua_pushnil(L);
|
||||
return; /* return nil to signal the error */
|
||||
}
|
||||
else
|
||||
lua_error(L, NULL); /* propagate error without additional messages */
|
||||
}
|
||||
else { /* no errors */
|
||||
if (strchr(options, 'p')) { /* pack results? */
|
||||
luaV_pack(L, L->Cstack.lua2C, L->Cstack.num, L->top);
|
||||
incr_top;
|
||||
}
|
||||
else
|
||||
L->Cstack.base = L->Cstack.lua2C; /* position of first result */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaB_nextvar (lua_State *L) {
|
||||
lua_Object o = lua_getparam(L, 1);
|
||||
TString *name;
|
||||
if (o == LUA_NOOBJECT || ttype(o) == TAG_NIL)
|
||||
name = NULL;
|
||||
else {
|
||||
luaL_arg_check(L, ttype(o) == TAG_STRING, 1, "variable name expected");
|
||||
name = tsvalue(o);
|
||||
}
|
||||
if (!luaA_nextvar(L, name))
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
void luaB_next (lua_State *L) {
|
||||
const Hash *a = gettable(L, 1);
|
||||
lua_Object k = lua_getparam(L, 2);
|
||||
int i; /* `luaA_next' gets first element after `i' */
|
||||
if (k == LUA_NOOBJECT || ttype(k) == TAG_NIL)
|
||||
i = 0; /* get first */
|
||||
else {
|
||||
i = luaH_pos(L, a, k)+1;
|
||||
luaL_arg_check(L, i != 0, 2, "key not found");
|
||||
}
|
||||
if (luaA_next(L, a, i) == 0)
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
void luaB_tostring (lua_State *L) {
|
||||
lua_Object o = luaL_nonnullarg(L, 1);
|
||||
char buff[64];
|
||||
switch (ttype(o)) {
|
||||
case TAG_NUMBER:
|
||||
lua_pushstring(L, lua_getstring(L, o));
|
||||
return;
|
||||
case TAG_STRING:
|
||||
lua_pushobject(L, o);
|
||||
return;
|
||||
case TAG_TABLE:
|
||||
sprintf(buff, "table: %p", o->value.a);
|
||||
break;
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
sprintf(buff, "function: %p", o->value.cl);
|
||||
break;
|
||||
case TAG_USERDATA:
|
||||
sprintf(buff, "userdata: %p(%d)", o->value.ts->u.d.value,
|
||||
o->value.ts->u.d.tag);
|
||||
break;
|
||||
case TAG_NIL:
|
||||
lua_pushstring(L, "nil");
|
||||
return;
|
||||
default:
|
||||
LUA_INTERNALERROR(L, "invalid type");
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** "Extra" functions
|
||||
** These functions can be written in Lua, so you can
|
||||
** delete them if you need a tiny Lua implementation.
|
||||
** If you delete them, remove their entries in array
|
||||
** "builtin_funcs".
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
void luaB_assert (lua_State *L) {
|
||||
lua_Object p = luaL_nonnullarg(L, 1);
|
||||
if (lua_isnil(L, p))
|
||||
luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
|
||||
}
|
||||
|
||||
|
||||
void luaB_foreachi (lua_State *L) {
|
||||
const Hash *t = gettable(L, 1);
|
||||
int n = (int)getnarg(L, t);
|
||||
int i;
|
||||
lua_Object f = luaL_functionarg(L, 2);
|
||||
luaD_checkstack(L, 3); /* for f, key, and val */
|
||||
for (i=1; i<=n; i++) {
|
||||
*(L->top++) = *f;
|
||||
ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i;
|
||||
*(L->top++) = *luaH_getint(L, t, i);
|
||||
luaD_call(L, L->top-3, 1);
|
||||
if (ttype(L->top-1) != TAG_NIL)
|
||||
return;
|
||||
L->top--; /* remove nil result */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaB_foreach (lua_State *L) {
|
||||
const Hash *a = gettable(L, 1);
|
||||
lua_Object f = luaL_functionarg(L, 2);
|
||||
int i;
|
||||
luaD_checkstack(L, 3); /* for f, key, and val */
|
||||
for (i=0; i<a->size; i++) {
|
||||
const Node *nd = &(a->node[i]);
|
||||
if (ttype(val(nd)) != TAG_NIL) {
|
||||
*(L->top++) = *f;
|
||||
*(L->top++) = *key(nd);
|
||||
*(L->top++) = *val(nd);
|
||||
luaD_call(L, L->top-3, 1);
|
||||
if (ttype(L->top-1) != TAG_NIL)
|
||||
return;
|
||||
L->top--; /* remove result */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaB_foreachvar (lua_State *L) {
|
||||
lua_Object f = luaL_functionarg(L, 1);
|
||||
GlobalVar *gv;
|
||||
luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */
|
||||
for (gv = L->rootglobal; gv; gv = gv->next) {
|
||||
if (gv->value.ttype != TAG_NIL) {
|
||||
pushtagstring(L, gv->name); /* keep (extra) name on stack to avoid GC */
|
||||
*(L->top++) = *f;
|
||||
pushtagstring(L, gv->name);
|
||||
*(L->top++) = gv->value;
|
||||
luaD_call(L, L->top-3, 1);
|
||||
if (ttype(L->top-1) != TAG_NIL) {
|
||||
*(L->top-2) = *(L->top-1); /* remove extra name */
|
||||
L->top--;
|
||||
return;
|
||||
}
|
||||
L->top-=2; /* remove result and extra name */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaB_getn (lua_State *L) {
|
||||
lua_pushnumber(L, getnarg(L, gettable(L, 1)));
|
||||
}
|
||||
|
||||
|
||||
void luaB_tinsert (lua_State *L) {
|
||||
Hash *a = gettable(L, 1);
|
||||
lua_Object v = lua_getparam(L, 3);
|
||||
int n = (int)getnarg(L, a);
|
||||
int pos;
|
||||
if (v != LUA_NOOBJECT)
|
||||
pos = luaL_check_int(L, 2);
|
||||
else { /* called with only 2 arguments */
|
||||
v = luaL_nonnullarg(L, 2);
|
||||
pos = n+1;
|
||||
}
|
||||
luaV_setn(L, a, n+1); /* a.n = n+1 */
|
||||
for (; n>=pos; n--)
|
||||
luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */
|
||||
luaH_setint(L, a, pos, v); /* a[pos] = v */
|
||||
}
|
||||
|
||||
|
||||
void luaB_tremove (lua_State *L) {
|
||||
Hash *a = gettable(L, 1);
|
||||
int n = (int)getnarg(L, a);
|
||||
int pos = luaL_opt_int(L, 2, n);
|
||||
if (n <= 0) return; /* table is "empty" */
|
||||
luaA_pushobject(L, luaH_getint(L, a, pos)); /* result = a[pos] */
|
||||
for ( ;pos<n; pos++)
|
||||
luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */
|
||||
luaV_setn(L, a, n-1); /* a.n = n-1 */
|
||||
luaH_setint(L, a, n, &luaO_nilobject); /* a[n] = nil */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Quicksort
|
||||
** (based on `Algorithms in MODULA-3', Robert Sedgewick; Addison-Wesley, 1993.)
|
||||
*/
|
||||
|
||||
static void swap (lua_State *L, Hash *a, int i, int j) {
|
||||
TObject temp;
|
||||
temp = *luaH_getint(L, a, i);
|
||||
luaH_move(L, a, j, i);
|
||||
luaH_setint(L, a, j, &temp);
|
||||
}
|
||||
|
||||
static int sort_comp (lua_State *L, lua_Object f, const TObject *a,
|
||||
const TObject *b) {
|
||||
/* WARNING: the caller (auxsort) must ensure stack space */
|
||||
if (f != LUA_NOOBJECT) {
|
||||
*(L->top) = *f;
|
||||
*(L->top+1) = *a;
|
||||
*(L->top+2) = *b;
|
||||
L->top += 3;
|
||||
luaD_call(L, L->top-3, 1);
|
||||
L->top--;
|
||||
return (ttype(L->top) != TAG_NIL);
|
||||
}
|
||||
else /* a < b? */
|
||||
return luaV_lessthan(L, a, b, L->top);
|
||||
}
|
||||
|
||||
static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) {
|
||||
StkId P = L->top++; /* temporary place for pivot */
|
||||
ttype(P) = TAG_NIL;
|
||||
while (l < u) { /* for tail recursion */
|
||||
int i, j;
|
||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||
if (sort_comp(L, f, luaH_getint(L, a, u), luaH_getint(L, a, l)))
|
||||
swap(L, a, l, u); /* a[u]<a[l] */
|
||||
if (u-l == 1) break; /* only 2 elements */
|
||||
i = (l+u)/2;
|
||||
*P = *luaH_getint(L, a, i); /* P = a[i] */
|
||||
if (sort_comp(L, f, P, luaH_getint(L, a, l))) /* a[i]<a[l]? */
|
||||
swap(L, a, l, i);
|
||||
else if (sort_comp(L, f, luaH_getint(L, a, u), P)) /* a[u]<a[i]? */
|
||||
swap(L, a, i, u);
|
||||
if (u-l == 2) break; /* only 3 elements */
|
||||
*P = *luaH_getint(L, a, i); /* save pivot on stack (GC) */
|
||||
swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */
|
||||
/* a[l] <= P == a[u-1] <= a[u], only needs 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 (sort_comp(L, f, luaH_getint(L, a, ++i), P))
|
||||
if (i>u) lua_error(L, "invalid order function for sorting");
|
||||
/* repeat j-- until a[j] <= P */
|
||||
while (sort_comp(L, f, P, luaH_getint(L, a, --j)))
|
||||
if (j<l) lua_error(L, "invalid order function for sorting");
|
||||
if (j<i) break;
|
||||
swap(L, a, i, j);
|
||||
}
|
||||
swap(L, a, 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, a, j, i, f); /* call recursively the smaller one */
|
||||
} /* repeat the routine for the larger one */
|
||||
L->top--; /* remove pivot from stack */
|
||||
}
|
||||
|
||||
void luaB_sort (lua_State *L) {
|
||||
Hash *a = gettable(L, 1);
|
||||
int n = (int)getnarg(L, a);
|
||||
lua_Object func = lua_getparam(L, 2);
|
||||
luaL_arg_check(L, func == LUA_NOOBJECT || lua_isfunction(L, func), 2,
|
||||
"function expected");
|
||||
luaD_checkstack(L, 4); /* for pivot, f, a, b (sort_comp) */
|
||||
auxsort(L, a, 1, n, func);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static const struct luaL_reg builtin_funcs[] = {
|
||||
{"_ALERT", luaB__ALERT},
|
||||
{"_ERRORMESSAGE", luaB__ERRORMESSAGE},
|
||||
{"call", luaB_call},
|
||||
{"collectgarbage", luaB_collectgarbage},
|
||||
{"copytagmethods", luaB_copytagmethods},
|
||||
{"dofile", luaB_dofile},
|
||||
{"dostring", luaB_dostring},
|
||||
{"error", luaB_error},
|
||||
{"getglobal", luaB_getglobal},
|
||||
{"gettagmethod", luaB_gettagmethod},
|
||||
{"newtag", luaB_newtag},
|
||||
{"next", luaB_next},
|
||||
{"nextvar", luaB_nextvar},
|
||||
{"print", luaB_print},
|
||||
{"rawgetglobal", luaB_rawgetglobal},
|
||||
{"rawgettable", luaB_rawgettable},
|
||||
{"rawsetglobal", luaB_rawsetglobal},
|
||||
{"rawsettable", luaB_rawsettable},
|
||||
{"setglobal", luaB_setglobal},
|
||||
{"settag", luaB_settag},
|
||||
{"settagmethod", luaB_settagmethod},
|
||||
{"tag", luaB_tag},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
{"type", luaB_type},
|
||||
/* "Extra" functions */
|
||||
{"assert", luaB_assert},
|
||||
{"foreach", luaB_foreach},
|
||||
{"foreachi", luaB_foreachi},
|
||||
{"foreachvar", luaB_foreachvar},
|
||||
{"getn", luaB_getn},
|
||||
{"sort", luaB_sort},
|
||||
{"tinsert", luaB_tinsert},
|
||||
{"tremove", luaB_tremove}
|
||||
};
|
||||
|
||||
|
||||
void luaB_predefine (lua_State *L) {
|
||||
/* pre-register mem error messages, to avoid loop when error arises */
|
||||
luaS_newfixed(L, tableEM);
|
||||
luaS_newfixed(L, memEM);
|
||||
luaL_openl(L, builtin_funcs);
|
||||
#ifdef DEBUG
|
||||
luaB_opentests(L); /* internal test functions */
|
||||
#endif
|
||||
lua_pushstring(L, LUA_VERSION);
|
||||
lua_setglobal(L, "_VERSION");
|
||||
}
|
||||
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuiltin.h,v 1.6 2000/03/03 14:58:26 roberto Exp roberto $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lbuiltin_h
|
||||
#define lbuiltin_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
void luaB__ALERT (lua_State *L);
|
||||
void luaB__ERRORMESSAGE (lua_State *L);
|
||||
void luaB_assert (lua_State *L);
|
||||
void luaB_call (lua_State *L);
|
||||
void luaB_collectgarbage (lua_State *L);
|
||||
void luaB_copytagmethods (lua_State *L);
|
||||
void luaB_dofile (lua_State *L);
|
||||
void luaB_dostring (lua_State *L);
|
||||
void luaB_error (lua_State *L);
|
||||
void luaB_foreach (lua_State *L);
|
||||
void luaB_foreachi (lua_State *L);
|
||||
void luaB_foreachvar (lua_State *L);
|
||||
void luaB_getglobal (lua_State *L);
|
||||
void luaB_getn (lua_State *L);
|
||||
void luaB_gettagmethod (lua_State *L);
|
||||
void luaB_newtag (lua_State *L);
|
||||
void luaB_next (lua_State *L);
|
||||
void luaB_nextvar (lua_State *L);
|
||||
void luaB_print (lua_State *L);
|
||||
void luaB_rawgetglobal (lua_State *L);
|
||||
void luaB_rawgettable (lua_State *L);
|
||||
void luaB_rawsetglobal (lua_State *L);
|
||||
void luaB_rawsettable (lua_State *L);
|
||||
void luaB_setglobal (lua_State *L);
|
||||
void luaB_settag (lua_State *L);
|
||||
void luaB_settagmethod (lua_State *L);
|
||||
void luaB_sort (lua_State *L);
|
||||
void luaB_tag (lua_State *L);
|
||||
void luaB_tinsert (lua_State *L);
|
||||
void luaB_tonumber (lua_State *L);
|
||||
void luaB_tostring (lua_State *L);
|
||||
void luaB_tremove (lua_State *L);
|
||||
void luaB_type (lua_State *L);
|
||||
|
||||
void luaB_predefine (lua_State *L);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcode.c,v 1.27 2000/04/17 14:05:34 roberto Exp roberto $
|
||||
** $Id: lcode.c,v 1.50 2000/08/31 14:08:27 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "stdlib.h"
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lcode.h"
|
||||
#include "ldo.h"
|
||||
@@ -16,11 +16,10 @@
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
#include "lstring.h"
|
||||
|
||||
|
||||
void luaK_error (LexState *ls, const char *msg) {
|
||||
luaX_error(ls, msg, ls->token);
|
||||
luaX_error(ls, msg, ls->t.token);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +37,7 @@ static Instruction previous_instruction (FuncState *fs) {
|
||||
|
||||
|
||||
int luaK_jump (FuncState *fs) {
|
||||
int j = luaK_code0(fs, OP_JMP);
|
||||
int j = luaK_code1(fs, OP_JMP, NO_JUMP);
|
||||
if (j == fs->lasttarget) { /* possible jumps to this jump? */
|
||||
luaK_concat(fs, &j, fs->jlt); /* keep them on hold */
|
||||
fs->jlt = NO_JUMP;
|
||||
@@ -70,16 +69,16 @@ static int luaK_getjump (FuncState *fs, int pc) {
|
||||
|
||||
|
||||
/*
|
||||
** discharge list of jumps to last target.
|
||||
** returns current `pc' and marks it as a jump target (to avoid wrong
|
||||
** optimizations with consecutive instructions not in the same basic block).
|
||||
** discharge list of jumps to last target.
|
||||
*/
|
||||
int luaK_getlabel (FuncState *fs) {
|
||||
if (fs->pc != fs->lasttarget) {
|
||||
int lasttarget = fs->lasttarget;
|
||||
fs->lasttarget = fs->pc;
|
||||
luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
|
||||
fs->jlt = NO_JUMP; /* nobody jumps to this new label (till now) */
|
||||
fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
|
||||
}
|
||||
return fs->pc;
|
||||
}
|
||||
@@ -87,7 +86,7 @@ int luaK_getlabel (FuncState *fs) {
|
||||
|
||||
void luaK_deltastack (FuncState *fs, int delta) {
|
||||
fs->stacklevel += delta;
|
||||
if (delta > 0 && fs->stacklevel > fs->f->maxstacksize) {
|
||||
if (fs->stacklevel > fs->f->maxstacksize) {
|
||||
if (fs->stacklevel > MAXSTACK)
|
||||
luaK_error(fs->ls, "function or expression too complex");
|
||||
fs->f->maxstacksize = fs->stacklevel;
|
||||
@@ -100,7 +99,7 @@ void luaK_kstr (LexState *ls, int c) {
|
||||
}
|
||||
|
||||
|
||||
static int real_constant (FuncState *fs, Number r) {
|
||||
static int number_constant (FuncState *fs, Number r) {
|
||||
/* check whether `r' has appeared within the last LOOKBACKNUMS entries */
|
||||
Proto *f = fs->f;
|
||||
int c = f->nknum;
|
||||
@@ -108,7 +107,8 @@ static int real_constant (FuncState *fs, Number r) {
|
||||
while (--c >= lim)
|
||||
if (f->knum[c] == r) return c;
|
||||
/* not found; create a new entry */
|
||||
luaM_growvector(fs->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U);
|
||||
luaM_growvector(fs->L, f->knum, f->nknum, 1, Number,
|
||||
"constant table overflow", MAXARG_U);
|
||||
c = f->nknum++;
|
||||
f->knum[c] = r;
|
||||
return c;
|
||||
@@ -116,17 +116,17 @@ static int real_constant (FuncState *fs, Number r) {
|
||||
|
||||
|
||||
void luaK_number (FuncState *fs, Number f) {
|
||||
if (f <= (Number)MAXARG_S && (int)f == f)
|
||||
if (f <= (Number)MAXARG_S && (Number)(int)f == f)
|
||||
luaK_code1(fs, OP_PUSHINT, (int)f); /* f has a short integer value */
|
||||
else
|
||||
luaK_code1(fs, OP_PUSHNUM, real_constant(fs, f));
|
||||
luaK_code1(fs, OP_PUSHNUM, number_constant(fs, f));
|
||||
}
|
||||
|
||||
|
||||
void luaK_adjuststack (FuncState *fs, int n) {
|
||||
if (n > 0)
|
||||
luaK_code1(fs, OP_POP, n);
|
||||
else if (n < 0)
|
||||
else
|
||||
luaK_code1(fs, OP_PUSHNIL, -n);
|
||||
}
|
||||
|
||||
@@ -148,11 +148,6 @@ void luaK_setcallreturns (FuncState *fs, int nresults) {
|
||||
}
|
||||
|
||||
|
||||
static void assertglobal (FuncState *fs, int index) {
|
||||
luaS_assertglobal(fs->L, fs->f->kstr[index]);
|
||||
}
|
||||
|
||||
|
||||
static int discharge (FuncState *fs, expdesc *var) {
|
||||
switch (var->k) {
|
||||
case VLOCAL:
|
||||
@@ -160,7 +155,6 @@ static int discharge (FuncState *fs, expdesc *var) {
|
||||
break;
|
||||
case VGLOBAL:
|
||||
luaK_code1(fs, OP_GETGLOBAL, var->u.index);
|
||||
assertglobal(fs, var->u.index); /* make sure that there is a global */
|
||||
break;
|
||||
case VINDEXED:
|
||||
luaK_code0(fs, OP_GETTABLE);
|
||||
@@ -176,7 +170,7 @@ static int discharge (FuncState *fs, expdesc *var) {
|
||||
|
||||
static void discharge1 (FuncState *fs, expdesc *var) {
|
||||
discharge(fs, var);
|
||||
/* if it has jumps it is already discharged */
|
||||
/* if it has jumps then it is already discharged */
|
||||
if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP)
|
||||
luaK_setcallreturns(fs, 1); /* call must return 1 value */
|
||||
}
|
||||
@@ -190,13 +184,12 @@ void luaK_storevar (LexState *ls, const expdesc *var) {
|
||||
break;
|
||||
case VGLOBAL:
|
||||
luaK_code1(fs, OP_SETGLOBAL, var->u.index);
|
||||
assertglobal(fs, var->u.index); /* make sure that there is a global */
|
||||
break;
|
||||
case VINDEXED: /* table is at top-3; pop 3 elements after operation */
|
||||
luaK_code2(fs, OP_SETTABLE, 3, 3);
|
||||
break;
|
||||
default:
|
||||
LUA_INTERNALERROR(ls->L, "invalid var kind to store");
|
||||
LUA_INTERNALERROR("invalid var kind to store");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +205,7 @@ static OpCode invertjump (OpCode op) {
|
||||
case OP_JMPT: case OP_JMPONT: return OP_JMPF;
|
||||
case OP_JMPF: case OP_JMPONF: return OP_JMPT;
|
||||
default:
|
||||
LUA_INTERNALERROR(NULL, "invalid jump instruction");
|
||||
LUA_INTERNALERROR("invalid jump instruction");
|
||||
return OP_END; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
@@ -273,22 +266,28 @@ void luaK_concat (FuncState *fs, int *l1, int l2) {
|
||||
|
||||
|
||||
static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) {
|
||||
int prevpos; /* position of last instruction */
|
||||
Instruction *previous;
|
||||
int *golist = &v->u.l.f;
|
||||
int *exitlist = &v->u.l.t;
|
||||
if (invert) { /* interchange `golist' and `exitlist' */
|
||||
int *temp = golist; golist = exitlist; exitlist = temp;
|
||||
int *golist, *exitlist;
|
||||
if (!invert) {
|
||||
golist = &v->u.l.f; /* go if false */
|
||||
exitlist = &v->u.l.t; /* exit if true */
|
||||
}
|
||||
else {
|
||||
golist = &v->u.l.t; /* go if true */
|
||||
exitlist = &v->u.l.f; /* exit if false */
|
||||
}
|
||||
discharge1(fs, v);
|
||||
previous = &fs->f->code[fs->pc-1];
|
||||
LUA_ASSERT(L, GET_OPCODE(*previous) != OP_SETLINE, "bad place to set line");
|
||||
if (ISJUMP(GET_OPCODE(*previous))) {
|
||||
prevpos = fs->pc-1;
|
||||
previous = &fs->f->code[prevpos];
|
||||
LUA_ASSERT(*previous==previous_instruction(fs), "no jump allowed here");
|
||||
if (!ISJUMP(GET_OPCODE(*previous)))
|
||||
prevpos = luaK_code1(fs, jump, NO_JUMP);
|
||||
else { /* last instruction is already a jump */
|
||||
if (invert)
|
||||
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
|
||||
}
|
||||
else
|
||||
luaK_code0(fs, jump);
|
||||
luaK_concat(fs, exitlist, fs->pc-1); /* insert last jump in `exitlist' */
|
||||
luaK_concat(fs, exitlist, prevpos); /* insert last jump in `exitlist' */
|
||||
luaK_patchlist(fs, *golist, luaK_getlabel(fs));
|
||||
*golist = NO_JUMP;
|
||||
}
|
||||
@@ -299,15 +298,14 @@ void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
|
||||
}
|
||||
|
||||
|
||||
void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
|
||||
static void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
|
||||
luaK_testgo(fs, v, 0, keepvalue ? OP_JMPONT : OP_JMPT);
|
||||
}
|
||||
|
||||
|
||||
static int code_label (FuncState *fs, OpCode op, int arg) {
|
||||
int j = luaK_getlabel(fs);
|
||||
luaK_code1(fs, op, arg);
|
||||
return j;
|
||||
luaK_getlabel(fs); /* those instructions may be jump targets */
|
||||
return luaK_code1(fs, op, arg);
|
||||
}
|
||||
|
||||
|
||||
@@ -315,7 +313,6 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
|
||||
FuncState *fs = ls->fs;
|
||||
if (!discharge(fs, v)) { /* `v' is an expression? */
|
||||
OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
|
||||
LUA_ASSERT(L, previous != OP_SETLINE, "bad place to set line");
|
||||
if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
|
||||
/* expression has no jumps */
|
||||
if (onlyone)
|
||||
@@ -326,14 +323,15 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
|
||||
int j = NO_JUMP; /* eventual jump over values */
|
||||
int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */
|
||||
int p_1 = NO_JUMP; /* position of an eventual PUSHINT */
|
||||
if (ISJUMP(previous) || need_value(fs, v->u.l.f, OP_JMPONF) ||
|
||||
need_value(fs, v->u.l.t, OP_JMPONT)) {
|
||||
if (ISJUMP(previous) || need_value(fs, v->u.l.f, OP_JMPONF)
|
||||
|| need_value(fs, v->u.l.t, OP_JMPONT)) {
|
||||
/* expression needs values */
|
||||
if (ISJUMP(previous))
|
||||
luaK_concat(fs, &v->u.l.t, fs->pc-1); /* put `previous' in t. list */
|
||||
else {
|
||||
j = code_label(fs, OP_JMP, 0); /* to jump over both pushes */
|
||||
luaK_deltastack(fs, -1); /* next PUSHes may be skipped */
|
||||
j = code_label(fs, OP_JMP, NO_JUMP); /* to jump over both pushes */
|
||||
/* correct stack for compiler and symbolic execution */
|
||||
luaK_adjuststack(fs, 1);
|
||||
}
|
||||
p_nil = code_label(fs, OP_PUSHNILJMP, 0);
|
||||
p_1 = code_label(fs, OP_PUSHINT, 1);
|
||||
@@ -348,9 +346,9 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
|
||||
}
|
||||
|
||||
|
||||
void luaK_prefix (LexState *ls, int op, expdesc *v) {
|
||||
void luaK_prefix (LexState *ls, UnOpr op, expdesc *v) {
|
||||
FuncState *fs = ls->fs;
|
||||
if (op == '-') {
|
||||
if (op == OPR_MINUS) {
|
||||
luaK_tostack(ls, v, 1);
|
||||
luaK_code0(fs, OP_MINUS);
|
||||
}
|
||||
@@ -368,47 +366,69 @@ void luaK_prefix (LexState *ls, int op, expdesc *v) {
|
||||
}
|
||||
|
||||
|
||||
void luaK_infix (LexState *ls, int op, expdesc *v) {
|
||||
void luaK_infix (LexState *ls, BinOpr op, expdesc *v) {
|
||||
FuncState *fs = ls->fs;
|
||||
if (op == TK_AND)
|
||||
luaK_goiftrue(fs, v, 1);
|
||||
else if (op == TK_OR)
|
||||
luaK_goiffalse(fs, v, 1);
|
||||
else
|
||||
luaK_tostack(ls, v, 1); /* all other binary operators need a value */
|
||||
switch (op) {
|
||||
case OPR_AND:
|
||||
luaK_goiftrue(fs, v, 1);
|
||||
break;
|
||||
case OPR_OR:
|
||||
luaK_goiffalse(fs, v, 1);
|
||||
break;
|
||||
default:
|
||||
luaK_tostack(ls, v, 1); /* all other binary operators need a value */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
|
||||
|
||||
static const struct {
|
||||
OpCode opcode; /* opcode for each binary operator */
|
||||
int arg; /* default argument for the opcode */
|
||||
} codes[] = { /* ORDER OPR */
|
||||
{OP_ADD, 0}, {OP_SUB, 0}, {OP_MULT, 0}, {OP_DIV, 0},
|
||||
{OP_POW, 0}, {OP_CONCAT, 2},
|
||||
{OP_JMPNE, NO_JUMP}, {OP_JMPEQ, NO_JUMP},
|
||||
{OP_JMPLT, NO_JUMP}, {OP_JMPLE, NO_JUMP},
|
||||
{OP_JMPGT, NO_JUMP}, {OP_JMPGE, NO_JUMP}
|
||||
};
|
||||
|
||||
|
||||
void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2) {
|
||||
FuncState *fs = ls->fs;
|
||||
if (op == TK_AND) {
|
||||
LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed");
|
||||
discharge1(fs, v2);
|
||||
v1->u.l.t = v2->u.l.t;
|
||||
luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
|
||||
}
|
||||
else if (op == TK_OR) {
|
||||
LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed");
|
||||
discharge1(fs, v2);
|
||||
v1->u.l.f = v2->u.l.f;
|
||||
luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
|
||||
}
|
||||
else {
|
||||
luaK_tostack(ls, v2, 1); /* `v2' must be a value */
|
||||
switch (op) {
|
||||
case '+': luaK_code0(fs, OP_ADD); break;
|
||||
case '-': luaK_code0(fs, OP_SUB); break;
|
||||
case '*': luaK_code0(fs, OP_MULT); break;
|
||||
case '/': luaK_code0(fs, OP_DIV); break;
|
||||
case '^': luaK_code0(fs, OP_POW); break;
|
||||
case TK_CONCAT: luaK_code1(fs, OP_CONCAT, 2); break;
|
||||
case TK_EQ: luaK_code0(fs, OP_JMPEQ); break;
|
||||
case TK_NE: luaK_code0(fs, OP_JMPNE); break;
|
||||
case '>': luaK_code0(fs, OP_JMPGT); break;
|
||||
case '<': luaK_code0(fs, OP_JMPLT); break;
|
||||
case TK_GE: luaK_code0(fs, OP_JMPGE); break;
|
||||
case TK_LE: luaK_code0(fs, OP_JMPLE); break;
|
||||
switch (op) {
|
||||
case OPR_AND: {
|
||||
LUA_ASSERT(v1->u.l.t == NO_JUMP, "list must be closed");
|
||||
discharge1(fs, v2);
|
||||
v1->u.l.t = v2->u.l.t;
|
||||
luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
|
||||
break;
|
||||
}
|
||||
case OPR_OR: {
|
||||
LUA_ASSERT(v1->u.l.f == NO_JUMP, "list must be closed");
|
||||
discharge1(fs, v2);
|
||||
v1->u.l.f = v2->u.l.f;
|
||||
luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
luaK_tostack(ls, v2, 1); /* `v2' must be a value */
|
||||
luaK_code1(fs, codes[op].opcode, codes[op].arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void codelineinfo (FuncState *fs) {
|
||||
Proto *f = fs->f;
|
||||
LexState *ls = fs->ls;
|
||||
if (ls->lastline > fs->lastline) {
|
||||
luaM_growvector(fs->L, f->lineinfo, f->nlineinfo, 2, int,
|
||||
"line info overflow", MAX_INT);
|
||||
if (ls->lastline > fs->lastline+1)
|
||||
f->lineinfo[f->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
|
||||
f->lineinfo[f->nlineinfo++] = fs->pc;
|
||||
fs->lastline = ls->lastline;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -423,221 +443,259 @@ int luaK_code1 (FuncState *fs, OpCode o, int arg1) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
|
||||
Instruction i = previous_instruction(fs);
|
||||
int delta = 0;
|
||||
enum {iO, iU, iS, iAB, iP} mode; /* instruction format (or iP to optimize) */
|
||||
mode = iP;
|
||||
|
||||
int delta = luaK_opproperties[o].push - luaK_opproperties[o].pop;
|
||||
int optm = 0; /* 1 when there is an optimization */
|
||||
switch (o) {
|
||||
|
||||
case OP_CLOSURE:
|
||||
case OP_CLOSURE: {
|
||||
delta = -arg2+1;
|
||||
mode = iAB;
|
||||
break;
|
||||
|
||||
case OP_SETLINE:
|
||||
mode = iU;
|
||||
break;
|
||||
|
||||
case OP_CALL:
|
||||
mode = iAB;
|
||||
break;
|
||||
|
||||
case OP_PUSHINT:
|
||||
delta = 1;
|
||||
mode = iS;
|
||||
break;
|
||||
|
||||
case OP_SETTABLE:
|
||||
}
|
||||
case OP_SETTABLE: {
|
||||
delta = -arg2;
|
||||
mode = iAB;
|
||||
break;
|
||||
|
||||
case OP_SETLIST:
|
||||
delta = -(arg2+1);
|
||||
mode = iAB;
|
||||
}
|
||||
case OP_SETLIST: {
|
||||
if (arg2 == 0) return NO_JUMP; /* nothing to do */
|
||||
delta = -arg2;
|
||||
break;
|
||||
|
||||
case OP_SETMAP:
|
||||
delta = -2*(arg1+1);
|
||||
mode = iU;
|
||||
}
|
||||
case OP_SETMAP: {
|
||||
if (arg1 == 0) return NO_JUMP; /* nothing to do */
|
||||
delta = -2*arg1;
|
||||
break;
|
||||
|
||||
case OP_FORLOOP:
|
||||
delta = -3;
|
||||
arg1 = NO_JUMP;
|
||||
mode = iS;
|
||||
break;
|
||||
|
||||
case OP_SETLOCAL:
|
||||
case OP_SETGLOBAL:
|
||||
delta = -1;
|
||||
mode = iU;
|
||||
break;
|
||||
|
||||
case OP_FORPREP:
|
||||
case OP_JMP:
|
||||
arg1 = NO_JUMP;
|
||||
mode = iS;
|
||||
break;
|
||||
|
||||
case OP_END:
|
||||
case OP_PUSHNILJMP:
|
||||
case OP_NOT:
|
||||
mode = iO;
|
||||
break;
|
||||
|
||||
case OP_PUSHSTRING:
|
||||
case OP_PUSHNUM:
|
||||
case OP_PUSHNEGNUM:
|
||||
case OP_PUSHUPVALUE:
|
||||
case OP_GETLOCAL:
|
||||
case OP_GETGLOBAL:
|
||||
case OP_PUSHSELF:
|
||||
case OP_CREATETABLE:
|
||||
delta = 1;
|
||||
mode = iU;
|
||||
break;
|
||||
|
||||
case OP_JMPLT:
|
||||
case OP_JMPLE:
|
||||
case OP_JMPGT:
|
||||
case OP_JMPGE:
|
||||
delta = -2;
|
||||
arg1 = NO_JUMP;
|
||||
mode = iS;
|
||||
break;
|
||||
|
||||
case OP_MULT:
|
||||
case OP_DIV:
|
||||
case OP_POW:
|
||||
delta = -1;
|
||||
mode = iO;
|
||||
break;
|
||||
|
||||
case OP_RETURN:
|
||||
}
|
||||
case OP_RETURN: {
|
||||
if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET) {
|
||||
SET_OPCODE(i, OP_TAILCALL);
|
||||
SETARG_B(i, arg1);
|
||||
optm = 1;
|
||||
}
|
||||
else mode = iU;
|
||||
break;
|
||||
|
||||
case OP_PUSHNIL:
|
||||
}
|
||||
case OP_PUSHNIL: {
|
||||
if (arg1 == 0) return NO_JUMP; /* nothing to do */
|
||||
delta = arg1;
|
||||
switch(GET_OPCODE(i)) {
|
||||
case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); break;
|
||||
default: mode = iU; break;
|
||||
case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); optm = 1; break;
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_POP:
|
||||
}
|
||||
case OP_POP: {
|
||||
if (arg1 == 0) return NO_JUMP; /* nothing to do */
|
||||
delta = -arg1;
|
||||
switch(GET_OPCODE(i)) {
|
||||
case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); break;
|
||||
default: mode = iU; break;
|
||||
case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); optm = 1; break;
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_GETTABLE:
|
||||
delta = -1;
|
||||
}
|
||||
case OP_GETTABLE: {
|
||||
switch(GET_OPCODE(i)) {
|
||||
case OP_PUSHSTRING: SET_OPCODE(i, OP_GETDOTTED); break; /* `t.x' */
|
||||
case OP_GETLOCAL: SET_OPCODE(i, OP_GETINDEXED); break; /* `t[i]' */
|
||||
default: mode = iO; break;
|
||||
case OP_PUSHSTRING: /* `t.x' */
|
||||
SET_OPCODE(i, OP_GETDOTTED);
|
||||
optm = 1;
|
||||
break;
|
||||
case OP_GETLOCAL: /* `t[i]' */
|
||||
SET_OPCODE(i, OP_GETINDEXED);
|
||||
optm = 1;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_ADD:
|
||||
delta = -1;
|
||||
}
|
||||
case OP_ADD: {
|
||||
switch(GET_OPCODE(i)) {
|
||||
case OP_PUSHINT: SET_OPCODE(i, OP_ADDI); break; /* `a+k' */
|
||||
default: mode = iO; break;
|
||||
case OP_PUSHINT: SET_OPCODE(i, OP_ADDI); optm = 1; break; /* `a+k' */
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_SUB:
|
||||
delta = -1;
|
||||
}
|
||||
case OP_SUB: {
|
||||
switch(GET_OPCODE(i)) {
|
||||
case OP_PUSHINT: i = CREATE_S(OP_ADDI, -GETARG_S(i)); break; /* `a-k' */
|
||||
default: mode = iO; break;
|
||||
case OP_PUSHINT: /* `a-k' */
|
||||
i = CREATE_S(OP_ADDI, -GETARG_S(i));
|
||||
optm = 1;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_CONCAT:
|
||||
}
|
||||
case OP_CONCAT: {
|
||||
delta = -arg1+1;
|
||||
switch(GET_OPCODE(i)) {
|
||||
case OP_CONCAT: SETARG_U(i, GETARG_U(i)+1); break; /* `a..b..c' */
|
||||
default: mode = iU; break;
|
||||
case OP_CONCAT: /* `a..b..c' */
|
||||
SETARG_U(i, GETARG_U(i)+1);
|
||||
optm = 1;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_MINUS:
|
||||
}
|
||||
case OP_MINUS: {
|
||||
switch(GET_OPCODE(i)) {
|
||||
case OP_PUSHINT: SETARG_S(i, -GETARG_S(i)); break; /* `-k' */
|
||||
case OP_PUSHNUM: SET_OPCODE(i, OP_PUSHNEGNUM); break; /* `-k' */
|
||||
default: mode = iO; break;
|
||||
case OP_PUSHINT: /* `-k' */
|
||||
SETARG_S(i, -GETARG_S(i));
|
||||
optm = 1;
|
||||
break;
|
||||
case OP_PUSHNUM: /* `-k' */
|
||||
SET_OPCODE(i, OP_PUSHNEGNUM);
|
||||
optm = 1;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_JMPNE:
|
||||
delta = -2;
|
||||
if (i == CREATE_U(OP_PUSHNIL, 1)) /* `a~=nil' */
|
||||
}
|
||||
case OP_JMPNE: {
|
||||
if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a~=nil' */
|
||||
i = CREATE_S(OP_JMPT, NO_JUMP);
|
||||
else {
|
||||
arg1 = NO_JUMP;
|
||||
mode = iS;
|
||||
optm = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_JMPEQ:
|
||||
delta = -2;
|
||||
}
|
||||
case OP_JMPEQ: {
|
||||
if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */
|
||||
i = CREATE_0(OP_NOT);
|
||||
delta = -1; /* just undo effect of previous PUSHNIL */
|
||||
}
|
||||
else {
|
||||
arg1 = NO_JUMP;
|
||||
mode = iS;
|
||||
optm = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
case OP_JMPT:
|
||||
case OP_JMPF:
|
||||
case OP_JMPONT:
|
||||
case OP_JMPONF:
|
||||
delta = -1;
|
||||
arg1 = NO_JUMP;
|
||||
case OP_JMPONT: {
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_NOT: i = CREATE_S(invertjump(o), NO_JUMP); break;
|
||||
default: mode = iS; break;
|
||||
case OP_NOT: {
|
||||
i = CREATE_S(OP_JMPF, NO_JUMP);
|
||||
optm = 1;
|
||||
break;
|
||||
}
|
||||
case OP_PUSHINT: {
|
||||
if (o == OP_JMPT) { /* JMPONT must keep original integer value */
|
||||
i = CREATE_S(OP_JMP, NO_JUMP);
|
||||
optm = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_PUSHNIL: {
|
||||
if (GETARG_U(i) == 1) {
|
||||
fs->pc--; /* erase previous instruction */
|
||||
luaK_deltastack(fs, -1); /* correct stack */
|
||||
return NO_JUMP;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
case OP_JMPF:
|
||||
case OP_JMPONF: {
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_NOT: {
|
||||
i = CREATE_S(OP_JMPT, NO_JUMP);
|
||||
optm = 1;
|
||||
break;
|
||||
}
|
||||
case OP_PUSHINT: { /* `while 1 do ...' */
|
||||
fs->pc--; /* erase previous instruction */
|
||||
luaK_deltastack(fs, -1); /* correct stack */
|
||||
return NO_JUMP;
|
||||
}
|
||||
case OP_PUSHNIL: { /* `repeat ... until nil' */
|
||||
if (GETARG_U(i) == 1) {
|
||||
i = CREATE_S(OP_JMP, NO_JUMP);
|
||||
optm = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_GETDOTTED:
|
||||
case OP_GETINDEXED:
|
||||
case OP_TAILCALL:
|
||||
case OP_ADDI:
|
||||
LUA_INTERNALERROR(L, "instruction used only for optimizations");
|
||||
return 0; /* to avoid warnings */
|
||||
|
||||
case OP_ADDI: {
|
||||
LUA_INTERNALERROR("instruction used only for optimizations");
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
LUA_ASSERT(delta != VD, "invalid delta");
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaK_deltastack(fs, delta);
|
||||
switch (mode) { /* handle instruction formats */
|
||||
if (optm) { /* optimize: put instruction in place of last one */
|
||||
fs->f->code[fs->pc-1] = i; /* change previous instruction */
|
||||
return fs->pc-1; /* do not generate new instruction */
|
||||
}
|
||||
/* else build new instruction */
|
||||
switch ((enum Mode)luaK_opproperties[o].mode) {
|
||||
case iO: i = CREATE_0(o); break;
|
||||
case iU: i = CREATE_U(o, arg1); break;
|
||||
case iS: i = CREATE_S(o, arg1); break;
|
||||
case iAB: i = CREATE_AB(o, arg1, arg2); break;
|
||||
case iP: { /* optimize: put instruction in place of last one */
|
||||
fs->f->code[fs->pc-1] = i; /* change previous instruction */
|
||||
return fs->pc-1;
|
||||
}
|
||||
}
|
||||
/* actually create the new instruction */
|
||||
luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAX_INT);
|
||||
codelineinfo(fs);
|
||||
/* put new instruction in code array */
|
||||
luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction,
|
||||
"code size overflow", MAX_INT);
|
||||
fs->f->code[fs->pc] = i;
|
||||
return fs->pc++;
|
||||
}
|
||||
|
||||
|
||||
const struct OpProperties luaK_opproperties[NUM_OPCODES] = {
|
||||
{iO, 0, 0}, /* OP_END */
|
||||
{iU, 0, 0}, /* OP_RETURN */
|
||||
{iAB, 0, 0}, /* OP_CALL */
|
||||
{iAB, 0, 0}, /* OP_TAILCALL */
|
||||
{iU, VD, 0}, /* OP_PUSHNIL */
|
||||
{iU, VD, 0}, /* OP_POP */
|
||||
{iS, 1, 0}, /* OP_PUSHINT */
|
||||
{iU, 1, 0}, /* OP_PUSHSTRING */
|
||||
{iU, 1, 0}, /* OP_PUSHNUM */
|
||||
{iU, 1, 0}, /* OP_PUSHNEGNUM */
|
||||
{iU, 1, 0}, /* OP_PUSHUPVALUE */
|
||||
{iU, 1, 0}, /* OP_GETLOCAL */
|
||||
{iU, 1, 0}, /* OP_GETGLOBAL */
|
||||
{iO, 1, 2}, /* OP_GETTABLE */
|
||||
{iU, 1, 1}, /* OP_GETDOTTED */
|
||||
{iU, 1, 1}, /* OP_GETINDEXED */
|
||||
{iU, 2, 1}, /* OP_PUSHSELF */
|
||||
{iU, 1, 0}, /* OP_CREATETABLE */
|
||||
{iU, 0, 1}, /* OP_SETLOCAL */
|
||||
{iU, 0, 1}, /* OP_SETGLOBAL */
|
||||
{iAB, VD, 0}, /* OP_SETTABLE */
|
||||
{iAB, VD, 0}, /* OP_SETLIST */
|
||||
{iU, VD, 0}, /* OP_SETMAP */
|
||||
{iO, 1, 2}, /* OP_ADD */
|
||||
{iS, 1, 1}, /* OP_ADDI */
|
||||
{iO, 1, 2}, /* OP_SUB */
|
||||
{iO, 1, 2}, /* OP_MULT */
|
||||
{iO, 1, 2}, /* OP_DIV */
|
||||
{iO, 1, 2}, /* OP_POW */
|
||||
{iU, VD, 0}, /* OP_CONCAT */
|
||||
{iO, 1, 1}, /* OP_MINUS */
|
||||
{iO, 1, 1}, /* OP_NOT */
|
||||
{iS, 0, 2}, /* OP_JMPNE */
|
||||
{iS, 0, 2}, /* OP_JMPEQ */
|
||||
{iS, 0, 2}, /* OP_JMPLT */
|
||||
{iS, 0, 2}, /* OP_JMPLE */
|
||||
{iS, 0, 2}, /* OP_JMPGT */
|
||||
{iS, 0, 2}, /* OP_JMPGE */
|
||||
{iS, 0, 1}, /* OP_JMPT */
|
||||
{iS, 0, 1}, /* OP_JMPF */
|
||||
{iS, 0, 1}, /* OP_JMPONT */
|
||||
{iS, 0, 1}, /* OP_JMPONF */
|
||||
{iS, 0, 0}, /* OP_JMP */
|
||||
{iO, 0, 0}, /* OP_PUSHNILJMP */
|
||||
{iS, 0, 0}, /* OP_FORPREP */
|
||||
{iS, 0, 3}, /* OP_FORLOOP */
|
||||
{iS, 2, 0}, /* OP_LFORPREP */
|
||||
{iS, 0, 3}, /* OP_LFORLOOP */
|
||||
{iAB, VD, 0} /* OP_CLOSURE */
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcode.h,v 1.11 2000/04/07 19:35:20 roberto Exp roberto $
|
||||
** $Id: lcode.h,v 1.15 2000/06/28 20:20:36 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -20,6 +20,31 @@
|
||||
#define NO_JUMP (-1)
|
||||
|
||||
|
||||
/*
|
||||
** grep "ORDER OPR" if you change these enums
|
||||
*/
|
||||
typedef enum BinOpr {
|
||||
OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_POW,
|
||||
OPR_CONCAT,
|
||||
OPR_NE, OPR_EQ, OPR_LT, OPR_LE, OPR_GT, OPR_GE,
|
||||
OPR_AND, OPR_OR,
|
||||
OPR_NOBINOPR
|
||||
} BinOpr;
|
||||
|
||||
typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr;
|
||||
|
||||
|
||||
enum Mode {iO, iU, iS, iAB}; /* instruction format */
|
||||
|
||||
#define VD 100 /* flag for variable delta */
|
||||
|
||||
extern const struct OpProperties {
|
||||
char mode;
|
||||
unsigned char push;
|
||||
unsigned char pop;
|
||||
} luaK_opproperties[];
|
||||
|
||||
|
||||
void luaK_error (LexState *ls, const char *msg);
|
||||
int luaK_code0 (FuncState *fs, OpCode o);
|
||||
int luaK_code1 (FuncState *fs, OpCode o, int arg1);
|
||||
@@ -28,7 +53,6 @@ int luaK_jump (FuncState *fs);
|
||||
void luaK_patchlist (FuncState *fs, int list, int target);
|
||||
void luaK_concat (FuncState *fs, int *l1, int l2);
|
||||
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue);
|
||||
void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue);
|
||||
int luaK_getlabel (FuncState *fs);
|
||||
void luaK_deltastack (FuncState *fs, int delta);
|
||||
void luaK_kstr (LexState *ls, int c);
|
||||
@@ -38,9 +62,9 @@ int luaK_lastisopen (FuncState *fs);
|
||||
void luaK_setcallreturns (FuncState *fs, int nresults);
|
||||
void luaK_tostack (LexState *ls, expdesc *v, int onlyone);
|
||||
void luaK_storevar (LexState *ls, const expdesc *var);
|
||||
void luaK_prefix (LexState *ls, int op, expdesc *v);
|
||||
void luaK_infix (LexState *ls, int op, expdesc *v);
|
||||
void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2);
|
||||
void luaK_prefix (LexState *ls, UnOpr op, expdesc *v);
|
||||
void luaK_infix (LexState *ls, BinOpr op, expdesc *v);
|
||||
void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,176 +1,188 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.12 2000/03/30 17:19:48 roberto Exp roberto $
|
||||
** $Id: ldblib.c,v 1.28 2000/11/06 13:19:08 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static void settabss (lua_State *L, lua_Object t, const char *i, const char *v) {
|
||||
lua_pushobject(L, t);
|
||||
static void settabss (lua_State *L, const char *i, const char *v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushstring(L, v);
|
||||
lua_settable(L);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void settabsi (lua_State *L, lua_Object t, const char *i, int v) {
|
||||
lua_pushobject(L, t);
|
||||
static void settabsi (lua_State *L, const char *i, int v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushnumber(L, v);
|
||||
lua_settable(L);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void settabso (lua_State *L, lua_Object t, const char *i, lua_Object v) {
|
||||
lua_pushobject(L, t);
|
||||
lua_pushstring(L, i);
|
||||
lua_pushobject(L, v);
|
||||
lua_settable(L);
|
||||
}
|
||||
|
||||
|
||||
static void getstack (lua_State *L) {
|
||||
static int getinfo (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
return;
|
||||
else {
|
||||
const char *options = luaL_check_string(L, 2);
|
||||
lua_Object res = lua_createtable(L);
|
||||
if (!lua_getinfo(L, options, &ar))
|
||||
luaL_argerror(L, 2, "invalid option");
|
||||
for (; *options; options++) {
|
||||
switch (*options) {
|
||||
case 'S':
|
||||
settabss(L, res, "source", ar.source);
|
||||
settabsi(L, res, "linedefined", ar.linedefined);
|
||||
settabss(L, res, "what", ar.what);
|
||||
break;
|
||||
case 'l':
|
||||
settabsi(L, res, "currentline", ar.currentline);
|
||||
break;
|
||||
case 'u':
|
||||
settabsi(L, res, "nups", ar.nups);
|
||||
break;
|
||||
case 'n':
|
||||
settabss(L, res, "name", ar.name);
|
||||
settabss(L, res, "namewhat", ar.namewhat);
|
||||
break;
|
||||
case 'f':
|
||||
settabso(L, res, "func", ar.func);
|
||||
break;
|
||||
|
||||
}
|
||||
const char *options = luaL_opt_string(L, 2, "flnSu");
|
||||
char buff[20];
|
||||
if (lua_isnumber(L, 1)) {
|
||||
if (!lua_getstack(L, (int)lua_tonumber(L, 1), &ar)) {
|
||||
lua_pushnil(L); /* level out of range */
|
||||
return 1;
|
||||
}
|
||||
lua_pushobject(L, res);
|
||||
}
|
||||
else if (lua_isfunction(L, 1)) {
|
||||
lua_pushvalue(L, 1);
|
||||
sprintf(buff, ">%.10s", options);
|
||||
options = buff;
|
||||
}
|
||||
else
|
||||
luaL_argerror(L, 1, "function or level expected");
|
||||
if (!lua_getinfo(L, options, &ar))
|
||||
luaL_argerror(L, 2, "invalid option");
|
||||
lua_newtable(L);
|
||||
for (; *options; options++) {
|
||||
switch (*options) {
|
||||
case 'S':
|
||||
settabss(L, "source", ar.source);
|
||||
if (ar.source)
|
||||
settabss(L, "short_src", ar.short_src);
|
||||
settabsi(L, "linedefined", ar.linedefined);
|
||||
settabss(L, "what", ar.what);
|
||||
break;
|
||||
case 'l':
|
||||
settabsi(L, "currentline", ar.currentline);
|
||||
break;
|
||||
case 'u':
|
||||
settabsi(L, "nups", ar.nups);
|
||||
break;
|
||||
case 'n':
|
||||
settabss(L, "name", ar.name);
|
||||
settabss(L, "namewhat", ar.namewhat);
|
||||
break;
|
||||
case 'f':
|
||||
lua_pushstring(L, "func");
|
||||
lua_pushvalue(L, -3);
|
||||
lua_settable(L, -3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
|
||||
static void getlocal (lua_State *L) {
|
||||
static int getlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
lua_Localvar lvar;
|
||||
const char *name;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
luaL_argerror(L, 1, "level out of range");
|
||||
lvar.index = luaL_check_int(L, 2);
|
||||
if (lua_getlocal(L, &ar, &lvar)) {
|
||||
lua_pushstring(L, lvar.name);
|
||||
lua_pushobject(L, lvar.value);
|
||||
name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
|
||||
if (name) {
|
||||
lua_pushstring(L, name);
|
||||
lua_pushvalue(L, -2);
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
else lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
static void setlocal (lua_State *L) {
|
||||
static int setlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
lua_Localvar lvar;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
luaL_argerror(L, 1, "level out of range");
|
||||
lvar.index = luaL_check_int(L, 2);
|
||||
lvar.value = luaL_nonnullarg(L, 3);
|
||||
if (lua_setlocal(L, &ar, &lvar))
|
||||
lua_pushstring(L, lvar.name);
|
||||
else lua_pushnil(L);
|
||||
luaL_checkany(L, 3);
|
||||
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** because of these variables, this module is not reentrant, and should
|
||||
** not be used in multiple states
|
||||
*/
|
||||
|
||||
static int linehook = LUA_NOREF; /* Lua reference to line hook function */
|
||||
static int callhook = LUA_NOREF; /* Lua reference to call hook function */
|
||||
/* dummy variables (to define unique addresses) */
|
||||
static char key1, key2;
|
||||
#define KEY_CALLHOOK (&key1)
|
||||
#define KEY_LINEHOOK (&key2)
|
||||
|
||||
|
||||
|
||||
static void linef (lua_State *L, lua_Debug *ar) {
|
||||
if (linehook != LUA_NOREF) {
|
||||
lua_pushnumber(L, ar->currentline);
|
||||
lua_callfunction(L, lua_getref(L, linehook));
|
||||
static void hookf (lua_State *L, void *key) {
|
||||
lua_getregistry(L);
|
||||
lua_pushuserdata(L, key);
|
||||
lua_gettable(L, -2);
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushvalue(L, 1);
|
||||
lua_rawcall(L, 1, 0);
|
||||
}
|
||||
else
|
||||
lua_pop(L, 1); /* pop result from gettable */
|
||||
lua_pop(L, 1); /* pop table */
|
||||
}
|
||||
|
||||
|
||||
static void callf (lua_State *L, lua_Debug *ar) {
|
||||
if (callhook != LUA_NOREF) {
|
||||
lua_pushstring(L, ar->event);
|
||||
lua_callfunction(L, lua_getref(L, callhook));
|
||||
}
|
||||
lua_pushstring(L, ar->event);
|
||||
hookf(L, KEY_CALLHOOK);
|
||||
}
|
||||
|
||||
|
||||
static void setcallhook (lua_State *L) {
|
||||
lua_Object f = lua_getparam(L, 1);
|
||||
lua_unref(L, callhook);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
callhook = LUA_NOREF;
|
||||
lua_setcallhook(L, NULL);
|
||||
}
|
||||
else {
|
||||
lua_pushobject(L, f);
|
||||
callhook = lua_ref(L, 1);
|
||||
lua_setcallhook(L, callf);
|
||||
}
|
||||
static void linef (lua_State *L, lua_Debug *ar) {
|
||||
lua_pushnumber(L, ar->currentline);
|
||||
hookf(L, KEY_LINEHOOK);
|
||||
}
|
||||
|
||||
|
||||
static void setlinehook (lua_State *L) {
|
||||
lua_Object f = lua_getparam(L, 1);
|
||||
lua_unref(L, linehook);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
linehook = LUA_NOREF;
|
||||
lua_setlinehook(L, NULL);
|
||||
}
|
||||
else {
|
||||
lua_pushobject(L, f);
|
||||
linehook = lua_ref(L, 1);
|
||||
lua_setlinehook(L, linef);
|
||||
}
|
||||
static void sethook (lua_State *L, void *key, lua_Hook hook,
|
||||
lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
|
||||
lua_settop(L, 1);
|
||||
if (lua_isnil(L, 1))
|
||||
(*sethookf)(L, NULL);
|
||||
else if (lua_isfunction(L, 1))
|
||||
(*sethookf)(L, hook);
|
||||
else
|
||||
luaL_argerror(L, 1, "function expected");
|
||||
lua_getregistry(L);
|
||||
lua_pushuserdata(L, key);
|
||||
lua_pushvalue(L, -1); /* dup key */
|
||||
lua_gettable(L, -3); /* get old value */
|
||||
lua_pushvalue(L, -2); /* key (again) */
|
||||
lua_pushvalue(L, 1);
|
||||
lua_settable(L, -5); /* set new value */
|
||||
}
|
||||
|
||||
|
||||
static int setcallhook (lua_State *L) {
|
||||
sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int setlinehook (lua_State *L) {
|
||||
sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_reg dblib[] = {
|
||||
{"getlocal", getlocal},
|
||||
{"getstack", getstack},
|
||||
{"getinfo", getinfo},
|
||||
{"setcallhook", setcallhook},
|
||||
{"setlinehook", setlinehook},
|
||||
{"setlocal", setlocal}
|
||||
};
|
||||
|
||||
|
||||
void lua_dblibopen (lua_State *L) {
|
||||
LUALIB_API void lua_dblibopen (lua_State *L) {
|
||||
luaL_openl(L, dblib);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 1.15 2000/03/30 17:19:48 roberto Exp roberto $
|
||||
** $Id: ldebug.c,v 1.49 2000/10/27 11:39:52 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,64 +7,57 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lcode.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
static const lua_Type normtype[] = { /* ORDER LUA_T */
|
||||
TAG_USERDATA, TAG_NUMBER, TAG_STRING, TAG_TABLE,
|
||||
TAG_LCLOSURE, TAG_CCLOSURE, TAG_NIL,
|
||||
TAG_LCLOSURE, TAG_CCLOSURE /* TAG_LMARK, TAG_CMARK */
|
||||
};
|
||||
|
||||
static const char *getfuncname (lua_State *L, StkId f, const char **name);
|
||||
|
||||
|
||||
static void setnormalized (TObject *d, const TObject *s) {
|
||||
d->value = s->value;
|
||||
d->ttype = normtype[ttype(s)];
|
||||
if (ttype(s) == LUA_TMARK) {
|
||||
clvalue(d) = infovalue(s)->func;
|
||||
ttype(d) = LUA_TFUNCTION;
|
||||
}
|
||||
else *d = *s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int hasdebuginfo (lua_State *L, StkId f) {
|
||||
return (f+1 < L->top && (f+1)->ttype == TAG_LINE);
|
||||
static int isLmark (StkId o) {
|
||||
return (o && ttype(o) == LUA_TMARK && !infovalue(o)->func->isC);
|
||||
}
|
||||
|
||||
|
||||
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
|
||||
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook = L->callhook;
|
||||
L->callhook = func;
|
||||
return oldhook;
|
||||
}
|
||||
|
||||
|
||||
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
|
||||
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook = L->linehook;
|
||||
L->linehook = func;
|
||||
return oldhook;
|
||||
}
|
||||
|
||||
|
||||
int lua_setdebug (lua_State *L, int debug) {
|
||||
int old = L->debug;
|
||||
L->debug = debug;
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
|
||||
int i;
|
||||
for (i = (top-1)-L->stack; i>=0; i--) {
|
||||
for (i = (top-1) - L->stack; i>=0; i--) {
|
||||
if (is_T_MARK(L->stack[i].ttype)) {
|
||||
if (level == 0)
|
||||
return L->stack+i;
|
||||
@@ -75,7 +68,7 @@ static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
|
||||
}
|
||||
|
||||
|
||||
int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
StkId f = aux_stackedfunction(L, level, L->top);
|
||||
if (f == NULL) return 0; /* there is no such level */
|
||||
else {
|
||||
@@ -85,137 +78,389 @@ int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
}
|
||||
|
||||
|
||||
static int lua_nups (StkId f) {
|
||||
static int nups (StkId f) {
|
||||
switch (ttype(f)) {
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
case TAG_LMARK: case TAG_CMARK:
|
||||
return f->value.cl->nelems;
|
||||
case LUA_TFUNCTION:
|
||||
return clvalue(f)->nupvalues;
|
||||
case LUA_TMARK:
|
||||
return infovalue(f)->func->nupvalues;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int lua_currentline (lua_State *L, StkId f) {
|
||||
return hasdebuginfo(L, f) ? (f+1)->value.i : -1;
|
||||
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
|
||||
int refi;
|
||||
if (lineinfo == NULL || pc == -1)
|
||||
return -1; /* no line info or function is not active */
|
||||
refi = prefi ? *prefi : 0;
|
||||
if (lineinfo[refi] < 0)
|
||||
refline += -lineinfo[refi++];
|
||||
LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
|
||||
while (lineinfo[refi] > pc) {
|
||||
refline--;
|
||||
refi--;
|
||||
if (lineinfo[refi] < 0)
|
||||
refline -= -lineinfo[refi--];
|
||||
LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
|
||||
}
|
||||
for (;;) {
|
||||
int nextline = refline + 1;
|
||||
int nextref = refi + 1;
|
||||
if (lineinfo[nextref] < 0)
|
||||
nextline += -lineinfo[nextref++];
|
||||
LUA_ASSERT(lineinfo[nextref] >= 0, "invalid line info");
|
||||
if (lineinfo[nextref] > pc)
|
||||
break;
|
||||
refline = nextline;
|
||||
refi = nextref;
|
||||
}
|
||||
if (prefi) *prefi = refi;
|
||||
return refline;
|
||||
}
|
||||
|
||||
|
||||
static int currentpc (StkId f) {
|
||||
CallInfo *ci = infovalue(f);
|
||||
LUA_ASSERT(isLmark(f), "function has no pc");
|
||||
if (ci->pc)
|
||||
return (*ci->pc - ci->func->f.l->code) - 1;
|
||||
else
|
||||
return -1; /* function is not active */
|
||||
}
|
||||
|
||||
|
||||
static int currentline (StkId f) {
|
||||
if (!isLmark(f))
|
||||
return -1; /* only active lua functions have current-line information */
|
||||
else {
|
||||
CallInfo *ci = infovalue(f);
|
||||
int *lineinfo = ci->func->f.l->lineinfo;
|
||||
return luaG_getline(lineinfo, currentpc(f), 1, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static Proto *getluaproto (StkId f) {
|
||||
return (ttype(f) == TAG_LMARK) ? clvalue(f)->f.l : NULL;
|
||||
return (isLmark(f) ? infovalue(f)->func->f.l : NULL);
|
||||
}
|
||||
|
||||
|
||||
int lua_getlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
|
||||
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const char *name;
|
||||
StkId f = ar->_func;
|
||||
Proto *fp = getluaproto(f);
|
||||
if (!fp) return 0; /* `f' is not a Lua function? */
|
||||
v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
|
||||
if (!v->name) return 0;
|
||||
/* if `name', there must be a TAG_LINE */
|
||||
/* therefore, f+2 points to function base */
|
||||
LUA_ASSERT(L, ttype(f+1) == TAG_LINE, "");
|
||||
v->value = luaA_putluaObject(L, (f+2)+(v->index-1));
|
||||
return 1;
|
||||
if (!fp) return NULL; /* `f' is not a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(f));
|
||||
if (!name) return NULL;
|
||||
luaA_pushobject(L, (f+1)+(n-1)); /* push value */
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
int lua_setlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
|
||||
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const char *name;
|
||||
StkId f = ar->_func;
|
||||
Proto *fp = getluaproto(f);
|
||||
if (!fp) return 0; /* `f' is not a Lua function? */
|
||||
v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
|
||||
if (!v->name) return 0;
|
||||
LUA_ASSERT(L, ttype(f+1) == TAG_LINE, "");
|
||||
*((f+2)+(v->index-1)) = *v->value;
|
||||
return 1;
|
||||
L->top--; /* pop new value */
|
||||
if (!fp) return NULL; /* `f' is not a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(f));
|
||||
if (!name || name[0] == '(') return NULL; /* `(' starts private locals */
|
||||
*((f+1)+(n-1)) = *L->top;
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
static void lua_funcinfo (lua_Debug *ar) {
|
||||
StkId func = ar->_func;
|
||||
static void infoLproto (lua_Debug *ar, Proto *f) {
|
||||
ar->source = f->source->str;
|
||||
ar->linedefined = f->lineDefined;
|
||||
ar->what = "Lua";
|
||||
}
|
||||
|
||||
|
||||
static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
|
||||
Closure *cl = NULL;
|
||||
switch (ttype(func)) {
|
||||
case TAG_LCLOSURE: case TAG_LMARK:
|
||||
ar->source = clvalue(func)->f.l->source->str;
|
||||
ar->linedefined = clvalue(func)->f.l->lineDefined;
|
||||
ar->what = "Lua";
|
||||
case LUA_TFUNCTION:
|
||||
cl = clvalue(func);
|
||||
break;
|
||||
case TAG_CCLOSURE: case TAG_CMARK:
|
||||
ar->source = "(C)";
|
||||
ar->linedefined = -1;
|
||||
ar->what = "C";
|
||||
case LUA_TMARK:
|
||||
cl = infovalue(func)->func;
|
||||
break;
|
||||
default:
|
||||
LUA_INTERNALERROR(L, "invalid `func' value");
|
||||
lua_error(L, "value for `lua_getinfo' is not a function");
|
||||
}
|
||||
if (cl->isC) {
|
||||
ar->source = "=C";
|
||||
ar->linedefined = -1;
|
||||
ar->what = "C";
|
||||
}
|
||||
else
|
||||
infoLproto(ar, cl->f.l);
|
||||
luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
|
||||
if (ar->linedefined == 0)
|
||||
ar->what = "main";
|
||||
}
|
||||
|
||||
|
||||
static int checkfunc (lua_State *L, TObject *o) {
|
||||
return luaO_equalObj(o, L->top);
|
||||
static const char *travtagmethods (lua_State *L, const TObject *o) {
|
||||
if (ttype(o) == LUA_TFUNCTION) {
|
||||
int e;
|
||||
for (e=0; e<TM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
if (clvalue(o) == luaT_gettm(L, t, e))
|
||||
return luaT_eventname[e];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static void lua_getobjname (lua_State *L, StkId f, lua_Debug *ar) {
|
||||
GlobalVar *g;
|
||||
/* try to find a name for given function */
|
||||
setnormalized(L->top, f); /* to be used by `checkfunc' */
|
||||
for (g=L->rootglobal; g; g=g->next) {
|
||||
if (checkfunc(L, &g->value)) {
|
||||
ar->name = g->name->str;
|
||||
ar->namewhat = "global";
|
||||
return;
|
||||
}
|
||||
static const char *travglobals (lua_State *L, const TObject *o) {
|
||||
Hash *g = L->gt;
|
||||
int i;
|
||||
for (i=0; i<g->size; i++) {
|
||||
if (luaO_equalObj(o, val(node(g, i))) &&
|
||||
ttype(key(node(g, i))) == LUA_TSTRING)
|
||||
return tsvalue(key(node(g, i)))->str;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static void getname (lua_State *L, StkId f, lua_Debug *ar) {
|
||||
TObject o;
|
||||
setnormalized(&o, f);
|
||||
/* try to find a name for given function */
|
||||
if ((ar->name = travglobals(L, &o)) != NULL)
|
||||
ar->namewhat = "global";
|
||||
/* not found: try tag methods */
|
||||
if ((ar->name = luaT_travtagmethods(L, checkfunc)) != NULL)
|
||||
else if ((ar->name = travtagmethods(L, &o)) != NULL)
|
||||
ar->namewhat = "tag-method";
|
||||
else ar->namewhat = ""; /* not found at all */
|
||||
}
|
||||
|
||||
|
||||
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
StkId func = ar->_func;
|
||||
LUA_ASSERT(L, is_T_MARK(ttype(func)), "invalid activation record");
|
||||
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
StkId func;
|
||||
int isactive = (*what != '>');
|
||||
if (isactive)
|
||||
func = ar->_func;
|
||||
else {
|
||||
what++; /* skip the '>' */
|
||||
func = L->top - 1;
|
||||
}
|
||||
for (; *what; what++) {
|
||||
switch (*what) {
|
||||
case 'S':
|
||||
lua_funcinfo(ar);
|
||||
case 'S': {
|
||||
funcinfo(L, ar, func);
|
||||
break;
|
||||
case 'l':
|
||||
ar->currentline = lua_currentline(L, func);
|
||||
}
|
||||
case 'l': {
|
||||
ar->currentline = currentline(func);
|
||||
break;
|
||||
case 'u':
|
||||
ar->nups = lua_nups(func);
|
||||
}
|
||||
case 'u': {
|
||||
ar->nups = nups(func);
|
||||
break;
|
||||
case 'n':
|
||||
lua_getobjname(L, func, ar);
|
||||
}
|
||||
case 'n': {
|
||||
ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
|
||||
if (ar->namewhat == NULL)
|
||||
getname(L, func, ar);
|
||||
break;
|
||||
case 'f':
|
||||
}
|
||||
case 'f': {
|
||||
setnormalized(L->top, func);
|
||||
incr_top;
|
||||
ar->func = luaA_putObjectOnTop(L);
|
||||
incr_top; /* push function */
|
||||
break;
|
||||
}
|
||||
default: return 0; /* invalid option */
|
||||
}
|
||||
}
|
||||
if (!isactive) L->top--; /* pop function */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Symbolic Execution
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void call_index_error (lua_State *L, TObject *o, const char *v) {
|
||||
luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o));
|
||||
|
||||
static int pushpc (int *stack, int pc, int top, int n) {
|
||||
while (n--)
|
||||
stack[top++] = pc-1;
|
||||
return top;
|
||||
}
|
||||
|
||||
|
||||
void luaG_callerror (lua_State *L, TObject *func) {
|
||||
call_index_error(L, func, "call");
|
||||
static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
|
||||
int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
|
||||
const Instruction *code = pt->code;
|
||||
int top = pt->numparams;
|
||||
int pc = 0;
|
||||
if (pt->is_vararg) /* varargs? */
|
||||
top++; /* `arg' */
|
||||
while (pc < lastpc) {
|
||||
const Instruction i = code[pc++];
|
||||
LUA_ASSERT(0 <= top && top <= pt->maxstacksize, "wrong stack");
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_RETURN: {
|
||||
LUA_ASSERT(top >= GETARG_U(i), "wrong stack");
|
||||
top = GETARG_U(i);
|
||||
break;
|
||||
}
|
||||
case OP_TAILCALL: {
|
||||
LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
|
||||
top = GETARG_B(i);
|
||||
break;
|
||||
}
|
||||
case OP_CALL: {
|
||||
int nresults = GETARG_B(i);
|
||||
if (nresults == MULT_RET) nresults = 1;
|
||||
LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
|
||||
top = pushpc(stack, pc, GETARG_A(i), nresults);
|
||||
break;
|
||||
}
|
||||
case OP_PUSHNIL: {
|
||||
top = pushpc(stack, pc, top, GETARG_U(i));
|
||||
break;
|
||||
}
|
||||
case OP_POP: {
|
||||
top -= GETARG_U(i);
|
||||
break;
|
||||
}
|
||||
case OP_SETTABLE:
|
||||
case OP_SETLIST: {
|
||||
top -= GETARG_B(i);
|
||||
break;
|
||||
}
|
||||
case OP_SETMAP: {
|
||||
top -= 2*GETARG_U(i);
|
||||
break;
|
||||
}
|
||||
case OP_CONCAT: {
|
||||
top -= GETARG_U(i);
|
||||
stack[top++] = pc-1;
|
||||
break;
|
||||
}
|
||||
case OP_CLOSURE: {
|
||||
top -= GETARG_B(i);
|
||||
stack[top++] = pc-1;
|
||||
break;
|
||||
}
|
||||
case OP_JMPONT:
|
||||
case OP_JMPONF: {
|
||||
int newpc = pc + GETARG_S(i);
|
||||
/* jump is forward and do not skip `lastpc'? */
|
||||
if (pc < newpc && newpc <= lastpc) {
|
||||
stack[top-1] = pc-1; /* value comes from `and'/`or' */
|
||||
pc = newpc; /* do the jump */
|
||||
}
|
||||
else
|
||||
top--; /* do not jump; pop value */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
OpCode op = GET_OPCODE(i);
|
||||
LUA_ASSERT(luaK_opproperties[op].push != VD,
|
||||
"invalid opcode for default");
|
||||
top -= luaK_opproperties[op].pop;
|
||||
LUA_ASSERT(top >= 0, "wrong stack");
|
||||
top = pushpc(stack, pc, top, luaK_opproperties[op].push);
|
||||
}
|
||||
}
|
||||
}
|
||||
return code[stack[stackpos]];
|
||||
}
|
||||
|
||||
|
||||
void luaG_indexerror (lua_State *L, TObject *t) {
|
||||
call_index_error(L, t, "index");
|
||||
static const char *getobjname (lua_State *L, StkId obj, const char **name) {
|
||||
StkId func = aux_stackedfunction(L, 0, obj);
|
||||
if (!isLmark(func))
|
||||
return NULL; /* not an active Lua function */
|
||||
else {
|
||||
Proto *p = infovalue(func)->func->f.l;
|
||||
int pc = currentpc(func);
|
||||
int stackpos = obj - (func+1); /* func+1 == function base */
|
||||
Instruction i = luaG_symbexec(p, pc, stackpos);
|
||||
LUA_ASSERT(pc != -1, "function must be active");
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_GETGLOBAL: {
|
||||
*name = p->kstr[GETARG_U(i)]->str;
|
||||
return "global";
|
||||
}
|
||||
case OP_GETLOCAL: {
|
||||
*name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
|
||||
LUA_ASSERT(*name, "local must exist");
|
||||
return "local";
|
||||
}
|
||||
case OP_PUSHSELF:
|
||||
case OP_GETDOTTED: {
|
||||
*name = p->kstr[GETARG_U(i)]->str;
|
||||
return "field";
|
||||
}
|
||||
default:
|
||||
return NULL; /* no useful name found */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *getfuncname (lua_State *L, StkId f, const char **name) {
|
||||
StkId func = aux_stackedfunction(L, 0, f); /* calling function */
|
||||
if (!isLmark(func))
|
||||
return NULL; /* not an active Lua function */
|
||||
else {
|
||||
Proto *p = infovalue(func)->func->f.l;
|
||||
int pc = currentpc(func);
|
||||
Instruction i;
|
||||
if (pc == -1) return NULL; /* function is not activated */
|
||||
i = p->code[pc];
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_CALL: case OP_TAILCALL:
|
||||
return getobjname(L, (func+1)+GETARG_A(i), name);
|
||||
default:
|
||||
return NULL; /* no useful name found */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, StkId o, const char *op) {
|
||||
const char *name;
|
||||
const char *kind = getobjname(L, o, &name);
|
||||
const char *t = luaO_typename(o);
|
||||
if (kind)
|
||||
luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
|
||||
op, kind, name, t);
|
||||
else
|
||||
luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
|
||||
}
|
||||
|
||||
|
||||
void luaG_binerror (lua_State *L, StkId p1, int t, const char *op) {
|
||||
if (ttype(p1) == t) p1++;
|
||||
LUA_ASSERT(ttype(p1) != t, "must be an error");
|
||||
luaG_typeerror(L, p1, op);
|
||||
}
|
||||
|
||||
|
||||
void luaG_ordererror (lua_State *L, StkId top) {
|
||||
const char *t1 = luaO_typename(top-2);
|
||||
const char *t2 = luaO_typename(top-1);
|
||||
if (t1[2] == t2[2])
|
||||
luaO_verror(L, "attempt to compare two %.10s values", t1);
|
||||
else
|
||||
luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** $Id: ldebug.h,v 1.6 2000/10/02 20:10:55 roberto Exp roberto $
|
||||
** Auxiliary functions from Debug Interface module
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,12 +8,14 @@
|
||||
#define ldebug_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
void luaG_callerror (lua_State *L, TObject *func);
|
||||
void luaG_indexerror (lua_State *L, TObject *t);
|
||||
void luaG_typeerror (lua_State *L, StkId o, const char *op);
|
||||
void luaG_binerror (lua_State *L, StkId p1, int t, const char *op);
|
||||
int luaG_getline (int *lineinfo, int pc, int refline, int *refi);
|
||||
void luaG_ordererror (lua_State *L, StkId top);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 1.72 2000/03/30 20:55:50 roberto Exp roberto $
|
||||
** $Id: ldo.c,v 1.108 2000/10/20 16:39:03 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,9 +10,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lgc.h"
|
||||
@@ -21,56 +20,43 @@
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
#define EXTRA_STACK 32 /* space to handle stack overflow errors */
|
||||
|
||||
/*
|
||||
** typical numer of stack slots used by a (big) function
|
||||
** (this constant is used only for choosing error messages)
|
||||
*/
|
||||
#define SLOTS_PER_F 20
|
||||
/* space to handle stack overflow errors */
|
||||
#define EXTRA_STACK (2*LUA_MINSTACK)
|
||||
|
||||
|
||||
void luaD_init (lua_State *L, int stacksize) {
|
||||
L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
|
||||
L->nblocks += stacksize*sizeof(TObject);
|
||||
L->stack_last = L->stack+(stacksize-1);
|
||||
L->stacksize = stacksize;
|
||||
L->Cstack.base = L->Cstack.lua2C = L->top = L->stack;
|
||||
L->Cstack.num = 0;
|
||||
L->Cbase = L->top = L->stack;
|
||||
}
|
||||
|
||||
|
||||
void luaD_checkstack (lua_State *L, int n) {
|
||||
if (L->stack_last-L->top <= n) { /* stack overflow? */
|
||||
if (L->stack_last - L->top <= n) { /* stack overflow? */
|
||||
if (L->stack_last-L->stack > (L->stacksize-1)) {
|
||||
/* overflow while handling overflow: do what?? */
|
||||
L->top -= EXTRA_STACK;
|
||||
lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
|
||||
/* overflow while handling overflow */
|
||||
luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
|
||||
}
|
||||
else {
|
||||
lua_Debug dummy;
|
||||
L->stack_last += EXTRA_STACK; /* to be used by error message */
|
||||
if (lua_getstack(L, L->stacksize/SLOTS_PER_F, &dummy) == 0) {
|
||||
/* too few funcs on stack: doesn't look like a recursion loop */
|
||||
lua_error(L, "Lua2C - C2Lua overflow");
|
||||
}
|
||||
else
|
||||
lua_error(L, "stack overflow; possible recursion loop");
|
||||
lua_error(L, "stack overflow");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void restore_stack_limit (lua_State *L) {
|
||||
if (L->top-L->stack < L->stacksize-1)
|
||||
L->stack_last = L->stack+(L->stacksize-1);
|
||||
if (L->top - L->stack < L->stacksize - 1)
|
||||
L->stack_last = L->stack + (L->stacksize-1);
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +72,7 @@ void luaD_adjusttop (lua_State *L, StkId base, int extra) {
|
||||
else {
|
||||
luaD_checkstack(L, diff);
|
||||
while (diff--)
|
||||
ttype(L->top++) = TAG_NIL;
|
||||
ttype(L->top++) = LUA_TNIL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,27 +80,33 @@ void luaD_adjusttop (lua_State *L, StkId base, int extra) {
|
||||
/*
|
||||
** Open a hole inside the stack at `pos'
|
||||
*/
|
||||
void luaD_openstack (lua_State *L, StkId pos) {
|
||||
static void luaD_openstack (lua_State *L, StkId pos) {
|
||||
int i = L->top-pos;
|
||||
while (i--) pos[i+1] = pos[i];
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line) {
|
||||
static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
|
||||
StkId old_Cbase = L->Cbase;
|
||||
StkId old_top = L->Cbase = L->top;
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||
(*hook)(L, ar);
|
||||
LUA_ASSERT(L->allowhooks == 0, "invalid allow");
|
||||
L->allowhooks = 1;
|
||||
L->top = old_top;
|
||||
L->Cbase = old_Cbase;
|
||||
}
|
||||
|
||||
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) {
|
||||
if (L->allowhooks) {
|
||||
lua_Debug ar;
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
|
||||
L->Cstack.num = 0;
|
||||
ar._func = func;
|
||||
ar.event = "line";
|
||||
ar.currentline = line;
|
||||
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||
(*L->linehook)(L, &ar);
|
||||
L->allowhooks = 1;
|
||||
L->top = old_top;
|
||||
L->Cstack = oldCLS;
|
||||
dohook(L, &ar, linehook);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,49 +115,33 @@ static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
|
||||
const char *event) {
|
||||
if (L->allowhooks) {
|
||||
lua_Debug ar;
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
|
||||
L->Cstack.num = 0;
|
||||
ar._func = func;
|
||||
ar.event = event;
|
||||
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||
callhook(L, &ar);
|
||||
L->allowhooks = 1;
|
||||
L->top = old_top;
|
||||
L->Cstack = oldCLS;
|
||||
infovalue(func)->pc = NULL; /* function is not active */
|
||||
dohook(L, &ar, callhook);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
|
||||
int nup = cl->nelems; /* number of upvalues */
|
||||
int numarg = L->top-base;
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId firstResult;
|
||||
if (nup > 0) {
|
||||
int n = numarg;
|
||||
luaD_checkstack(L, nup);
|
||||
/* open space for upvalues as extra arguments */
|
||||
while (n--) *(base+nup+n) = *(base+n);
|
||||
L->top += nup;
|
||||
numarg += nup;
|
||||
/* copy upvalues into stack */
|
||||
while (nup--) *(base+nup) = cl->consts[nup];
|
||||
}
|
||||
L->Cstack.num = numarg;
|
||||
L->Cstack.lua2C = base;
|
||||
L->Cstack.base = L->top;
|
||||
(*cl->f.c)(L); /* do the actual call */
|
||||
firstResult = L->Cstack.base;
|
||||
L->Cstack = oldCLS;
|
||||
return firstResult;
|
||||
int nup = cl->nupvalues; /* number of upvalues */
|
||||
StkId old_Cbase = L->Cbase;
|
||||
int n;
|
||||
L->Cbase = base; /* new base for C function */
|
||||
luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
|
||||
for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
|
||||
*(L->top++) = cl->upvalue[n];
|
||||
n = (*cl->f.c)(L); /* do the actual call */
|
||||
L->Cbase = old_Cbase; /* restore old C base */
|
||||
return L->top - n; /* return index of first result */
|
||||
}
|
||||
|
||||
|
||||
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
|
||||
void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults) {
|
||||
StkId base = L->top - nParams;
|
||||
luaD_openstack(L, base);
|
||||
*base = *f;
|
||||
clvalue(base) = f;
|
||||
ttype(base) = LUA_TFUNCTION;
|
||||
luaD_call(L, base, nResults);
|
||||
}
|
||||
|
||||
@@ -175,55 +151,186 @@ void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
|
||||
** The arguments are on the stack, right after the function.
|
||||
** When returns, the results are on the stack, starting at the original
|
||||
** function position.
|
||||
** The number of results is nResults, unless nResults=MULT_RET.
|
||||
** The number of results is nResults, unless nResults=LUA_MULTRET.
|
||||
*/
|
||||
void luaD_call (lua_State *L, StkId func, int nResults) {
|
||||
lua_Hook callhook;
|
||||
StkId firstResult;
|
||||
lua_Hook callhook = L->callhook;
|
||||
retry: /* for `function' tag method */
|
||||
switch (ttype(func)) {
|
||||
case TAG_LCLOSURE: {
|
||||
ttype(func) = TAG_LMARK;
|
||||
if (callhook)
|
||||
luaD_callHook(L, func, callhook, "call");
|
||||
firstResult = luaV_execute(L, clvalue(func), func+1);
|
||||
break;
|
||||
}
|
||||
case TAG_CCLOSURE: {
|
||||
ttype(func) = TAG_CMARK;
|
||||
if (callhook)
|
||||
luaD_callHook(L, func, callhook, "call");
|
||||
firstResult = callCclosure(L, clvalue(func), func+1);
|
||||
break;
|
||||
}
|
||||
default: { /* `func' is not a function; check the `function' tag method */
|
||||
const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
|
||||
if (ttype(im) == TAG_NIL)
|
||||
luaG_callerror(L, func);
|
||||
luaD_openstack(L, func);
|
||||
*func = *im; /* tag method is the new function to be called */
|
||||
goto retry; /* retry the call */
|
||||
}
|
||||
CallInfo ci;
|
||||
Closure *cl;
|
||||
if (ttype(func) != LUA_TFUNCTION) {
|
||||
/* `func' is not a function; check the `function' tag method */
|
||||
Closure *tm = luaT_gettmbyObj(L, func, TM_FUNCTION);
|
||||
if (tm == NULL)
|
||||
luaG_typeerror(L, func, "call");
|
||||
luaD_openstack(L, func);
|
||||
clvalue(func) = tm; /* tag method is the new function to be called */
|
||||
ttype(func) = LUA_TFUNCTION;
|
||||
}
|
||||
cl = clvalue(func);
|
||||
ci.func = cl;
|
||||
infovalue(func) = &ci;
|
||||
ttype(func) = LUA_TMARK;
|
||||
callhook = L->callhook;
|
||||
if (callhook)
|
||||
luaD_callHook(L, func, callhook, "call");
|
||||
firstResult = (cl->isC ? callCclosure(L, cl, func+1) :
|
||||
luaV_execute(L, cl, func+1));
|
||||
if (callhook) /* same hook that was active at entry */
|
||||
luaD_callHook(L, func, callhook, "return");
|
||||
/* adjust the number of results */
|
||||
if (nResults == MULT_RET)
|
||||
nResults = L->top - firstResult;
|
||||
else
|
||||
luaD_adjusttop(L, firstResult, nResults);
|
||||
LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag");
|
||||
/* move results to `func' (to erase parameters and function) */
|
||||
while (nResults) {
|
||||
*func++ = *(L->top - nResults);
|
||||
nResults--;
|
||||
if (nResults == LUA_MULTRET) {
|
||||
while (firstResult < L->top) /* copy all results */
|
||||
*func++ = *firstResult++;
|
||||
L->top = func;
|
||||
}
|
||||
L->top = func;
|
||||
else { /* copy at most `nResults' */
|
||||
for (; nResults > 0 && firstResult < L->top; nResults--)
|
||||
*func++ = *firstResult++;
|
||||
L->top = func;
|
||||
for (; nResults > 0; nResults--) { /* if there are not enough results */
|
||||
ttype(L->top) = LUA_TNIL; /* adjust the stack */
|
||||
incr_top; /* must check stack space */
|
||||
}
|
||||
}
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected call.
|
||||
*/
|
||||
struct CallS { /* data to `f_call' */
|
||||
StkId func;
|
||||
int nresults;
|
||||
};
|
||||
|
||||
static void f_call (lua_State *L, void *ud) {
|
||||
struct CallS *c = (struct CallS *)ud;
|
||||
luaD_call(L, c->func, c->nresults);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
|
||||
StkId func = L->top - (nargs+1); /* function to be called */
|
||||
struct CallS c;
|
||||
int status;
|
||||
c.func = func; c.nresults = nresults;
|
||||
status = luaD_runprotected(L, f_call, &c);
|
||||
if (status != 0) /* an error occurred? */
|
||||
L->top = func; /* remove parameters from the stack */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected parser.
|
||||
*/
|
||||
struct ParserS { /* data to `f_parser' */
|
||||
ZIO *z;
|
||||
int bin;
|
||||
};
|
||||
|
||||
static void f_parser (lua_State *L, void *ud) {
|
||||
struct ParserS *p = (struct ParserS *)ud;
|
||||
Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
|
||||
luaV_Lclosure(L, tf, 0);
|
||||
}
|
||||
|
||||
|
||||
static int protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||
struct ParserS p;
|
||||
unsigned long old_blocks;
|
||||
int status;
|
||||
p.z = z; p.bin = bin;
|
||||
luaC_checkGC(L);
|
||||
old_blocks = L->nblocks;
|
||||
status = luaD_runprotected(L, f_parser, &p);
|
||||
if (status == 0) {
|
||||
/* add new memory to threshold (as it probably will stay) */
|
||||
L->GCthreshold += (L->nblocks - old_blocks);
|
||||
}
|
||||
else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
|
||||
status = LUA_ERRSYNTAX;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int parse_file (lua_State *L, const char *filename) {
|
||||
ZIO z;
|
||||
int status;
|
||||
int bin; /* flag for file mode */
|
||||
int c; /* look ahead char */
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
bin = (c == ID_CHUNK);
|
||||
if (bin && f != stdin) {
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
|
||||
}
|
||||
lua_pushstring(L, "@");
|
||||
lua_pushstring(L, (filename == NULL) ? "(stdin)" : filename);
|
||||
lua_concat(L, 2);
|
||||
filename = lua_tostring(L, -1); /* filename = '@'..filename */
|
||||
lua_pop(L, 1); /* OK: there is no GC during parser */
|
||||
luaZ_Fopen(&z, f, filename);
|
||||
status = protectedparser(L, &z, bin);
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_dofile (lua_State *L, const char *filename) {
|
||||
int status = parse_file(L, filename);
|
||||
if (status == 0) /* parse OK? */
|
||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int parse_buffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name) {
|
||||
ZIO z;
|
||||
if (!name) name = "?";
|
||||
luaZ_mopen(&z, buff, size, name);
|
||||
return protectedparser(L, &z, buff[0]==ID_CHUNK);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name) {
|
||||
int status = parse_buffer(L, buff, size, name);
|
||||
if (status == 0) /* parse OK? */
|
||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_dostring (lua_State *L, const char *str) {
|
||||
return lua_dobuffer(L, str, strlen(str), str);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Error-recover functions (based on long jumps)
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/* chain list of long jump buffers */
|
||||
struct lua_longjmp {
|
||||
jmp_buf b;
|
||||
struct lua_longjmp *previous;
|
||||
volatile int status; /* error code */
|
||||
};
|
||||
|
||||
|
||||
static void message (lua_State *L, const char *s) {
|
||||
const TObject *em = &(luaS_assertglobalbyname(L, "_ERRORMESSAGE")->value);
|
||||
if (*luaO_typename(em) == 'f') {
|
||||
const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
|
||||
if (ttype(em) == LUA_TFUNCTION) {
|
||||
*L->top = *em;
|
||||
incr_top;
|
||||
lua_pushstring(L, s);
|
||||
@@ -231,136 +338,48 @@ static void message (lua_State *L, const char *s) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Reports an error, and jumps up to the available recovery label
|
||||
*/
|
||||
void lua_error (lua_State *L, const char *s) {
|
||||
LUA_API void lua_error (lua_State *L, const char *s) {
|
||||
if (s) message(L, s);
|
||||
if (L->errorJmp)
|
||||
luaD_breakrun(L, LUA_ERRRUN);
|
||||
}
|
||||
|
||||
|
||||
void luaD_breakrun (lua_State *L, int errcode) {
|
||||
if (L->errorJmp) {
|
||||
L->errorJmp->status = errcode;
|
||||
longjmp(L->errorJmp->b, 1);
|
||||
}
|
||||
else {
|
||||
message(L, "unable to recover; exiting\n");
|
||||
exit(1);
|
||||
if (errcode != LUA_ERRMEM)
|
||||
message(L, "unable to recover; exiting\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected call. Assumes that function is at Cstack.base and
|
||||
** parameters are on top of it.
|
||||
*/
|
||||
int luaD_protectedrun (lua_State *L) {
|
||||
struct lua_longjmp myErrorJmp;
|
||||
StkId base = L->Cstack.base;
|
||||
int numCblocks = L->numCblocks;
|
||||
int status;
|
||||
struct lua_longjmp *volatile oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp.b) == 0) {
|
||||
luaD_call(L, base, MULT_RET);
|
||||
L->Cstack.lua2C = base; /* position of the new results */
|
||||
L->Cstack.num = L->top - base;
|
||||
L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
|
||||
status = 0;
|
||||
}
|
||||
else { /* an error occurred: restore the stack */
|
||||
L->Cstack.num = 0; /* no results */
|
||||
L->top = L->Cstack.base = L->Cstack.lua2C = base;
|
||||
L->numCblocks = numCblocks;
|
||||
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
|
||||
StkId oldCbase = L->Cbase;
|
||||
StkId oldtop = L->top;
|
||||
struct lua_longjmp lj;
|
||||
int allowhooks = L->allowhooks;
|
||||
lj.status = 0;
|
||||
lj.previous = L->errorJmp; /* chain new error handler */
|
||||
L->errorJmp = &lj;
|
||||
if (setjmp(lj.b) == 0)
|
||||
(*f)(L, ud);
|
||||
else { /* an error occurred: restore the state */
|
||||
L->allowhooks = allowhooks;
|
||||
L->Cbase = oldCbase;
|
||||
L->top = oldtop;
|
||||
restore_stack_limit(L);
|
||||
status = 1;
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
return status;
|
||||
L->errorJmp = lj.previous; /* restore old error handler */
|
||||
return lj.status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
|
||||
*/
|
||||
static int protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||
struct lua_longjmp myErrorJmp;
|
||||
StkId base = L->Cstack.base;
|
||||
int numCblocks = L->numCblocks;
|
||||
int status;
|
||||
Proto *volatile tf;
|
||||
struct lua_longjmp *volatile oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
L->top = base; /* clear C2Lua */
|
||||
if (setjmp(myErrorJmp.b) == 0) {
|
||||
tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
|
||||
status = 0;
|
||||
}
|
||||
else { /* an error occurred: restore Cstack and top */
|
||||
L->Cstack.num = 0; /* no results */
|
||||
L->top = L->Cstack.base = L->Cstack.lua2C = base;
|
||||
L->numCblocks = numCblocks;
|
||||
tf = NULL;
|
||||
status = 1;
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
if (status) return 1; /* error code */
|
||||
if (tf == NULL) return 2; /* `natural' end */
|
||||
luaV_Lclosure(L, tf, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int do_main (lua_State *L, ZIO *z, int bin) {
|
||||
int status;
|
||||
int debug = L->debug; /* save debug status */
|
||||
do {
|
||||
long old_blocks = (luaC_checkGC(L), L->nblocks);
|
||||
status = protectedparser(L, z, bin);
|
||||
if (status == 1) return 1; /* error */
|
||||
else if (status == 2) return 0; /* `natural' end */
|
||||
else {
|
||||
unsigned long newelems2 = 2*(L->nblocks-old_blocks);
|
||||
L->GCthreshold += newelems2;
|
||||
status = luaD_protectedrun(L);
|
||||
L->GCthreshold -= newelems2;
|
||||
}
|
||||
} while (bin && status == 0);
|
||||
L->debug = debug; /* restore debug status */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
#define MAXFILENAME 260 /* maximum part of a file name kept */
|
||||
|
||||
int lua_dofile (lua_State *L, const char *filename) {
|
||||
ZIO z;
|
||||
int status;
|
||||
int bin; /* flag for file mode */
|
||||
int c; /* look ahead char */
|
||||
char source[MAXFILENAME];
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL) return 2; /* unable to open file */
|
||||
luaL_filesource(source, filename, sizeof(source));
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
bin = (c == ID_CHUNK);
|
||||
if (bin && f != stdin) {
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
if (f == NULL) return 2; /* unable to reopen file */
|
||||
}
|
||||
luaZ_Fopen(&z, f, source);
|
||||
status = do_main(L, &z, bin);
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int lua_dostring (lua_State *L, const char *str) {
|
||||
return lua_dobuffer(L, str, strlen(str), str);
|
||||
}
|
||||
|
||||
|
||||
int lua_dobuffer (lua_State *L, const char *buff, int size, const char *name) {
|
||||
ZIO z;
|
||||
if (!name) name = "?";
|
||||
luaZ_mopen(&z, buff, size, name);
|
||||
return do_main(L, &z, buff[0]==ID_CHUNK);
|
||||
}
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.h,v 1.19 2000/03/29 20:19:20 roberto Exp roberto $
|
||||
** $Id: ldo.h,v 1.27 2000/10/05 13:00:17 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -21,12 +21,13 @@
|
||||
|
||||
void luaD_init (lua_State *L, int stacksize);
|
||||
void luaD_adjusttop (lua_State *L, StkId base, int extra);
|
||||
void luaD_openstack (lua_State *L, StkId pos);
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line);
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook);
|
||||
void luaD_call (lua_State *L, StkId func, int nResults);
|
||||
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults);
|
||||
int luaD_protectedrun (lua_State *L);
|
||||
void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults);
|
||||
void luaD_checkstack (lua_State *L, int n);
|
||||
|
||||
void luaD_breakrun (lua_State *L, int errcode);
|
||||
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 1.20 2000/03/10 18:37:44 roberto Exp roberto $
|
||||
** $Id: lfunc.c,v 1.33 2000/10/18 17:19:09 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,62 +7,86 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
#define gcsizeproto(L, p) numblocks(L, 0, sizeof(Proto))
|
||||
#define gcsizeclosure(L, c) numblocks(L, c->nelems, sizeof(Closure))
|
||||
|
||||
#define sizeclosure(n) ((int)sizeof(Closure) + (int)sizeof(TObject)*((n)-1))
|
||||
|
||||
|
||||
Closure *luaF_newclosure (lua_State *L, int nelems) {
|
||||
Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
|
||||
sizeof(TObject)*(nelems-1));
|
||||
int size = sizeclosure(nelems);
|
||||
Closure *c = (Closure *)luaM_malloc(L, size);
|
||||
c->next = L->rootcl;
|
||||
L->rootcl = c;
|
||||
c->marked = 0;
|
||||
c->nelems = nelems;
|
||||
L->nblocks += gcsizeclosure(L, c);
|
||||
c->mark = c;
|
||||
c->nupvalues = nelems;
|
||||
L->nblocks += size;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
Proto *luaF_newproto (lua_State *L) {
|
||||
Proto *f = luaM_new(L, Proto);
|
||||
f->code = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->source = NULL;
|
||||
f->kstr = NULL;
|
||||
f->nkstr = 0;
|
||||
f->knum = NULL;
|
||||
f->nknum = 0;
|
||||
f->kstr = NULL;
|
||||
f->nkstr = 0;
|
||||
f->kproto = NULL;
|
||||
f->nkproto = 0;
|
||||
f->locvars = NULL;
|
||||
f->next = L->rootproto;
|
||||
L->rootproto = f;
|
||||
f->code = NULL;
|
||||
f->ncode = 0;
|
||||
f->numparams = 0;
|
||||
f->is_vararg = 0;
|
||||
f->maxstacksize = 0;
|
||||
f->marked = 0;
|
||||
L->nblocks += gcsizeproto(L, f);
|
||||
f->lineinfo = NULL;
|
||||
f->nlineinfo = 0;
|
||||
f->nlocvars = 0;
|
||||
f->locvars = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->source = NULL;
|
||||
f->next = L->rootproto; /* chain in list of protos */
|
||||
L->rootproto = f;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
static size_t protosize (Proto *f) {
|
||||
return sizeof(Proto)
|
||||
+ f->nknum*sizeof(Number)
|
||||
+ f->nkstr*sizeof(TString *)
|
||||
+ f->nkproto*sizeof(Proto *)
|
||||
+ f->ncode*sizeof(Instruction)
|
||||
+ f->nlocvars*sizeof(struct LocVar)
|
||||
+ f->nlineinfo*sizeof(int);
|
||||
}
|
||||
|
||||
|
||||
void luaF_protook (lua_State *L, Proto *f, int pc) {
|
||||
f->ncode = pc; /* signal that proto was properly created */
|
||||
L->nblocks += protosize(f);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||
L->nblocks -= gcsizeproto(L, f);
|
||||
if (f->ncode > 0) /* function was properly created? */
|
||||
L->nblocks -= protosize(f);
|
||||
luaM_free(L, f->code);
|
||||
luaM_free(L, f->locvars);
|
||||
luaM_free(L, f->kstr);
|
||||
luaM_free(L, f->knum);
|
||||
luaM_free(L, f->kproto);
|
||||
luaM_free(L, f->lineinfo);
|
||||
luaM_free(L, f);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
L->nblocks -= gcsizeclosure(L, c);
|
||||
L->nblocks -= sizeclosure(c->nupvalues);
|
||||
luaM_free(L, c);
|
||||
}
|
||||
|
||||
@@ -71,22 +95,15 @@ void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
** Look for n-th local variable at line `line' in function `func'.
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
const char *luaF_getlocalname (const Proto *func,
|
||||
int local_number, int line) {
|
||||
int count = 0;
|
||||
const char *varname = NULL;
|
||||
LocVar *lv = func->locvars;
|
||||
if (lv == NULL)
|
||||
return NULL;
|
||||
for (; lv->line != -1 && lv->line < line; lv++) {
|
||||
if (lv->varname) { /* register */
|
||||
if (++count == local_number)
|
||||
varname = lv->varname->str;
|
||||
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
||||
int i;
|
||||
for (i = 0; i<f->nlocvars && f->locvars[i].startpc <= pc; i++) {
|
||||
if (pc < f->locvars[i].endpc) { /* is variable active? */
|
||||
local_number--;
|
||||
if (local_number == 0)
|
||||
return f->locvars[i].varname->str;
|
||||
}
|
||||
else /* unregister */
|
||||
if (--count < local_number)
|
||||
varname = NULL;
|
||||
}
|
||||
return varname;
|
||||
return NULL; /* not found */
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lfunc.h,v 1.10 1999/12/27 17:33:22 roberto Exp roberto $
|
||||
** $Id: lfunc.h,v 1.12 2000/06/26 19:28:31 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,12 +13,12 @@
|
||||
|
||||
|
||||
Proto *luaF_newproto (lua_State *L);
|
||||
void luaF_protook (lua_State *L, Proto *f, int pc);
|
||||
Closure *luaF_newclosure (lua_State *L, int nelems);
|
||||
void luaF_freeproto (lua_State *L, Proto *f);
|
||||
void luaF_freeclosure (lua_State *L, Closure *c);
|
||||
|
||||
const char *luaF_getlocalname (const Proto *func,
|
||||
int local_number, int line);
|
||||
const char *luaF_getlocalname (const Proto *func, int local_number, int pc);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,130 +1,185 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 1.46 2000/03/30 20:55:50 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 1.71 2000/10/05 13:00:17 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lref.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
typedef struct GCState {
|
||||
Hash *tmark; /* list of marked tables to be visited */
|
||||
Closure *cmark; /* list of marked closures to be visited */
|
||||
} GCState;
|
||||
|
||||
|
||||
|
||||
static void luaD_gcTM (lua_State *L, const TObject *o) {
|
||||
const TObject *im = luaT_getimbyObj(L, o, IM_GC);
|
||||
if (ttype(im) != TAG_NIL) {
|
||||
luaD_checkstack(L, 2);
|
||||
*(L->top++) = *im;
|
||||
*(L->top++) = *o;
|
||||
luaD_call(L, L->top-2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int markobject (lua_State *L, TObject *o);
|
||||
static void markobject (GCState *st, TObject *o);
|
||||
|
||||
|
||||
/* mark a string; marks larger than 1 cannot be changed */
|
||||
#define strmark(L, s) {if ((s)->marked == 0) (s)->marked = 1;}
|
||||
#define strmark(s) {if ((s)->marked == 0) (s)->marked = 1;}
|
||||
|
||||
|
||||
|
||||
static void protomark (lua_State *L, Proto *f) {
|
||||
static void protomark (Proto *f) {
|
||||
if (!f->marked) {
|
||||
int i;
|
||||
f->marked = 1;
|
||||
strmark(L, f->source);
|
||||
for (i=f->nkstr-1; i>=0; i--)
|
||||
strmark(L, f->kstr[i]);
|
||||
for (i=f->nkproto-1; i>=0; i--)
|
||||
protomark(L, f->kproto[i]);
|
||||
strmark(f->source);
|
||||
for (i=0; i<f->nkstr; i++)
|
||||
strmark(f->kstr[i]);
|
||||
for (i=0; i<f->nkproto; i++)
|
||||
protomark(f->kproto[i]);
|
||||
for (i=0; i<f->nlocvars; i++) /* mark local-variable names */
|
||||
strmark(f->locvars[i].varname);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void closuremark (lua_State *L, Closure *f) {
|
||||
if (!f->marked) {
|
||||
int i = f->nelems;
|
||||
f->marked = 1;
|
||||
while (i--)
|
||||
markobject(L, &f->consts[i]);
|
||||
}
|
||||
static void markstack (lua_State *L, GCState *st) {
|
||||
StkId o;
|
||||
for (o=L->stack; o<L->top; o++)
|
||||
markobject(st, o);
|
||||
}
|
||||
|
||||
|
||||
static void hashmark (lua_State *L, Hash *h) {
|
||||
if (!h->marked) {
|
||||
int i;
|
||||
h->marked = 1;
|
||||
for (i=h->size-1; i>=0; i--) {
|
||||
Node *n = node(h,i);
|
||||
if (ttype(key(n)) != TAG_NIL) {
|
||||
markobject(L, &n->key);
|
||||
markobject(L, &n->val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void travglobal (lua_State *L) {
|
||||
GlobalVar *gv;
|
||||
for (gv=L->rootglobal; gv; gv=gv->next) {
|
||||
LUA_ASSERT(L, gv->name->u.s.gv == gv, "inconsistent global name");
|
||||
if (gv->value.ttype != TAG_NIL) {
|
||||
strmark(L, gv->name); /* cannot collect non nil global variables */
|
||||
markobject(L, &gv->value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void travstack (lua_State *L) {
|
||||
int i;
|
||||
for (i = (L->top-1)-L->stack; i>=0; i--)
|
||||
markobject(L, L->stack+i);
|
||||
}
|
||||
|
||||
|
||||
static void travlock (lua_State *L) {
|
||||
static void marklock (lua_State *L, GCState *st) {
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++) {
|
||||
if (L->refArray[i].st == LOCK)
|
||||
markobject(L, &L->refArray[i].o);
|
||||
markobject(st, &L->refArray[i].o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int markobject (lua_State *L, TObject *o) {
|
||||
static void markclosure (GCState *st, Closure *cl) {
|
||||
if (!ismarked(cl)) {
|
||||
if (!cl->isC)
|
||||
protomark(cl->f.l);
|
||||
cl->mark = st->cmark; /* chain it for later traversal */
|
||||
st->cmark = cl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void marktagmethods (lua_State *L, GCState *st) {
|
||||
int e;
|
||||
for (e=0; e<TM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++) {
|
||||
Closure *cl = luaT_gettm(L, t, e);
|
||||
if (cl) markclosure(st, cl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markobject (GCState *st, TObject *o) {
|
||||
switch (ttype(o)) {
|
||||
case TAG_USERDATA: case TAG_STRING:
|
||||
strmark(L, tsvalue(o));
|
||||
case LUA_TUSERDATA: case LUA_TSTRING:
|
||||
strmark(tsvalue(o));
|
||||
break;
|
||||
case TAG_TABLE:
|
||||
hashmark(L, avalue(o));
|
||||
case LUA_TMARK:
|
||||
markclosure(st, infovalue(o)->func);
|
||||
break;
|
||||
case TAG_LCLOSURE: case TAG_LMARK:
|
||||
protomark(L, clvalue(o)->f.l);
|
||||
/* go trhough */
|
||||
case TAG_CCLOSURE: case TAG_CMARK:
|
||||
closuremark(L, clvalue(o));
|
||||
case LUA_TFUNCTION:
|
||||
markclosure(st, clvalue(o));
|
||||
break;
|
||||
case LUA_TTABLE: {
|
||||
if (!ismarked(hvalue(o))) {
|
||||
hvalue(o)->mark = st->tmark; /* chain it in list of marked */
|
||||
st->tmark = hvalue(o);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: break; /* numbers, etc */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void markall (lua_State *L) {
|
||||
GCState st;
|
||||
st.cmark = NULL;
|
||||
st.tmark = L->gt; /* put table of globals in mark list */
|
||||
L->gt->mark = NULL;
|
||||
marktagmethods(L, &st); /* mark tag methods */
|
||||
markstack(L, &st); /* mark stack objects */
|
||||
marklock(L, &st); /* mark locked objects */
|
||||
for (;;) { /* mark tables and closures */
|
||||
if (st.cmark) {
|
||||
int i;
|
||||
Closure *f = st.cmark; /* get first closure from list */
|
||||
st.cmark = f->mark; /* remove it from list */
|
||||
for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
|
||||
markobject(&st, &f->upvalue[i]);
|
||||
}
|
||||
else if (st.tmark) {
|
||||
int i;
|
||||
Hash *h = st.tmark; /* get first table from list */
|
||||
st.tmark = h->mark; /* remove it from list */
|
||||
for (i=0; i<h->size; i++) {
|
||||
Node *n = node(h, i);
|
||||
if (ttype(key(n)) != LUA_TNIL) {
|
||||
if (ttype(val(n)) == LUA_TNIL)
|
||||
luaH_remove(h, key(n)); /* dead element; try to remove it */
|
||||
markobject(&st, &n->key);
|
||||
markobject(&st, &n->val);
|
||||
}
|
||||
}
|
||||
}
|
||||
else break; /* nothing else to mark */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int hasmark (const TObject *o) {
|
||||
/* valid only for locked objects */
|
||||
switch (o->ttype) {
|
||||
case LUA_TSTRING: case LUA_TUSERDATA:
|
||||
return tsvalue(o)->marked;
|
||||
case LUA_TTABLE:
|
||||
return ismarked(hvalue(o));
|
||||
case LUA_TFUNCTION:
|
||||
return ismarked(clvalue(o));
|
||||
default: /* number */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* macro for internal debugging; check if a link of free refs is valid */
|
||||
#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
|
||||
|
||||
static void invalidaterefs (lua_State *L) {
|
||||
int n = L->refSize;
|
||||
int i;
|
||||
for (i=0; i<n; i++) {
|
||||
struct Ref *r = &L->refArray[i];
|
||||
if (r->st == HOLD && !hasmark(&r->o))
|
||||
r->st = COLLECTED;
|
||||
LUA_ASSERT((r->st == LOCK && hasmark(&r->o)) ||
|
||||
(r->st == HOLD && hasmark(&r->o)) ||
|
||||
r->st == COLLECTED ||
|
||||
r->st == NONEXT ||
|
||||
(r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
|
||||
"inconsistent ref table");
|
||||
}
|
||||
LUA_ASSERT(VALIDLINK(L, L->refFree, n), "inconsistent ref table");
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void collectproto (lua_State *L) {
|
||||
Proto **p = &L->rootproto;
|
||||
Proto *next;
|
||||
@@ -145,8 +200,8 @@ static void collectclosure (lua_State *L) {
|
||||
Closure **p = &L->rootcl;
|
||||
Closure *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->marked) {
|
||||
next->marked = 0;
|
||||
if (ismarked(next)) {
|
||||
next->mark = next; /* unmark */
|
||||
p = &next->next;
|
||||
}
|
||||
else {
|
||||
@@ -161,8 +216,8 @@ static void collecttable (lua_State *L) {
|
||||
Hash **p = &L->roottable;
|
||||
Hash *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->marked) {
|
||||
next->marked = 0;
|
||||
if (ismarked(next)) {
|
||||
next->mark = next; /* unmark */
|
||||
p = &next->next;
|
||||
}
|
||||
else {
|
||||
@@ -173,97 +228,126 @@ static void collecttable (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** remove from the global list globals whose names will be collected
|
||||
** (the global itself is freed when its name is freed)
|
||||
*/
|
||||
static void clear_global_list (lua_State *L, int limit) {
|
||||
GlobalVar **p = &L->rootglobal;
|
||||
GlobalVar *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->name->marked >= limit) p = &next->next;
|
||||
else *p = next->next;
|
||||
}
|
||||
static void checktab (lua_State *L, stringtable *tb) {
|
||||
if (tb->nuse < (lint32)(tb->size/4) && tb->size > 10)
|
||||
luaS_resize(L, tb, tb->size/2); /* table is too big */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** collect all elements with `marked' < `limit'.
|
||||
** with limit=1, that means all unmarked elements;
|
||||
** with limit=MAX_INT, that means all elements.
|
||||
*/
|
||||
static void collectstring (lua_State *L, int limit) {
|
||||
TObject o; /* to call userdata `gc' tag method */
|
||||
static void collectstrings (lua_State *L, int all) {
|
||||
int i;
|
||||
ttype(&o) = TAG_USERDATA;
|
||||
clear_global_list(L, limit);
|
||||
for (i=0; i<NUM_HASHS; i++) { /* for each hash table */
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) { /* for each list */
|
||||
TString **p = &tb->hash[j];
|
||||
TString *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->marked >= limit) {
|
||||
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
||||
next->marked = 0;
|
||||
p = &next->nexthash;
|
||||
}
|
||||
else { /* collect */
|
||||
if (next->constindex == -1) { /* is userdata? */
|
||||
tsvalue(&o) = next;
|
||||
luaD_gcTM(L, &o);
|
||||
}
|
||||
*p = next->nexthash;
|
||||
luaS_free(L, next);
|
||||
tb->nuse--;
|
||||
}
|
||||
for (i=0; i<L->strt.size; i++) { /* for each list */
|
||||
TString **p = &L->strt.hash[i];
|
||||
TString *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->marked && !all) { /* preserve? */
|
||||
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
||||
next->marked = 0;
|
||||
p = &next->nexthash;
|
||||
}
|
||||
else { /* collect */
|
||||
*p = next->nexthash;
|
||||
L->strt.nuse--;
|
||||
L->nblocks -= sizestring(next->len);
|
||||
luaM_free(L, next);
|
||||
}
|
||||
}
|
||||
if ((tb->nuse+1)*6 < tb->size)
|
||||
luaS_resize(L, tb, tb->size/2); /* table is too big */
|
||||
}
|
||||
checktab(L, &L->strt);
|
||||
}
|
||||
|
||||
|
||||
static void collectudata (lua_State *L, int all) {
|
||||
int i;
|
||||
for (i=0; i<L->udt.size; i++) { /* for each list */
|
||||
TString **p = &L->udt.hash[i];
|
||||
TString *next;
|
||||
while ((next = *p) != NULL) {
|
||||
LUA_ASSERT(next->marked <= 1, "udata cannot be fixed");
|
||||
if (next->marked && !all) { /* preserve? */
|
||||
next->marked = 0;
|
||||
p = &next->nexthash;
|
||||
}
|
||||
else { /* collect */
|
||||
int tag = next->u.d.tag;
|
||||
*p = next->nexthash;
|
||||
next->nexthash = L->TMtable[tag].collected; /* chain udata */
|
||||
L->TMtable[tag].collected = next;
|
||||
L->nblocks -= sizestring(next->len);
|
||||
L->udt.nuse--;
|
||||
}
|
||||
}
|
||||
}
|
||||
checktab(L, &L->udt);
|
||||
}
|
||||
|
||||
|
||||
#define MINBUFFER 256
|
||||
static void checkMbuffer (lua_State *L) {
|
||||
if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
|
||||
size_t newsize = L->Mbuffsize/2; /* still larger than MINBUFFER */
|
||||
L->nblocks += (newsize - L->Mbuffsize)*sizeof(char);
|
||||
L->Mbuffsize = newsize;
|
||||
luaM_reallocvector(L, L->Mbuffer, newsize, char);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markall (lua_State *L) {
|
||||
travstack(L); /* mark stack objects */
|
||||
travglobal(L); /* mark global variable values and names */
|
||||
travlock(L); /* mark locked objects */
|
||||
luaT_travtagmethods(L, markobject); /* mark tag methods */
|
||||
static void callgcTM (lua_State *L, const TObject *o) {
|
||||
Closure *tm = luaT_gettmbyObj(L, o, TM_GC);
|
||||
if (tm != NULL) {
|
||||
int oldah = L->allowhooks;
|
||||
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
|
||||
luaD_checkstack(L, 2);
|
||||
clvalue(L->top) = tm;
|
||||
ttype(L->top) = LUA_TFUNCTION;
|
||||
*(L->top+1) = *o;
|
||||
L->top += 2;
|
||||
luaD_call(L, L->top-2, 0);
|
||||
L->allowhooks = oldah; /* restore hooks */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void callgcTMudata (lua_State *L) {
|
||||
int tag;
|
||||
TObject o;
|
||||
ttype(&o) = LUA_TUSERDATA;
|
||||
L->GCthreshold = 2*L->nblocks; /* avoid GC during tag methods */
|
||||
for (tag=L->last_tag; tag>=0; tag--) { /* for each tag (in reverse order) */
|
||||
TString *udata;
|
||||
while ((udata = L->TMtable[tag].collected) != NULL) {
|
||||
L->TMtable[tag].collected = udata->nexthash; /* remove it from list */
|
||||
tsvalue(&o) = udata;
|
||||
callgcTM(L, &o);
|
||||
luaM_free(L, udata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_collect (lua_State *L, int all) {
|
||||
int oldah = L->allowhooks;
|
||||
L->allowhooks = 0; /* stop debug hooks during GC */
|
||||
L->GCthreshold *= 4; /* to avoid GC during GC */
|
||||
collectudata(L, all);
|
||||
callgcTMudata(L);
|
||||
collectstrings(L, all);
|
||||
collecttable(L);
|
||||
collectstring(L, all?MAX_INT:1);
|
||||
collectproto(L);
|
||||
collectclosure(L);
|
||||
L->allowhooks = oldah; /* restore hooks */
|
||||
}
|
||||
|
||||
|
||||
long lua_collectgarbage (lua_State *L, long limit) {
|
||||
unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
|
||||
static void luaC_collectgarbage (lua_State *L) {
|
||||
markall(L);
|
||||
luaR_invalidaterefs(L);
|
||||
invalidaterefs(L); /* check unlocked references */
|
||||
luaC_collect(L, 0);
|
||||
recovered = recovered - L->nblocks;
|
||||
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
||||
if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */
|
||||
L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */
|
||||
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
|
||||
}
|
||||
luaD_gcTM(L, &luaO_nilobject);
|
||||
return recovered;
|
||||
checkMbuffer(L);
|
||||
L->GCthreshold = 2*L->nblocks; /* set new threshold */
|
||||
callgcTM(L, &luaO_nilobject);
|
||||
}
|
||||
|
||||
|
||||
void luaC_checkGC (lua_State *L) {
|
||||
if (L->nblocks >= L->GCthreshold)
|
||||
lua_collectgarbage(L, 0);
|
||||
luaC_collectgarbage(L);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lgc.h,v 1.6 1999/10/04 17:51:04 roberto Exp roberto $
|
||||
** $Id: lgc.h,v 1.7 1999/11/22 13:12:07 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,8 +11,8 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaC_checkGC (lua_State *L);
|
||||
void luaC_collect (lua_State *L, int all);
|
||||
void luaC_checkGC (lua_State *L);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,21 +1,8 @@
|
||||
/*
|
||||
** $Id: linit.c,v 1.2 1999/11/22 13:12:07 roberto Exp roberto $
|
||||
** $Id: linit.c,v 1.5 2000/06/16 17:22:43 roberto Exp roberto $
|
||||
** Initialization of libraries for lua.c
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
void lua_userinit (lua_State *L) {
|
||||
lua_beginblock(L);
|
||||
lua_iolibopen(L);
|
||||
lua_strlibopen(L);
|
||||
lua_mathlibopen(L);
|
||||
lua_dblibopen(L);
|
||||
lua_endblock(L);
|
||||
}
|
||||
>>>>>>> This module is now obsolete, and is not part of Lua. <<<<<<<<<
|
||||
|
||||
|
||||
@@ -1,27 +1,28 @@
|
||||
/*
|
||||
** $Id: liolib.c,v 1.61 2000/03/30 17:19:48 roberto Exp roberto $
|
||||
** $Id: liolib.c,v 1.90 2000/10/27 16:15:53 roberto Exp roberto $
|
||||
** Standard I/O (and system) library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#ifndef OLD_ANSI
|
||||
#include <errno.h>
|
||||
#include <locale.h>
|
||||
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
||||
#define free(b) if (b) (free)(b)
|
||||
#else
|
||||
/* no support for locale and for strerror: fake them */
|
||||
#define setlocale(a,b) ((void)a, strcmp((b),"C")==0?"C":NULL)
|
||||
@@ -31,7 +32,8 @@
|
||||
#define LC_MONETARY 0
|
||||
#define LC_NUMERIC 0
|
||||
#define LC_TIME 0
|
||||
#define strerror(e) "(no error message provided by operating system)"
|
||||
#define strerror(e) "generic I/O error"
|
||||
#define errno (-1)
|
||||
#endif
|
||||
|
||||
|
||||
@@ -51,46 +53,26 @@ int pclose(); */
|
||||
#define OUTFILE 1
|
||||
|
||||
typedef struct IOCtrl {
|
||||
FILE *file[2]; /* values of _INPUT and _OUTPUT */
|
||||
int ref[2]; /* ref for strings _INPUT/_OUTPUT */
|
||||
int iotag; /* tag for file handles */
|
||||
int closedtag; /* tag for closed handles */
|
||||
} IOCtrl;
|
||||
|
||||
|
||||
|
||||
static const char *const filenames[] = {"_INPUT", "_OUTPUT", NULL};
|
||||
static const char *const filenames[] = {"_INPUT", "_OUTPUT"};
|
||||
|
||||
|
||||
static void atribTM (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
const char *varname = luaL_check_string(L, 2);
|
||||
lua_Object newvalue = lua_getparam(L, 4);
|
||||
int inout;
|
||||
if ((inout = luaL_findstring(varname, filenames)) != -1) {
|
||||
if (lua_tag(L, newvalue) != ctrl->iotag)
|
||||
luaL_verror(L, "%.20s value must be a valid file handle",
|
||||
filenames[inout]);
|
||||
ctrl->file[inout] = (FILE *)lua_getuserdata(L, newvalue);
|
||||
}
|
||||
/* set the actual variable */
|
||||
lua_pushobject(L, newvalue);
|
||||
lua_rawsetglobal(L, varname);
|
||||
}
|
||||
|
||||
|
||||
static void ctrl_collect (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
free(ctrl);
|
||||
}
|
||||
|
||||
|
||||
static void pushresult (lua_State *L, int i) {
|
||||
if (i)
|
||||
static int pushresult (lua_State *L, int i) {
|
||||
if (i) {
|
||||
lua_pushuserdata(L, NULL);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, strerror(errno));
|
||||
lua_pushnumber(L, errno);
|
||||
return 3;;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,11 +84,12 @@ static void pushresult (lua_State *L, int i) {
|
||||
*/
|
||||
|
||||
|
||||
static FILE *gethandle (lua_State *L, IOCtrl *ctrl, lua_Object f) {
|
||||
if (lua_isuserdata(L, f)) {
|
||||
static FILE *gethandle (lua_State *L, IOCtrl *ctrl, int f) {
|
||||
void *p = lua_touserdata(L, f);
|
||||
if (p != NULL) { /* is `f' a userdata ? */
|
||||
int ftag = lua_tag(L, f);
|
||||
if (ftag == ctrl->iotag)
|
||||
return (FILE *)lua_getuserdata(L, f);
|
||||
if (ftag == ctrl->iotag) /* does it have the correct tag? */
|
||||
return (FILE *)p;
|
||||
else if (ftag == ctrl->closedtag)
|
||||
lua_error(L, "cannot access a closed file");
|
||||
/* else go through */
|
||||
@@ -116,20 +99,22 @@ static FILE *gethandle (lua_State *L, IOCtrl *ctrl, lua_Object f) {
|
||||
|
||||
|
||||
static FILE *getnonullfile (lua_State *L, IOCtrl *ctrl, int arg) {
|
||||
FILE *f = gethandle(L, ctrl, lua_getparam(L, arg));
|
||||
FILE *f = gethandle(L, ctrl, arg);
|
||||
luaL_arg_check(L, f, arg, "invalid file handle");
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
static FILE *getfileparam (lua_State *L, IOCtrl *ctrl, int *arg, int inout) {
|
||||
FILE *f = gethandle(L, ctrl, lua_getparam(L, *arg));
|
||||
if (f) {
|
||||
(*arg)++;
|
||||
return f;
|
||||
}
|
||||
else
|
||||
return ctrl->file[inout];
|
||||
static FILE *getfilebyref (lua_State *L, IOCtrl *ctrl, int inout) {
|
||||
FILE *f;
|
||||
lua_getglobals(L);
|
||||
lua_getref(L, ctrl->ref[inout]);
|
||||
lua_rawget(L, -2);
|
||||
f = gethandle(L, ctrl, -1);
|
||||
if (f == NULL)
|
||||
luaL_verror(L, "global variable `%.10s' is not a file handle",
|
||||
filenames[inout]);
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
@@ -140,31 +125,24 @@ static void setfilebyname (lua_State *L, IOCtrl *ctrl, FILE *f,
|
||||
}
|
||||
|
||||
|
||||
static void setfile (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) {
|
||||
ctrl->file[inout] = f;
|
||||
lua_pushusertag(L, f, ctrl->iotag);
|
||||
lua_setglobal(L, filenames[inout]);
|
||||
}
|
||||
#define setfile(L,ctrl,f,inout) (setfilebyname(L,ctrl,f,filenames[inout]))
|
||||
|
||||
|
||||
static void setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) {
|
||||
static int setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) {
|
||||
if (f == NULL)
|
||||
pushresult(L, 0);
|
||||
return pushresult(L, 0);
|
||||
else {
|
||||
setfile(L, ctrl, f, inout);
|
||||
lua_pushusertag(L, f, ctrl->iotag);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) {
|
||||
if (f == stdin || f == stdout)
|
||||
if (f == stdin || f == stdout || f == stderr)
|
||||
return 1;
|
||||
else {
|
||||
if (f == ctrl->file[INFILE])
|
||||
setfile(L, ctrl, stdin, INFILE);
|
||||
else if (f == ctrl->file[OUTFILE])
|
||||
setfile(L, ctrl, stdout, OUTFILE);
|
||||
lua_pushusertag(L, f, ctrl->iotag);
|
||||
lua_settag(L, ctrl->closedtag);
|
||||
return (CLOSEFILE(L, f) == 0);
|
||||
@@ -172,53 +150,71 @@ static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) {
|
||||
}
|
||||
|
||||
|
||||
static void io_close (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
pushresult(L, closefile(L, ctrl, getnonullfile(L, ctrl, 2)));
|
||||
static int io_close (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||
lua_pop(L, 1); /* remove upvalue */
|
||||
return pushresult(L, closefile(L, ctrl, getnonullfile(L, ctrl, 1)));
|
||||
}
|
||||
|
||||
|
||||
static void io_open (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
FILE *f = fopen(luaL_check_string(L, 2), luaL_check_string(L, 3));
|
||||
if (f) lua_pushusertag(L, f, ctrl->iotag);
|
||||
else pushresult(L, 0);
|
||||
static int file_collect (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||
FILE *f = getnonullfile(L, ctrl, 1);
|
||||
if (f != stdin && f != stdout && f != stderr)
|
||||
CLOSEFILE(L, f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void io_fromto (lua_State *L, int inout, const char *mode) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
lua_Object f = lua_getparam(L, 2);
|
||||
FILE *current;
|
||||
if (f == LUA_NOOBJECT) {
|
||||
pushresult(L, closefile(L, ctrl, ctrl->file[inout]));
|
||||
return;
|
||||
static int io_open (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||
FILE *f;
|
||||
lua_pop(L, 1); /* remove upvalue */
|
||||
f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
|
||||
if (f) {
|
||||
lua_pushusertag(L, f, ctrl->iotag);
|
||||
return 1;
|
||||
}
|
||||
else if (lua_tag(L, f) == ctrl->iotag) /* deprecated option */
|
||||
current = (FILE *)lua_getuserdata(L, f);
|
||||
else
|
||||
return pushresult(L, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int io_fromto (lua_State *L, int inout, const char *mode) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||
FILE *current;
|
||||
lua_pop(L, 1); /* remove upvalue */
|
||||
if (lua_isnull(L, 1)) {
|
||||
closefile(L, ctrl, getfilebyref(L, ctrl, inout));
|
||||
current = (inout == 0) ? stdin : stdout;
|
||||
}
|
||||
else if (lua_tag(L, 1) == ctrl->iotag) /* deprecated option */
|
||||
current = (FILE *)lua_touserdata(L, 1);
|
||||
else {
|
||||
const char *s = luaL_check_string(L, 2);
|
||||
const char *s = luaL_check_string(L, 1);
|
||||
current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode);
|
||||
}
|
||||
setreturn(L, ctrl, current, inout);
|
||||
return setreturn(L, ctrl, current, inout);
|
||||
}
|
||||
|
||||
|
||||
static void io_readfrom (lua_State *L) {
|
||||
io_fromto(L, INFILE, "r");
|
||||
static int io_readfrom (lua_State *L) {
|
||||
return io_fromto(L, INFILE, "r");
|
||||
}
|
||||
|
||||
|
||||
static void io_writeto (lua_State *L) {
|
||||
io_fromto(L, OUTFILE, "w");
|
||||
static int io_writeto (lua_State *L) {
|
||||
return io_fromto(L, OUTFILE, "w");
|
||||
}
|
||||
|
||||
|
||||
static void io_appendto (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
FILE *current = fopen(luaL_check_string(L, 2), "a");
|
||||
setreturn(L, ctrl, current, OUTFILE);
|
||||
static int io_appendto (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||
FILE *current;
|
||||
lua_pop(L, 1); /* remove upvalue */
|
||||
current = fopen(luaL_check_string(L, 1), "a");
|
||||
return setreturn(L, ctrl, current, OUTFILE);
|
||||
}
|
||||
|
||||
|
||||
@@ -243,6 +239,8 @@ static void io_appendto (lua_State *L) {
|
||||
static int read_pattern (lua_State *L, FILE *f, const char *p) {
|
||||
int inskip = 0; /* {skip} level */
|
||||
int c = NEED_OTHER;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
while (*p != '\0') {
|
||||
switch (*p) {
|
||||
case '{':
|
||||
@@ -260,7 +258,7 @@ static int read_pattern (lua_State *L, FILE *f, const char *p) {
|
||||
if (c == NEED_OTHER) c = getc(f);
|
||||
m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
|
||||
if (m) {
|
||||
if (!inskip) luaL_addchar(L, c);
|
||||
if (!inskip) luaL_putchar(&b, c);
|
||||
c = NEED_OTHER;
|
||||
}
|
||||
switch (*ep) {
|
||||
@@ -271,7 +269,7 @@ static int read_pattern (lua_State *L, FILE *f, const char *p) {
|
||||
while (m) { /* reads the same item until it fails */
|
||||
c = getc(f);
|
||||
m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
|
||||
if (m && !inskip) luaL_addchar(L, c);
|
||||
if (m && !inskip) luaL_putchar(&b, c);
|
||||
}
|
||||
/* go through to continue reading the pattern */
|
||||
case '?': /* optional */
|
||||
@@ -285,6 +283,7 @@ static int read_pattern (lua_State *L, FILE *f, const char *p) {
|
||||
}
|
||||
} break_while:
|
||||
if (c != NEED_OTHER) ungetc(c, f);
|
||||
luaL_pushresult(&b); /* close buffer */
|
||||
return (*p == '\0');
|
||||
}
|
||||
|
||||
@@ -305,71 +304,108 @@ static int read_number (lua_State *L, FILE *f) {
|
||||
}
|
||||
|
||||
|
||||
static void read_word (lua_State *L, FILE *f) {
|
||||
static int read_word (lua_State *L, FILE *f) {
|
||||
int c;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
do { c = fgetc(f); } while (isspace(c)); /* skip spaces */
|
||||
while (c != EOF && !isspace(c)) {
|
||||
luaL_addchar(L, c);
|
||||
luaL_putchar(&b, c);
|
||||
c = fgetc(f);
|
||||
}
|
||||
ungetc(c, f);
|
||||
luaL_pushresult(&b); /* close buffer */
|
||||
return (lua_strlen(L, -1) > 0);
|
||||
}
|
||||
|
||||
|
||||
#define HUNK_LINE 256
|
||||
#define HUNK_FILE BUFSIZ
|
||||
|
||||
static int read_line (lua_State *L, FILE *f) {
|
||||
int n;
|
||||
char *b;
|
||||
do {
|
||||
b = luaL_openspace(L, HUNK_LINE);
|
||||
if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */
|
||||
n = strlen(b);
|
||||
luaL_addsize(L, n);
|
||||
} while (b[n-1] != '\n');
|
||||
luaL_addsize(L, -1); /* remove '\n' */
|
||||
return 1;
|
||||
int n = 0;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
for (;;) {
|
||||
char *p = luaL_prepbuffer(&b);
|
||||
if (!fgets(p, LUAL_BUFFERSIZE, f)) /* read fails? */
|
||||
break;
|
||||
n = strlen(p);
|
||||
if (p[n-1] != '\n')
|
||||
luaL_addsize(&b, n);
|
||||
else {
|
||||
luaL_addsize(&b, n-1); /* do not add the `\n' */
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaL_pushresult(&b); /* close buffer */
|
||||
return (n > 0); /* read something? */
|
||||
}
|
||||
|
||||
|
||||
static void read_file (lua_State *L, FILE *f) {
|
||||
size_t len = 0;
|
||||
size_t size = BUFSIZ;
|
||||
char *buffer = NULL;
|
||||
for (;;) {
|
||||
char *newbuffer = (char *)realloc(buffer, size);
|
||||
if (newbuffer == NULL) {
|
||||
free(buffer);
|
||||
lua_error(L, "not enough memory to read a file");
|
||||
}
|
||||
buffer = newbuffer;
|
||||
len += fread(buffer+len, sizeof(char), size-len, f);
|
||||
if (len < size) break; /* did not read all it could */
|
||||
size *= 2;
|
||||
}
|
||||
lua_pushlstring(L, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
|
||||
static int read_chars (lua_State *L, FILE *f, size_t n) {
|
||||
char *buffer;
|
||||
size_t n1;
|
||||
char statbuff[BUFSIZ];
|
||||
if (n <= BUFSIZ)
|
||||
buffer = statbuff;
|
||||
else {
|
||||
buffer = (char *)malloc(n);
|
||||
if (buffer == NULL)
|
||||
lua_error(L, "not enough memory to read a file");
|
||||
}
|
||||
n1 = fread(buffer, sizeof(char), n, f);
|
||||
lua_pushlstring(L, buffer, n1);
|
||||
if (buffer != statbuff) free(buffer);
|
||||
return (n1 > 0 || n == 0);
|
||||
}
|
||||
|
||||
|
||||
static int io_read (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||
int lastarg = lua_gettop(L) - 1;
|
||||
int firstarg = 1;
|
||||
FILE *f = gethandle(L, ctrl, firstarg);
|
||||
int n;
|
||||
do {
|
||||
char *b = luaL_openspace(L, HUNK_FILE);
|
||||
n = fread(b, sizeof(char), HUNK_FILE, f);
|
||||
luaL_addsize(L, n);
|
||||
} while (n==HUNK_FILE);
|
||||
}
|
||||
|
||||
|
||||
static int read_chars (lua_State *L, FILE *f, int n) {
|
||||
char *b = luaL_openspace(L, n);
|
||||
int n1 = fread(b, sizeof(char), n, f);
|
||||
luaL_addsize(L, n1);
|
||||
return (n == n1);
|
||||
}
|
||||
|
||||
|
||||
static void io_read (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
int arg = 2;
|
||||
FILE *f = getfileparam(L, ctrl, &arg, INFILE);
|
||||
lua_Object op = lua_getparam(L, arg);
|
||||
do { /* repeat for each part */
|
||||
long l;
|
||||
if (f) firstarg++;
|
||||
else f = getfilebyref(L, ctrl, INFILE); /* get _INPUT */
|
||||
lua_pop(L, 1);
|
||||
if (firstarg > lastarg) { /* no arguments? */
|
||||
lua_settop(L, 0); /* erase upvalue and other eventual garbage */
|
||||
firstarg = lastarg = 1; /* correct indices */
|
||||
lua_pushstring(L, "*l"); /* push default argument */
|
||||
}
|
||||
else /* ensure stack space for all results and for auxlib's buffer */
|
||||
luaL_checkstack(L, lastarg-firstarg+1+LUA_MINSTACK, "too many arguments");
|
||||
for (n = firstarg; n<=lastarg; n++) {
|
||||
int success;
|
||||
luaL_resetbuffer(L);
|
||||
if (lua_isnumber(L, op))
|
||||
success = read_chars(L, f, (int)lua_getnumber(L, op));
|
||||
if (lua_isnumber(L, n))
|
||||
success = read_chars(L, f, (size_t)lua_tonumber(L, n));
|
||||
else {
|
||||
const char *p = luaL_opt_string(L, arg, "*l");
|
||||
const char *p = luaL_check_string(L, n);
|
||||
if (p[0] != '*')
|
||||
success = read_pattern(L, f, p); /* deprecated! */
|
||||
else {
|
||||
switch (p[1]) {
|
||||
case 'n': /* number */
|
||||
if (!read_number(L, f)) return; /* read fails */
|
||||
if (!read_number(L, f)) goto endloop; /* read fails */
|
||||
continue; /* number is already pushed; avoid the "pushstring" */
|
||||
case 'l': /* line */
|
||||
success = read_line(L, f);
|
||||
@@ -379,68 +415,78 @@ static void io_read (lua_State *L) {
|
||||
success = 1; /* always success */
|
||||
break;
|
||||
case 'w': /* word */
|
||||
read_word(L, f);
|
||||
success = 0; /* must read something to succeed */
|
||||
success = read_word(L, f);
|
||||
break;
|
||||
default:
|
||||
luaL_argerror(L, arg, "invalid format");
|
||||
luaL_argerror(L, n, "invalid format");
|
||||
success = 0; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
}
|
||||
l = luaL_getsize(L);
|
||||
if (!success && l==0) return; /* read fails */
|
||||
lua_pushlstring(L, luaL_buffer(L), l);
|
||||
} while ((op = lua_getparam(L, ++arg)) != LUA_NOOBJECT);
|
||||
if (!success) {
|
||||
lua_pop(L, 1); /* remove last result */
|
||||
break; /* read fails */
|
||||
}
|
||||
} endloop:
|
||||
return n - firstarg;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static void io_write (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
int arg = 2;
|
||||
FILE *f = getfileparam(L, ctrl, &arg, OUTFILE);
|
||||
static int io_write (lua_State *L) {
|
||||
int lastarg = lua_gettop(L) - 1;
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||
int arg = 1;
|
||||
int status = 1;
|
||||
lua_Object o;
|
||||
while ((o = lua_getparam(L, arg)) != LUA_NOOBJECT) {
|
||||
if (lua_type(L, o)[2] == 'm') { /* nuMber? */ /* LUA_NUMBER */
|
||||
FILE *f = gethandle(L, ctrl, arg);
|
||||
if (f) arg++;
|
||||
else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */
|
||||
for (; arg <= lastarg; arg++) {
|
||||
if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */
|
||||
/* optimization: could be done exactly as for strings */
|
||||
status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0;
|
||||
status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0;
|
||||
}
|
||||
else {
|
||||
long l;
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, arg, &l);
|
||||
status = status && ((long)fwrite(s, sizeof(char), l, f) == l);
|
||||
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
||||
}
|
||||
arg++;
|
||||
}
|
||||
pushresult(L, status);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void io_seek (lua_State *L) {
|
||||
static int io_seek (lua_State *L) {
|
||||
static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
|
||||
static const char *const modenames[] = {"set", "cur", "end", NULL};
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
FILE *f = getnonullfile(L, ctrl, 2);
|
||||
int op = luaL_findstring(luaL_opt_string(L, 3, "cur"), modenames);
|
||||
long offset = luaL_opt_long(L, 4, 0);
|
||||
luaL_arg_check(L, op != -1, 3, "invalid mode");
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||
FILE *f;
|
||||
int op;
|
||||
long offset;
|
||||
lua_pop(L, 1); /* remove upvalue */
|
||||
f = getnonullfile(L, ctrl, 1);
|
||||
op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
|
||||
offset = luaL_opt_long(L, 3, 0);
|
||||
luaL_arg_check(L, op != -1, 2, "invalid mode");
|
||||
op = fseek(f, offset, mode[op]);
|
||||
if (op)
|
||||
pushresult(L, 0); /* error */
|
||||
else
|
||||
return pushresult(L, 0); /* error */
|
||||
else {
|
||||
lua_pushnumber(L, ftell(f));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void io_flush (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_getuserdata(L, lua_getparam(L, 1));
|
||||
lua_Object of = lua_getparam(L, 2);
|
||||
FILE *f = gethandle(L, ctrl, of);
|
||||
luaL_arg_check(L, f || of == LUA_NOOBJECT, 2, "invalid file handle");
|
||||
pushresult(L, fflush(f) == 0);
|
||||
static int io_flush (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||
FILE *f;
|
||||
lua_pop(L, 1); /* remove upvalue */
|
||||
f = gethandle(L, ctrl, 1);
|
||||
luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle");
|
||||
return pushresult(L, fflush(f) == 0);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
@@ -452,39 +498,43 @@ static void io_flush (lua_State *L) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void io_execute (lua_State *L) {
|
||||
static int io_execute (lua_State *L) {
|
||||
lua_pushnumber(L, system(luaL_check_string(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void io_remove (lua_State *L) {
|
||||
pushresult(L, remove(luaL_check_string(L, 1)) == 0);
|
||||
static int io_remove (lua_State *L) {
|
||||
return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static void io_rename (lua_State *L) {
|
||||
pushresult(L, rename(luaL_check_string(L, 1),
|
||||
static int io_rename (lua_State *L) {
|
||||
return pushresult(L, rename(luaL_check_string(L, 1),
|
||||
luaL_check_string(L, 2)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static void io_tmpname (lua_State *L) {
|
||||
static int io_tmpname (lua_State *L) {
|
||||
lua_pushstring(L, tmpnam(NULL));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void io_getenv (lua_State *L) {
|
||||
static int io_getenv (lua_State *L) {
|
||||
lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void io_clock (lua_State *L) {
|
||||
static int io_clock (lua_State *L) {
|
||||
lua_pushnumber(L, ((double)clock())/CLOCKS_PER_SEC);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void io_date (lua_State *L) {
|
||||
static int io_date (lua_State *L) {
|
||||
char b[256];
|
||||
const char *s = luaL_opt_string(L, 1, "%c");
|
||||
struct tm *stm;
|
||||
@@ -494,10 +544,11 @@ static void io_date (lua_State *L) {
|
||||
lua_pushstring(L, b);
|
||||
else
|
||||
lua_error(L, "invalid `date' format");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void setloc (lua_State *L) {
|
||||
static int setloc (lua_State *L) {
|
||||
static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
|
||||
LC_NUMERIC, LC_TIME};
|
||||
static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
|
||||
@@ -505,87 +556,107 @@ static void setloc (lua_State *L) {
|
||||
int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
|
||||
luaL_arg_check(L, op != -1, 2, "invalid option");
|
||||
lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void io_exit (lua_State *L) {
|
||||
static int io_exit (lua_State *L) {
|
||||
exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
static void io_debug (lua_State *L) {
|
||||
static int io_debug (lua_State *L) {
|
||||
for (;;) {
|
||||
char buffer[250];
|
||||
fprintf(stderr, "lua_debug> ");
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||
strcmp(buffer, "cont\n") == 0)
|
||||
return;
|
||||
return 0;
|
||||
lua_dostring(L, buffer);
|
||||
lua_settop(L, 0); /* remove eventual returns */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define LEVELS1 12 /* size of the first part of the stack */
|
||||
#define LEVELS2 10 /* size of the second part of the stack */
|
||||
|
||||
#define MESSAGESIZE 150
|
||||
#define MAXMESSAGE (MESSAGESIZE*10)
|
||||
|
||||
|
||||
static void errorfb (lua_State *L) {
|
||||
char buff[MAXMESSAGE];
|
||||
static int errorfb (lua_State *L) {
|
||||
int level = 1; /* skip level 0 (it's this function) */
|
||||
int firstpart = 1; /* still before eventual `...' */
|
||||
lua_Debug ar;
|
||||
lua_Object alertfunc = lua_rawgetglobal(L, "_ALERT");
|
||||
sprintf(buff, "error: %.200s\n", lua_getstring(L, lua_getparam(L, 1)));
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
luaL_addstring(&b, "error: ");
|
||||
luaL_addstring(&b, luaL_check_string(L, 1));
|
||||
luaL_addstring(&b, "\n");
|
||||
while (lua_getstack(L, level++, &ar)) {
|
||||
char buffchunk[60];
|
||||
lua_getinfo(L, "Snl", &ar);
|
||||
luaL_chunkid(buffchunk, ar.source, sizeof(buffchunk));
|
||||
if (level == 2) strcat(buff, "stack traceback:\n");
|
||||
strcat(buff, " ");
|
||||
if (strlen(buff) > MAXMESSAGE-MESSAGESIZE) {
|
||||
strcat(buff, "...\n");
|
||||
break; /* buffer is full */
|
||||
char buff[120]; /* enough to fit following `sprintf's */
|
||||
if (level == 2)
|
||||
luaL_addstring(&b, "stack traceback:\n");
|
||||
else if (level > LEVELS1 && firstpart) {
|
||||
/* no more than `LEVELS2' more levels? */
|
||||
if (!lua_getstack(L, level+LEVELS2, &ar))
|
||||
level--; /* keep going */
|
||||
else {
|
||||
luaL_addstring(&b, " ...\n"); /* too many levels */
|
||||
while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
|
||||
level++;
|
||||
}
|
||||
firstpart = 0;
|
||||
continue;
|
||||
}
|
||||
sprintf(buff, "%4d: ", level-1);
|
||||
luaL_addstring(&b, buff);
|
||||
lua_getinfo(L, "Snl", &ar);
|
||||
switch (*ar.namewhat) {
|
||||
case 'g': case 'l': /* global, local */
|
||||
sprintf(buff+strlen(buff), "function `%.50s'", ar.name);
|
||||
sprintf(buff, "function `%.50s'", ar.name);
|
||||
break;
|
||||
case 'f': /* field */
|
||||
sprintf(buff+strlen(buff), "method `%.50s'", ar.name);
|
||||
sprintf(buff, "method `%.50s'", ar.name);
|
||||
break;
|
||||
case 't': /* tag method */
|
||||
sprintf(buff+strlen(buff), "`%.50s' tag method", ar.name);
|
||||
sprintf(buff, "`%.50s' tag method", ar.name);
|
||||
break;
|
||||
default: {
|
||||
if (*ar.what == 'm') /* main? */
|
||||
sprintf(buff+strlen(buff), "main of %.70s", buffchunk);
|
||||
sprintf(buff, "main of %.70s", ar.short_src);
|
||||
else if (*ar.what == 'C') /* C function? */
|
||||
sprintf(buff+strlen(buff), "%.70s", buffchunk);
|
||||
sprintf(buff, "%.70s", ar.short_src);
|
||||
else
|
||||
sprintf(buff+strlen(buff), "function <%d:%.70s>",
|
||||
ar.linedefined, buffchunk);
|
||||
ar.source = NULL;
|
||||
sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src);
|
||||
ar.source = NULL; /* do not print source again */
|
||||
}
|
||||
}
|
||||
if (ar.currentline > 0)
|
||||
sprintf(buff+strlen(buff), " at line %d", ar.currentline);
|
||||
if (ar.source)
|
||||
sprintf(buff+strlen(buff), " [%.70s]", buffchunk);
|
||||
strcat(buff, "\n");
|
||||
luaL_addstring(&b, buff);
|
||||
if (ar.currentline > 0) {
|
||||
sprintf(buff, " at line %d", ar.currentline);
|
||||
luaL_addstring(&b, buff);
|
||||
}
|
||||
if (ar.source) {
|
||||
sprintf(buff, " [%.70s]", ar.short_src);
|
||||
luaL_addstring(&b, buff);
|
||||
}
|
||||
luaL_addstring(&b, "\n");
|
||||
}
|
||||
if (lua_isfunction(L, alertfunc)) { /* avoid loop if _ALERT is not defined */
|
||||
lua_pushstring(L, buff);
|
||||
lua_callfunction(L, alertfunc);
|
||||
luaL_pushresult(&b);
|
||||
lua_getglobal(L, LUA_ALERT);
|
||||
if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
|
||||
lua_pushvalue(L, -2); /* error message */
|
||||
lua_rawcall(L, 1, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static const struct luaL_reg iolib[] = {
|
||||
{"_ERRORMESSAGE", errorfb},
|
||||
{LUA_ERRORMESSAGE, errorfb},
|
||||
{"clock", io_clock},
|
||||
{"date", io_date},
|
||||
{"debug", io_debug},
|
||||
@@ -613,36 +684,34 @@ static const struct luaL_reg iolibtag[] = {
|
||||
|
||||
|
||||
static void openwithcontrol (lua_State *L) {
|
||||
IOCtrl *ctrl = (IOCtrl *)malloc(sizeof(IOCtrl));
|
||||
IOCtrl *ctrl = (IOCtrl *)lua_newuserdata(L, sizeof(IOCtrl));
|
||||
unsigned int i;
|
||||
int ctrltag = lua_newtag(L);
|
||||
ctrl->iotag = lua_newtag(L);
|
||||
ctrl->closedtag = lua_newtag(L);
|
||||
for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
|
||||
/* put `ctrl' as upvalue for these functions */
|
||||
lua_pushusertag(L, ctrl, ctrltag);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_pushcclosure(L, iolibtag[i].func, 1);
|
||||
lua_setglobal(L, iolibtag[i].name);
|
||||
}
|
||||
/* create references to variable names */
|
||||
lua_pushstring(L, filenames[INFILE]);
|
||||
ctrl->ref[INFILE] = lua_ref(L, 1);
|
||||
lua_pushstring(L, filenames[OUTFILE]);
|
||||
ctrl->ref[OUTFILE] = lua_ref(L, 1);
|
||||
/* predefined file handles */
|
||||
ctrl->file[INFILE] = stdin;
|
||||
setfile(L, ctrl, stdin, INFILE);
|
||||
ctrl->file[OUTFILE] = stdout;
|
||||
setfile(L, ctrl, stdout, OUTFILE);
|
||||
setfilebyname(L, ctrl, stdin, "_STDIN");
|
||||
setfilebyname(L, ctrl, stdout, "_STDOUT");
|
||||
setfilebyname(L, ctrl, stderr, "_STDERR");
|
||||
/* change file when assigned */
|
||||
lua_pushusertag(L, ctrl, ctrltag);
|
||||
lua_pushcclosure(L, atribTM, 1);
|
||||
lua_settagmethod(L, ctrl->iotag, "setglobal");
|
||||
/* delete `ctrl' when collected */
|
||||
lua_pushusertag(L, ctrl, ctrltag);
|
||||
lua_pushcclosure(L, ctrl_collect, 1);
|
||||
lua_settagmethod(L, ctrltag, "gc");
|
||||
/* close files when collected */
|
||||
lua_pushcclosure(L, file_collect, 1); /* pops `ctrl' from stack */
|
||||
lua_settagmethod(L, ctrl->iotag, "gc");
|
||||
}
|
||||
|
||||
void lua_iolibopen (lua_State *L) {
|
||||
|
||||
LUALIB_API void lua_iolibopen (lua_State *L) {
|
||||
luaL_openl(L, iolib);
|
||||
openwithcontrol(L);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.c,v 1.56 2000/04/07 13:11:49 roberto Exp roberto $
|
||||
** $Id: llex.c,v 1.71 2000/09/27 17:41:58 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,15 +9,15 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "luadebug.h"
|
||||
#include "lzio.h"
|
||||
|
||||
@@ -26,9 +26,6 @@
|
||||
#define next(LS) (LS->current = zgetc(LS->z))
|
||||
|
||||
|
||||
#define save(L, c) luaL_addchar(L, c)
|
||||
#define save_and_next(L, LS) (save(L, LS->current), next(LS))
|
||||
|
||||
|
||||
/* ORDER RESERVED */
|
||||
static const char *const token2string [] = {
|
||||
@@ -48,10 +45,20 @@ void luaX_init (lua_State *L) {
|
||||
|
||||
#define MAXSRC 80
|
||||
|
||||
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
|
||||
if (val > limit) {
|
||||
char buff[100];
|
||||
sprintf(buff, "too many %.50s (limit=%d)", msg, limit);
|
||||
luaX_error(ls, buff, ls->t.token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
|
||||
char buff[MAXSRC];
|
||||
luaL_chunkid(buff, zname(ls->z), sizeof(buff));
|
||||
luaL_verror(ls->L, "%.100s;\n last token read: `%.50s' at line %d in %.80s",
|
||||
luaO_chunkid(buff, ls->source->str, sizeof(buff));
|
||||
luaO_verror(ls->L, "%.99s;\n last token read: `%.30s' at line %d in %.80s",
|
||||
s, token, ls->linenumber, buff);
|
||||
}
|
||||
|
||||
@@ -59,10 +66,8 @@ void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
|
||||
void luaX_error (LexState *ls, const char *s, int token) {
|
||||
char buff[TOKEN_LEN];
|
||||
luaX_token2str(token, buff);
|
||||
if (buff[0] == '\0') {
|
||||
save(ls->L, '\0');
|
||||
luaX_syntaxerror(ls, s, luaL_buffer(ls->L));
|
||||
}
|
||||
if (buff[0] == '\0')
|
||||
luaX_syntaxerror(ls, s, ls->L->Mbuffer);
|
||||
else
|
||||
luaX_syntaxerror(ls, s, buff);
|
||||
}
|
||||
@@ -85,140 +90,26 @@ static void luaX_invalidchar (LexState *ls, int c) {
|
||||
}
|
||||
|
||||
|
||||
static void firstline (LexState *LS)
|
||||
{
|
||||
int c = zgetc(LS->z);
|
||||
if (c == '#')
|
||||
while ((c=zgetc(LS->z)) != '\n' && c != EOZ) /* skip first line */;
|
||||
zungetc(LS->z);
|
||||
}
|
||||
|
||||
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z) {
|
||||
LS->L = L;
|
||||
LS->current = '\n';
|
||||
LS->linenumber = 0;
|
||||
LS->iflevel = 0;
|
||||
LS->ifstate[0].skip = 0;
|
||||
LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
|
||||
LS->z = z;
|
||||
LS->fs = NULL;
|
||||
firstline(LS);
|
||||
luaL_resetbuffer(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** PRAGMAS
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void skipspace (LexState *LS) {
|
||||
while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
|
||||
next(LS);
|
||||
}
|
||||
|
||||
|
||||
static int checkcond (lua_State *L, LexState *LS, const char *buff) {
|
||||
static const char *const opts[] = {"nil", "1", NULL};
|
||||
int i = luaL_findstring(buff, opts);
|
||||
if (i >= 0) return i;
|
||||
else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
|
||||
return luaS_globaldefined(L, buff);
|
||||
else {
|
||||
luaX_syntaxerror(LS, "invalid $if condition", buff);
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void readname (LexState *LS, char *buff) {
|
||||
int i = 0;
|
||||
skipspace(LS);
|
||||
while (isalnum(LS->current) || LS->current == '_') {
|
||||
if (i >= PRAGMASIZE) {
|
||||
buff[PRAGMASIZE] = 0;
|
||||
luaX_syntaxerror(LS, "pragma too long", buff);
|
||||
}
|
||||
buff[i++] = (char)LS->current;
|
||||
next(LS);
|
||||
}
|
||||
buff[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (lua_State *L, LexState *LS);
|
||||
|
||||
|
||||
static void ifskip (lua_State *L, LexState *LS) {
|
||||
while (LS->ifstate[LS->iflevel].skip) {
|
||||
if (LS->current == '\n')
|
||||
inclinenumber(L, LS);
|
||||
else if (LS->current == EOZ)
|
||||
luaX_error(LS, "input ends inside a $if", TK_EOS);
|
||||
else next(LS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (lua_State *L, LexState *LS) {
|
||||
static const char *const pragmas [] =
|
||||
{"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
|
||||
static void inclinenumber (LexState *LS) {
|
||||
next(LS); /* skip '\n' */
|
||||
++LS->linenumber;
|
||||
if (LS->current == '$') { /* is a pragma? */
|
||||
char buff[PRAGMASIZE+1];
|
||||
int ifnot = 0;
|
||||
int skip = LS->ifstate[LS->iflevel].skip;
|
||||
next(LS); /* skip $ */
|
||||
readname(LS, buff);
|
||||
switch (luaL_findstring(buff, pragmas)) {
|
||||
case 0: /* debug */
|
||||
if (!skip) L->debug = 1;
|
||||
break;
|
||||
case 1: /* nodebug */
|
||||
if (!skip) L->debug = 0;
|
||||
break;
|
||||
case 2: /* endinput */
|
||||
if (!skip) {
|
||||
LS->current = EOZ;
|
||||
LS->iflevel = 0; /* to allow $endinput inside a $if */
|
||||
}
|
||||
break;
|
||||
case 3: /* end */
|
||||
if (LS->iflevel-- == 0)
|
||||
luaX_syntaxerror(LS, "unmatched $end", "$end");
|
||||
break;
|
||||
case 4: /* ifnot */
|
||||
ifnot = 1;
|
||||
/* go through */
|
||||
case 5: /* if */
|
||||
if (LS->iflevel == MAX_IFS-1)
|
||||
luaX_syntaxerror(LS, "too many nested $ifs", "$if");
|
||||
readname(LS, buff);
|
||||
LS->iflevel++;
|
||||
LS->ifstate[LS->iflevel].elsepart = 0;
|
||||
LS->ifstate[LS->iflevel].condition = checkcond(L, LS, buff) ? !ifnot : ifnot;
|
||||
LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
|
||||
break;
|
||||
case 6: /* else */
|
||||
if (LS->ifstate[LS->iflevel].elsepart)
|
||||
luaX_syntaxerror(LS, "unmatched $else", "$else");
|
||||
LS->ifstate[LS->iflevel].elsepart = 1;
|
||||
LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip ||
|
||||
LS->ifstate[LS->iflevel].condition;
|
||||
break;
|
||||
default:
|
||||
luaX_syntaxerror(LS, "unknown pragma", buff);
|
||||
}
|
||||
skipspace(LS);
|
||||
if (LS->current == '\n') /* pragma must end with a '\n' ... */
|
||||
inclinenumber(L, LS);
|
||||
else if (LS->current != EOZ) /* or eof */
|
||||
luaX_syntaxerror(LS, "invalid pragma format", buff);
|
||||
ifskip(L, LS);
|
||||
luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
|
||||
}
|
||||
|
||||
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
|
||||
LS->L = L;
|
||||
LS->lookahead.token = TK_EOS; /* no look-ahead token */
|
||||
LS->z = z;
|
||||
LS->fs = NULL;
|
||||
LS->linenumber = 1;
|
||||
LS->lastline = 1;
|
||||
LS->source = source;
|
||||
next(LS); /* read first char */
|
||||
if (LS->current == '#') {
|
||||
do { /* skip first line */
|
||||
next(LS);
|
||||
} while (LS->current != '\n' && LS->current != EOZ);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,61 +122,133 @@ static void inclinenumber (lua_State *L, LexState *LS) {
|
||||
*/
|
||||
|
||||
|
||||
/* use Mbuffer to store names, literal strings and numbers */
|
||||
|
||||
static void read_long_string (lua_State *L, LexState *LS) {
|
||||
int cont = 0;
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
luaX_error(LS, "unfinished long string", TK_STRING);
|
||||
break; /* to avoid warnings */
|
||||
case '[':
|
||||
save_and_next(L, LS);
|
||||
if (LS->current == '[') {
|
||||
cont++;
|
||||
save_and_next(L, LS);
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next(L, LS);
|
||||
if (LS->current == ']') {
|
||||
if (cont == 0) goto endloop;
|
||||
cont--;
|
||||
save_and_next(L, LS);
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
save(L, '\n');
|
||||
inclinenumber(L, LS);
|
||||
continue;
|
||||
default:
|
||||
save_and_next(L, LS);
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(L, LS); /* skip the second ']' */
|
||||
LS->seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+2),
|
||||
L->Mbuffnext-L->Mbuffbase-4);
|
||||
#define EXTRABUFF 128
|
||||
#define checkbuffer(L, n, len) if ((len)+(n) > L->Mbuffsize) \
|
||||
luaO_openspace(L, (len)+(n)+EXTRABUFF)
|
||||
|
||||
#define save(L, c, l) (L->Mbuffer[l++] = (char)c)
|
||||
#define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS))
|
||||
|
||||
|
||||
static const char *readname (LexState *LS) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
do {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(L, '\0', l);
|
||||
return L->Mbuffer;
|
||||
}
|
||||
|
||||
|
||||
static void read_string (lua_State *L, LexState *LS, int del) {
|
||||
save_and_next(L, LS);
|
||||
/* LUA_NUMBER */
|
||||
static void read_number (LexState *LS, int comma, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
if (comma) save(L, '.', l);
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
if (LS->current == '.') {
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == '.') {
|
||||
save_and_next(L, LS, l);
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "ambiguous syntax"
|
||||
" (decimal point x string concatenation)", TK_NUMBER);
|
||||
}
|
||||
}
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
if (LS->current == 'e' || LS->current == 'E') {
|
||||
save_and_next(L, LS, l); /* read 'E' */
|
||||
if (LS->current == '+' || LS->current == '-')
|
||||
save_and_next(L, LS, l); /* optional exponent sign */
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
}
|
||||
save(L, '\0', l);
|
||||
if (!luaO_str2d(L->Mbuffer, &seminfo->r))
|
||||
luaX_error(LS, "malformed number", TK_NUMBER);
|
||||
}
|
||||
|
||||
|
||||
static void read_long_string (LexState *LS, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
int cont = 0;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
save(L, '[', l); /* save first '[' */
|
||||
save_and_next(L, LS, l); /* pass the second '[' */
|
||||
for (;;) {
|
||||
checkbuffer(L, 10, l);
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "unfinished long string", TK_STRING);
|
||||
break; /* to avoid warnings */
|
||||
case '[':
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == '[') {
|
||||
cont++;
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == ']') {
|
||||
if (cont == 0) goto endloop;
|
||||
cont--;
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
save(L, '\n', l);
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
default:
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(L, LS, l); /* skip the second ']' */
|
||||
save(L, '\0', l);
|
||||
seminfo->ts = luaS_newlstr(L, L->Mbuffer+2, l-5);
|
||||
}
|
||||
|
||||
|
||||
static void read_string (LexState *LS, int del, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
while (LS->current != del) {
|
||||
checkbuffer(L, 10, l);
|
||||
switch (LS->current) {
|
||||
case EOZ: case '\n':
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "unfinished string", TK_STRING);
|
||||
break; /* to avoid warnings */
|
||||
case '\\':
|
||||
next(LS); /* do not save the '\' */
|
||||
switch (LS->current) {
|
||||
case 'a': save(L, '\a'); next(LS); break;
|
||||
case 'b': save(L, '\b'); next(LS); break;
|
||||
case 'f': save(L, '\f'); next(LS); break;
|
||||
case 'n': save(L, '\n'); next(LS); break;
|
||||
case 'r': save(L, '\r'); next(LS); break;
|
||||
case 't': save(L, '\t'); next(LS); break;
|
||||
case 'v': save(L, '\v'); next(LS); break;
|
||||
case '\n': save(L, '\n'); inclinenumber(L, LS); break;
|
||||
case 'a': save(L, '\a', l); next(LS); break;
|
||||
case 'b': save(L, '\b', l); next(LS); break;
|
||||
case 'f': save(L, '\f', l); next(LS); break;
|
||||
case 'n': save(L, '\n', l); next(LS); break;
|
||||
case 'r': save(L, '\r', l); next(LS); break;
|
||||
case 't': save(L, '\t', l); next(LS); break;
|
||||
case 'v': save(L, '\v', l); next(LS); break;
|
||||
case '\n': save(L, '\n', l); inclinenumber(LS); break;
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9': {
|
||||
int c = 0;
|
||||
@@ -294,28 +257,28 @@ static void read_string (lua_State *L, LexState *LS, int del) {
|
||||
c = 10*c + (LS->current-'0');
|
||||
next(LS);
|
||||
} while (++i<3 && isdigit(LS->current));
|
||||
if (c != (unsigned char)c)
|
||||
if (c != (unsigned char)c) {
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "escape sequence too large", TK_STRING);
|
||||
save(L, c);
|
||||
}
|
||||
save(L, c, l);
|
||||
break;
|
||||
}
|
||||
default: /* handles \\, \", \', and \? */
|
||||
save(L, LS->current);
|
||||
next(LS);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next(L, LS);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
}
|
||||
save_and_next(L, LS); /* skip delimiter */
|
||||
LS->seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+1),
|
||||
L->Mbuffnext-L->Mbuffbase-2);
|
||||
save_and_next(L, LS, l); /* skip delimiter */
|
||||
save(L, '\0', l);
|
||||
seminfo->ts = luaS_newlstr(L, L->Mbuffer+1, l-3);
|
||||
}
|
||||
|
||||
|
||||
int luaX_lex (LexState *LS) {
|
||||
lua_State *L = LS->L;
|
||||
int luaX_lex (LexState *LS, SemInfo *seminfo) {
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
|
||||
@@ -324,9 +287,13 @@ int luaX_lex (LexState *LS) {
|
||||
continue;
|
||||
|
||||
case '\n':
|
||||
inclinenumber(L, LS);
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
|
||||
case '$':
|
||||
luaX_error(LS, "unexpected `$' (pragmas are no longer supported)", '$');
|
||||
break;
|
||||
|
||||
case '-':
|
||||
next(LS);
|
||||
if (LS->current != '-') return '-';
|
||||
@@ -334,12 +301,10 @@ int luaX_lex (LexState *LS) {
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
luaL_resetbuffer(L);
|
||||
save_and_next(L, LS);
|
||||
next(LS);
|
||||
if (LS->current != '[') return '[';
|
||||
else {
|
||||
save_and_next(L, LS); /* pass the second '[' */
|
||||
read_long_string(L, LS);
|
||||
read_long_string(LS, seminfo);
|
||||
return TK_STRING;
|
||||
}
|
||||
|
||||
@@ -365,13 +330,11 @@ int luaX_lex (LexState *LS) {
|
||||
|
||||
case '"':
|
||||
case '\'':
|
||||
luaL_resetbuffer(L);
|
||||
read_string(L, LS, LS->current);
|
||||
read_string(LS, LS->current, seminfo);
|
||||
return TK_STRING;
|
||||
|
||||
case '.':
|
||||
luaL_resetbuffer(L);
|
||||
save_and_next(L, LS);
|
||||
next(LS);
|
||||
if (LS->current == '.') {
|
||||
next(LS);
|
||||
if (LS->current == '.') {
|
||||
@@ -381,40 +344,17 @@ int luaX_lex (LexState *LS) {
|
||||
else return TK_CONCAT; /* .. */
|
||||
}
|
||||
else if (!isdigit(LS->current)) return '.';
|
||||
else goto fraction; /* LS->current is a digit */
|
||||
else {
|
||||
read_number(LS, 1, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
luaL_resetbuffer(L);
|
||||
do {
|
||||
save_and_next(L, LS);
|
||||
} while (isdigit(LS->current));
|
||||
if (LS->current == '.') {
|
||||
save_and_next(L, LS);
|
||||
if (LS->current == '.') {
|
||||
save(L, '.');
|
||||
luaX_error(LS, "ambiguous syntax"
|
||||
" (decimal point x string concatenation)", TK_NUMBER);
|
||||
}
|
||||
}
|
||||
fraction: /* LUA_NUMBER */
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(L, LS);
|
||||
if (LS->current == 'e' || LS->current == 'E') {
|
||||
save_and_next(L, LS); /* read 'E' */
|
||||
if (LS->current == '+' || LS->current == '-')
|
||||
save_and_next(L, LS); /* optional exponent sign */
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(L, LS);
|
||||
}
|
||||
save(L, '\0');
|
||||
if (!luaO_str2d(L->Mbuffer+L->Mbuffbase, &LS->seminfo.r))
|
||||
luaX_error(LS, "malformed number", TK_NUMBER);
|
||||
read_number(LS, 0, seminfo);
|
||||
return TK_NUMBER;
|
||||
|
||||
case EOZ:
|
||||
if (LS->iflevel > 0)
|
||||
luaX_error(LS, "input ends inside a $if", TK_EOS);
|
||||
return TK_EOS;
|
||||
|
||||
case '_': goto tname;
|
||||
@@ -428,16 +368,10 @@ int luaX_lex (LexState *LS) {
|
||||
return c;
|
||||
}
|
||||
tname: { /* identifier or reserved word */
|
||||
TString *ts;
|
||||
luaL_resetbuffer(L);
|
||||
do {
|
||||
save_and_next(L, LS);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(L, '\0');
|
||||
ts = luaS_new(L, L->Mbuffer+L->Mbuffbase);
|
||||
TString *ts = luaS_new(LS->L, readname(LS));
|
||||
if (ts->marked >= RESERVEDMARK) /* reserved word? */
|
||||
return ts->marked-RESERVEDMARK+FIRST_RESERVED;
|
||||
LS->seminfo.ts = ts;
|
||||
seminfo->ts = ts;
|
||||
return TK_NAME;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.23 2000/04/07 13:11:49 roberto Exp roberto $
|
||||
** $Id: llex.h,v 1.30 2000/06/21 18:13:56 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -35,34 +35,35 @@ enum RESERVED {
|
||||
#define NUM_RESERVED ((int)(TK_WHILE-FIRST_RESERVED+1))
|
||||
|
||||
|
||||
/* `ifState' keeps the state of each nested $if the lexical is dealing with. */
|
||||
typedef union {
|
||||
Number r;
|
||||
TString *ts;
|
||||
} SemInfo; /* semantics information */
|
||||
|
||||
struct ifState {
|
||||
int elsepart; /* true if it's in the $else part */
|
||||
int condition; /* true if $if condition is true */
|
||||
int skip; /* true if part must be skipped */
|
||||
};
|
||||
|
||||
typedef struct Token {
|
||||
int token;
|
||||
SemInfo seminfo;
|
||||
} Token;
|
||||
|
||||
|
||||
typedef struct LexState {
|
||||
int current; /* look ahead character */
|
||||
int token; /* look ahead token */
|
||||
int current; /* current character */
|
||||
Token t; /* current token */
|
||||
Token lookahead; /* look ahead token */
|
||||
struct FuncState *fs; /* `FuncState' is private to the parser */
|
||||
struct lua_State *L;
|
||||
union {
|
||||
Number r;
|
||||
TString *ts;
|
||||
} seminfo; /* semantics information */
|
||||
struct zio *z; /* input stream */
|
||||
int linenumber; /* input line counter */
|
||||
int iflevel; /* level of nested $if's (for lexical analysis) */
|
||||
struct ifState ifstate[MAX_IFS];
|
||||
int lastline; /* line of last token `consumed' */
|
||||
TString *source; /* current source name */
|
||||
} LexState;
|
||||
|
||||
|
||||
void luaX_init (lua_State *L);
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z);
|
||||
int luaX_lex (LexState *LS);
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source);
|
||||
int luaX_lex (LexState *LS, SemInfo *seminfo);
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg);
|
||||
void luaX_syntaxerror (LexState *ls, const char *s, const char *token);
|
||||
void luaX_error (LexState *ls, const char *s, int token);
|
||||
void luaX_token2str (int token, char *s);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.4 2000/03/31 16:28:45 roberto Exp roberto $
|
||||
** Limits, basic types, and some other "instalation-dependent" definitions
|
||||
** $Id: llimits.h,v 1.18 2000/10/09 13:47:32 roberto Exp roberto $
|
||||
** Limits, basic types, and some other "installation-dependent" definitions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -9,6 +9,26 @@
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** try to find number of bits in an integer
|
||||
*/
|
||||
#ifndef BITS_INT
|
||||
/* avoid overflows in comparison */
|
||||
#if INT_MAX-20 < 32760
|
||||
#define BITS_INT 16
|
||||
#else
|
||||
#if INT_MAX > 2147483640L
|
||||
/* machine has at least 32 bits */
|
||||
#define BITS_INT 32
|
||||
#else
|
||||
#error "you must define BITS_INT with number of bits in an integer"
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ -21,6 +41,19 @@
|
||||
|
||||
typedef LUA_NUM_TYPE Number;
|
||||
|
||||
/* function to convert a Number to a string */
|
||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||
#define lua_number2str(s,n) sprintf((s), NUMBER_FMT, (n))
|
||||
|
||||
/* function to convert a string to a Number */
|
||||
#define lua_str2number(s,p) strtod((s), (p))
|
||||
|
||||
|
||||
|
||||
typedef unsigned long lint32; /* unsigned int with at least 32 bits */
|
||||
|
||||
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0)-2)
|
||||
|
||||
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
|
||||
@@ -29,16 +62,9 @@ typedef LUA_NUM_TYPE Number;
|
||||
** conversion of pointer to int (for hashing only)
|
||||
** (the shift removes bits that are usually 0 because of alignment)
|
||||
*/
|
||||
#define IntPoint(L, p) (((unsigned long)(p)) >> 3)
|
||||
#define IntPoint(p) (((unsigned long)(p)) >> 3)
|
||||
|
||||
|
||||
/*
|
||||
** number of `blocks' for garbage collection: each reference to other
|
||||
** objects count 1, and each 32 bytes of `raw' memory count 1; we add
|
||||
** 2 to the total as a minimum (and also to count the overhead of malloc)
|
||||
*/
|
||||
#define numblocks(L, o,b) ((o)+((b)>>5)+2)
|
||||
|
||||
|
||||
#define MINPOWER2 4 /* minimum size for "growing" vectors */
|
||||
|
||||
@@ -50,9 +76,14 @@ typedef LUA_NUM_TYPE Number;
|
||||
|
||||
|
||||
|
||||
/* type to ensure maximum alignment */
|
||||
union L_Umaxalign { double d; char *s; long l; };
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** type for virtual-machine instructions
|
||||
** must be an unsigned with 4 bytes (see details in lopcodes.h)
|
||||
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||
** For a very small machine, you may change that to 2 bytes (and adjust
|
||||
** the following limits accordingly)
|
||||
*/
|
||||
@@ -60,51 +91,56 @@ typedef unsigned long Instruction;
|
||||
|
||||
|
||||
/*
|
||||
** limits for opcode arguments.
|
||||
** size and position of opcode arguments.
|
||||
** For an instruction with 2 bytes, size is 16, and size_b can be 5
|
||||
** (accordingly, size_u will be 10, and size_a will be 5)
|
||||
*/
|
||||
#define SIZE_INSTRUCTION 32
|
||||
#define SIZE_OP 6
|
||||
#define SIZE_B 9
|
||||
|
||||
#define SIZE_OP 6
|
||||
#define SIZE_U (SIZE_INSTRUCTION-SIZE_OP)
|
||||
#define POS_U SIZE_OP
|
||||
#define POS_B SIZE_OP
|
||||
#define SIZE_A (SIZE_INSTRUCTION-(SIZE_OP+SIZE_B))
|
||||
#define POS_A (SIZE_OP+SIZE_B)
|
||||
|
||||
#define MAXARG_U ((1<<SIZE_U)-1)
|
||||
#define MAXARG_S (MAXARG_U>>1) /* `S' is signed */
|
||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||
|
||||
|
||||
/*
|
||||
** we use int to manipulate most arguments, so they must fit
|
||||
** limits for opcode arguments.
|
||||
** we use (signed) int to manipulate most arguments,
|
||||
** so they must fit in BITS_INT-1 bits (-1 for sign)
|
||||
*/
|
||||
#if MAXARG_U > MAX_INT
|
||||
#undef MAXARG_U
|
||||
#if SIZE_U < BITS_INT-1
|
||||
#define MAXARG_U ((1<<SIZE_U)-1)
|
||||
#define MAXARG_S (MAXARG_U>>1) /* `S' is signed */
|
||||
#else
|
||||
#define MAXARG_U MAX_INT
|
||||
#endif
|
||||
|
||||
#if MAXARG_S > MAX_INT
|
||||
#undef MAXARG_S
|
||||
#define MAXARG_S MAX_INT
|
||||
#endif
|
||||
|
||||
#if MAXARG_A > MAX_INT
|
||||
#undef MAXARG_A
|
||||
#if SIZE_A < BITS_INT-1
|
||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||
#else
|
||||
#define MAXARG_A MAX_INT
|
||||
#endif
|
||||
|
||||
#if MAXARG_B > MAX_INT
|
||||
#undef MAXARG_B
|
||||
#if SIZE_B < BITS_INT-1
|
||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||
#else
|
||||
#define MAXARG_B MAX_INT
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum stack size in a function */
|
||||
#ifndef MAXSTACK
|
||||
#define MAXSTACK 250
|
||||
#endif
|
||||
|
||||
#if MAXSTACK > MAXARG_B
|
||||
#undef MAXSTACK
|
||||
#define MAXSTACK MAXARG_B
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of local variables */
|
||||
@@ -127,14 +163,6 @@ typedef unsigned long Instruction;
|
||||
#endif
|
||||
|
||||
|
||||
/* special code for multiple returns */
|
||||
#define MULT_RET 255 /* (<=MAXARG_B) */
|
||||
#if MULT_RET>MAXARG_B
|
||||
#undef MULT_RET
|
||||
#define MULT_RET MAXARG_B
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of variables in the left side of an assignment */
|
||||
#ifndef MAXVARSLH
|
||||
#define MAXVARSLH 100 /* arbitrary limit (<MULT_RET) */
|
||||
@@ -167,24 +195,6 @@ typedef unsigned long Instruction;
|
||||
#define RFIELDS_PER_FLUSH (LFIELDS_PER_FLUSH/2)
|
||||
|
||||
|
||||
/* maximum number of values printed in one call to `print' */
|
||||
#ifndef MAXPRINT
|
||||
#define MAXPRINT 40 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum depth of nested $ifs */
|
||||
#ifndef MAX_IFS
|
||||
#define MAX_IFS 5 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum size of a pragma line */
|
||||
#ifndef PRAGMASIZE
|
||||
#define PRAGMASIZE 80 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum lookback to find a real constant (for code generation) */
|
||||
#ifndef LOOKBACKNUMS
|
||||
#define LOOKBACKNUMS 20 /* arbitrary constant */
|
||||
|
||||
+79
-49
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.23 1999/12/27 17:33:22 roberto Exp roberto $
|
||||
** $Id: lmathlib.c,v 1.31 2000/10/27 16:15:53 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,10 +8,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
@@ -34,138 +33,169 @@
|
||||
#endif
|
||||
|
||||
|
||||
static void math_abs (lua_State *L) {
|
||||
static int math_abs (lua_State *L) {
|
||||
lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_sin (lua_State *L) {
|
||||
static int math_sin (lua_State *L) {
|
||||
lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_cos (lua_State *L) {
|
||||
static int math_cos (lua_State *L) {
|
||||
lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_tan (lua_State *L) {
|
||||
static int math_tan (lua_State *L) {
|
||||
lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_asin (lua_State *L) {
|
||||
static int math_asin (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_acos (lua_State *L) {
|
||||
static int math_acos (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_atan (lua_State *L) {
|
||||
static int math_atan (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_atan2 (lua_State *L) {
|
||||
static int math_atan2 (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_ceil (lua_State *L) {
|
||||
static int math_ceil (lua_State *L) {
|
||||
lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_floor (lua_State *L) {
|
||||
static int math_floor (lua_State *L) {
|
||||
lua_pushnumber(L, floor(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_mod (lua_State *L) {
|
||||
static int math_mod (lua_State *L) {
|
||||
lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_sqrt (lua_State *L) {
|
||||
static int math_sqrt (lua_State *L) {
|
||||
lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_pow (lua_State *L) {
|
||||
static int math_pow (lua_State *L) {
|
||||
lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_log (lua_State *L) {
|
||||
static int math_log (lua_State *L) {
|
||||
lua_pushnumber(L, log(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_log10 (lua_State *L) {
|
||||
static int math_log10 (lua_State *L) {
|
||||
lua_pushnumber(L, log10(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_exp (lua_State *L) {
|
||||
static int math_exp (lua_State *L) {
|
||||
lua_pushnumber(L, exp(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_deg (lua_State *L) {
|
||||
static int math_deg (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_rad (lua_State *L) {
|
||||
static int math_rad (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_frexp (lua_State *L) {
|
||||
static int math_frexp (lua_State *L) {
|
||||
int e;
|
||||
lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e));
|
||||
lua_pushnumber(L, e);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static void math_ldexp (lua_State *L) {
|
||||
static int math_ldexp (lua_State *L) {
|
||||
lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_min (lua_State *L) {
|
||||
int i = 1;
|
||||
double dmin = luaL_check_number(L, i);
|
||||
while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
|
||||
static int math_min (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
double dmin = luaL_check_number(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
double d = luaL_check_number(L, i);
|
||||
if (d < dmin)
|
||||
dmin = d;
|
||||
}
|
||||
lua_pushnumber(L, dmin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_max (lua_State *L) {
|
||||
int i = 1;
|
||||
double dmax = luaL_check_number(L, i);
|
||||
while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
|
||||
static int math_max (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
double dmax = luaL_check_number(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
double d = luaL_check_number(L, i);
|
||||
if (d > dmax)
|
||||
dmax = d;
|
||||
}
|
||||
lua_pushnumber(L, dmax);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_random (lua_State *L) {
|
||||
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 */
|
||||
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
||||
if (lua_getparam(L, 1) == LUA_NOOBJECT) /* no arguments? */
|
||||
lua_pushnumber(L, r); /* Number between 0 and 1 */
|
||||
else {
|
||||
int l, u; /* lower & upper limits */
|
||||
if (lua_getparam(L, 2) == LUA_NOOBJECT) { /* only one argument? */
|
||||
l = 1;
|
||||
u = luaL_check_int(L, 1);
|
||||
switch (lua_gettop(L)) { /* check number of arguments */
|
||||
case 0: { /* no arguments */
|
||||
lua_pushnumber(L, r); /* Number between 0 and 1 */
|
||||
break;
|
||||
}
|
||||
else { /* two arguments */
|
||||
l = luaL_check_int(L, 1);
|
||||
u = luaL_check_int(L, 2);
|
||||
case 1: { /* only upper limit */
|
||||
int u = luaL_check_int(L, 1);
|
||||
luaL_arg_check(L, 1<=u, 1, "interval is empty");
|
||||
lua_pushnumber(L, (int)(r*u)+1); /* integer between 1 and `u' */
|
||||
break;
|
||||
}
|
||||
luaL_arg_check(L, l<=u, 1, "interval is empty");
|
||||
lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
|
||||
case 2: { /* lower and upper limits */
|
||||
int l = luaL_check_int(L, 1);
|
||||
int u = luaL_check_int(L, 2);
|
||||
luaL_arg_check(L, l<=u, 2, "interval is empty");
|
||||
lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
|
||||
break;
|
||||
}
|
||||
default: lua_error(L, "wrong number of arguments");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_randomseed (lua_State *L) {
|
||||
static int math_randomseed (lua_State *L) {
|
||||
srand(luaL_check_int(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -198,11 +228,11 @@ static const struct luaL_reg mathlib[] = {
|
||||
/*
|
||||
** Open math library
|
||||
*/
|
||||
void lua_mathlibopen (lua_State *L) {
|
||||
LUALIB_API void lua_mathlibopen (lua_State *L) {
|
||||
luaL_openl(L, mathlib);
|
||||
lua_pushcfunction(L, math_pow);
|
||||
lua_pushnumber(L, 0); /* to get its tag */
|
||||
lua_settagmethod(L, lua_tag(L, lua_pop(L)), "pow");
|
||||
lua_pushnumber(L, PI); lua_setglobal(L, "PI");
|
||||
lua_settagmethod(L, LUA_TNUMBER, "pow");
|
||||
lua_pushnumber(L, PI);
|
||||
lua_setglobal(L, "PI");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.28 2000/03/10 18:37:44 roberto Exp roberto $
|
||||
** $Id: lmem.c,v 1.38 2000/10/26 12:47:05 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,26 +7,17 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/*
|
||||
** Number ANSI systems do not need these tests;
|
||||
** but some systems (Sun OS) are not that ANSI...
|
||||
*/
|
||||
#ifdef OLD_ANSI
|
||||
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
||||
#define free(b) if (b) (free)(b)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
#ifdef LUA_DEBUG
|
||||
/*
|
||||
** {======================================================================
|
||||
** Controlled version for realloc.
|
||||
@@ -35,16 +26,16 @@
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define realloc(b, s) debug_realloc(b, s)
|
||||
#define malloc(b) debug_realloc(NULL, 0)
|
||||
#define malloc(b) debug_realloc(NULL, b)
|
||||
#define free(b) debug_realloc(b, 0)
|
||||
|
||||
|
||||
/* ensures maximum alignment for HEADER */
|
||||
#define HEADER (sizeof(double)>sizeof(long) ? sizeof(double) : sizeof(long))
|
||||
#define HEADER (sizeof(union L_Umaxalign))
|
||||
|
||||
#define MARKSIZE 16
|
||||
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
||||
@@ -55,6 +46,7 @@
|
||||
unsigned long memdebug_numblocks = 0;
|
||||
unsigned long memdebug_total = 0;
|
||||
unsigned long memdebug_maxmem = 0;
|
||||
unsigned long memdebug_memlimit = LONG_MAX;
|
||||
|
||||
|
||||
static void *checkblock (void *block) {
|
||||
@@ -84,10 +76,13 @@ static void *debug_realloc (void *block, size_t size) {
|
||||
freeblock(block);
|
||||
return NULL;
|
||||
}
|
||||
else if (memdebug_total+size > memdebug_memlimit)
|
||||
return NULL; /* to test memory allocation errors */
|
||||
else {
|
||||
size_t realsize = HEADER+size+MARKSIZE;
|
||||
char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
|
||||
int i;
|
||||
if (realsize < size) return NULL; /* overflow! */
|
||||
if (newblock == NULL) return NULL;
|
||||
if (block) {
|
||||
size_t oldsize = *blocksize(block);
|
||||
@@ -111,10 +106,20 @@ static void *debug_realloc (void *block, size_t size) {
|
||||
|
||||
|
||||
|
||||
void *luaM_growaux (lua_State *L, void *block, unsigned long nelems,
|
||||
int inc, int size, const char *errormsg, unsigned long limit) {
|
||||
unsigned long newn = nelems+inc;
|
||||
if (newn >= limit) lua_error(L, errormsg);
|
||||
/*
|
||||
** Real ISO (ANSI) systems do not need these tests;
|
||||
** but some systems (Sun OS) are not that ISO...
|
||||
*/
|
||||
#ifdef OLD_ANSI
|
||||
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
||||
#define free(b) if (b) (free)(b)
|
||||
#endif
|
||||
|
||||
|
||||
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
|
||||
int inc, size_t size, const char *errormsg, size_t limit) {
|
||||
size_t newn = nelems+inc;
|
||||
if (nelems >= limit-inc) lua_error(L, errormsg);
|
||||
if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
|
||||
(nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
|
||||
return block; /* do not need to reallocate */
|
||||
@@ -126,16 +131,19 @@ void *luaM_growaux (lua_State *L, void *block, unsigned long nelems,
|
||||
/*
|
||||
** generic allocation routine.
|
||||
*/
|
||||
void *luaM_realloc (lua_State *L, void *block, unsigned long size) {
|
||||
void *luaM_realloc (lua_State *L, void *block, lint32 size) {
|
||||
if (size == 0) {
|
||||
free(block); /* block may be NULL; that is OK for free */
|
||||
return NULL;
|
||||
}
|
||||
else if ((size_t)size != size)
|
||||
else if (size >= MAX_SIZET)
|
||||
lua_error(L, "memory allocation error: block too big");
|
||||
block = realloc(block, size);
|
||||
if (block == NULL)
|
||||
lua_error(L, memEM);
|
||||
if (block == NULL) {
|
||||
if (L)
|
||||
luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
|
||||
else return NULL; /* error before creating state! */
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.12 2000/01/13 16:30:47 roberto Exp roberto $
|
||||
** $Id: lmem.h,v 1.15 2000/08/07 18:39:16 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,35 +8,33 @@
|
||||
#define lmem_h
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
/* memory error messages */
|
||||
#define codeEM "code size overflow"
|
||||
#define constantEM "constant table overflow"
|
||||
#define refEM "reference table overflow"
|
||||
#define tableEM "table overflow"
|
||||
#define memEM "not enough memory"
|
||||
#define arrEM "internal array larger than `int' limit"
|
||||
void *luaM_realloc (lua_State *L, void *oldblock, lint32 size);
|
||||
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
|
||||
int inc, size_t size, const char *errormsg,
|
||||
size_t limit);
|
||||
|
||||
void *luaM_realloc (lua_State *L, void *oldblock, unsigned long size);
|
||||
void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, int inc, int size,
|
||||
const char *errormsg, unsigned long limit);
|
||||
|
||||
#define luaM_free(L, b) luaM_realloc(L, (b), 0)
|
||||
#define luaM_free(L, b) luaM_realloc(L, (b), 0)
|
||||
#define luaM_malloc(L, t) luaM_realloc(L, NULL, (t))
|
||||
#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t)))
|
||||
#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*sizeof(t)))
|
||||
#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*(lint32)sizeof(t)))
|
||||
|
||||
#define luaM_growvector(L, v,nelems,inc,t,e,l) \
|
||||
((v)=(t *)luaM_growaux(L, v,nelems,inc,sizeof(t),e,l))
|
||||
#define luaM_reallocvector(L, v,n,t) ((v)=(t *)luaM_realloc(L, v,(n)*sizeof(t)))
|
||||
|
||||
#define luaM_reallocvector(L, v,n,t) \
|
||||
((v)=(t *)luaM_realloc(L, v,(n)*(lint32)sizeof(t)))
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef LUA_DEBUG
|
||||
extern unsigned long memdebug_numblocks;
|
||||
extern unsigned long memdebug_total;
|
||||
extern unsigned long memdebug_maxmem;
|
||||
extern unsigned long memdebug_memlimit;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -1,110 +1,125 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 1.35 2000/03/29 20:19:20 roberto Exp roberto $
|
||||
** $Id: lobject.c,v 1.54 2000/10/10 19:53:20 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
const char *const luaO_typenames[] = { /* ORDER LUA_T */
|
||||
"userdata", "number", "string", "table", "function", "function", "nil",
|
||||
"function", "function", "line"
|
||||
|
||||
|
||||
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
|
||||
|
||||
|
||||
const char *const luaO_typenames[] = {
|
||||
"userdata", "nil", "number", "string", "table", "function"
|
||||
};
|
||||
|
||||
|
||||
const TObject luaO_nilobject = {TAG_NIL, {NULL}};
|
||||
|
||||
|
||||
/*
|
||||
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
||||
*/
|
||||
unsigned long luaO_power2 (unsigned long n) {
|
||||
unsigned long p = MINPOWER2;
|
||||
lint32 luaO_power2 (lint32 n) {
|
||||
lint32 p = MINPOWER2;
|
||||
while (p<=n) p<<=1;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
int luaO_equalval (const TObject *t1, const TObject *t2) {
|
||||
int luaO_equalObj (const TObject *t1, const TObject *t2) {
|
||||
if (ttype(t1) != ttype(t2)) return 0;
|
||||
switch (ttype(t1)) {
|
||||
case TAG_NUMBER:
|
||||
case LUA_TNUMBER:
|
||||
return nvalue(t1) == nvalue(t2);
|
||||
case TAG_STRING: case TAG_USERDATA:
|
||||
return svalue(t1) == svalue(t2);
|
||||
case TAG_TABLE:
|
||||
return avalue(t1) == avalue(t2);
|
||||
case TAG_CCLOSURE: case TAG_LCLOSURE:
|
||||
case LUA_TSTRING: case LUA_TUSERDATA:
|
||||
return tsvalue(t1) == tsvalue(t2);
|
||||
case LUA_TTABLE:
|
||||
return hvalue(t1) == hvalue(t2);
|
||||
case LUA_TFUNCTION:
|
||||
return clvalue(t1) == clvalue(t2);
|
||||
default:
|
||||
LUA_ASSERT(L, ttype(t1) == TAG_NIL, "invalid type");
|
||||
return 1; /* TAG_NIL */
|
||||
LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type");
|
||||
return 1; /* LUA_TNIL */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static double expten (unsigned int e) {
|
||||
double exp = 10.0;
|
||||
double res = 1.0;
|
||||
for (; e; e>>=1) {
|
||||
if (e & 1) res *= exp;
|
||||
exp *= exp;
|
||||
char *luaO_openspace (lua_State *L, size_t n) {
|
||||
if (n > L->Mbuffsize) {
|
||||
luaM_reallocvector(L, L->Mbuffer, n, char);
|
||||
L->nblocks += (n - L->Mbuffsize)*sizeof(char);
|
||||
L->Mbuffsize = n;
|
||||
}
|
||||
return res;
|
||||
return L->Mbuffer;
|
||||
}
|
||||
|
||||
|
||||
int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
|
||||
double a = 0.0;
|
||||
int point = 0; /* number of decimal digits */
|
||||
int sig;
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
sig = 0;
|
||||
switch (*s) {
|
||||
case '-': sig = 1; /* go through */
|
||||
case '+': s++;
|
||||
}
|
||||
if (! (isdigit((unsigned char)*s) ||
|
||||
(*s == '.' && isdigit((unsigned char)*(s+1)))))
|
||||
return 0; /* not (at least one digit before or after the point) */
|
||||
while (isdigit((unsigned char)*s))
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
if (*s == '.') {
|
||||
s++;
|
||||
while (isdigit((unsigned char)*s)) {
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
point++;
|
||||
}
|
||||
}
|
||||
if (sig) a = -a;
|
||||
if (*s == 'e' || *s == 'E') {
|
||||
int e = 0;
|
||||
s++;
|
||||
sig = 0;
|
||||
switch (*s) {
|
||||
case '-': sig = 1; /* go through */
|
||||
case '+': s++;
|
||||
}
|
||||
if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
|
||||
do {
|
||||
e = 10*e + (*(s++)-'0');
|
||||
} while (isdigit((unsigned char)*s));
|
||||
if (sig) e = -e;
|
||||
point -= e;
|
||||
}
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
if (*s != '\0') return 0; /* invalid trailing characters? */
|
||||
if (point != 0) {
|
||||
if (point > 0) a /= expten(point);
|
||||
else a *= expten(-point);
|
||||
}
|
||||
*result = a;
|
||||
char *endptr;
|
||||
Number res = lua_str2number(s, &endptr);
|
||||
if (endptr == s) return 0; /* no conversion */
|
||||
while (isspace((unsigned char)*endptr)) endptr++;
|
||||
if (*endptr != '\0') return 0; /* invalid trailing characters? */
|
||||
*result = res;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* maximum length of a string format for `luaO_verror' */
|
||||
#define MAX_VERROR 280
|
||||
|
||||
/* this function needs to handle only '%d' and '%.XXs' formats */
|
||||
void luaO_verror (lua_State *L, const char *fmt, ...) {
|
||||
va_list argp;
|
||||
char buff[MAX_VERROR]; /* to hold formatted message */
|
||||
va_start(argp, fmt);
|
||||
vsprintf(buff, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_error(L, buff);
|
||||
}
|
||||
|
||||
|
||||
void luaO_chunkid (char *out, const char *source, int bufflen) {
|
||||
if (*source == '=') {
|
||||
strncpy(out, source+1, bufflen); /* remove first char */
|
||||
out[bufflen-1] = '\0'; /* ensures null termination */
|
||||
}
|
||||
else {
|
||||
if (*source == '@') {
|
||||
int l;
|
||||
source++; /* skip the `@' */
|
||||
bufflen -= sizeof("file `...%s'");
|
||||
l = strlen(source);
|
||||
if (l>bufflen) {
|
||||
source += (l-bufflen); /* get last part of file name */
|
||||
sprintf(out, "file `...%.99s'", source);
|
||||
}
|
||||
else
|
||||
sprintf(out, "file `%.99s'", source);
|
||||
}
|
||||
else {
|
||||
int len = strcspn(source, "\n"); /* stop at first newline */
|
||||
bufflen -= sizeof("string \"%.*s...\"");
|
||||
if (len > bufflen) len = bufflen;
|
||||
if (source[len] != '\0') { /* must truncate? */
|
||||
strcpy(out, "string \"");
|
||||
out += strlen(out);
|
||||
strncpy(out, source, len);
|
||||
strcpy(out+len, "...\"");
|
||||
}
|
||||
else
|
||||
sprintf(out, "string \"%.99s\"", source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 1.59 2000/03/31 16:28:45 roberto Exp roberto $
|
||||
** $Id: lobject.h,v 1.81 2000/10/30 16:29:59 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,18 +12,18 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef LUA_DEBUG
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
#define LUA_INTERNALERROR(L,s) assert(((void)s,0))
|
||||
#define LUA_ASSERT(L,c,s) assert(((void)s,(c)))
|
||||
#define LUA_INTERNALERROR(s) assert(((void)s,0))
|
||||
#define LUA_ASSERT(c,s) assert(((void)s,(c)))
|
||||
#else
|
||||
#define LUA_INTERNALERROR(L,s) /* empty */
|
||||
#define LUA_ASSERT(L,c,s) /* empty */
|
||||
#define LUA_INTERNALERROR(s) /* empty */
|
||||
#define LUA_ASSERT(c,s) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef LUA_DEBUG
|
||||
/* to avoid warnings, and make sure value is really unused */
|
||||
#define UNUSED(x) (x=0, (void)(x))
|
||||
#else
|
||||
@@ -31,86 +31,69 @@
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Lua TYPES
|
||||
** WARNING: if you change the order of this enumeration,
|
||||
** grep "ORDER LUA_T"
|
||||
*/
|
||||
typedef enum {
|
||||
TAG_USERDATA = 0, /* default tag for userdata */
|
||||
TAG_NUMBER, /* fixed tag for numbers */
|
||||
TAG_STRING, /* fixed tag for strings */
|
||||
TAG_TABLE, /* default tag for tables */
|
||||
TAG_LCLOSURE, /* fixed tag for Lua closures */
|
||||
TAG_CCLOSURE, /* fixed tag for C closures */
|
||||
TAG_NIL, /* last "pre-defined" tag */
|
||||
/* mark for closures active in the stack */
|
||||
#define LUA_TMARK 6
|
||||
|
||||
TAG_LMARK, /* mark for Lua closures */
|
||||
TAG_CMARK, /* mark for C closures */
|
||||
|
||||
TAG_LINE
|
||||
} lua_Type;
|
||||
|
||||
/* tags for values visible from Lua == first user-created tag */
|
||||
#define NUM_TAGS 7
|
||||
#define NUM_TAGS 6
|
||||
|
||||
|
||||
/*
|
||||
** check whether `t' is a mark
|
||||
*/
|
||||
#define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK)
|
||||
/* check whether `t' is a mark */
|
||||
#define is_T_MARK(t) ((t) == LUA_TMARK)
|
||||
|
||||
|
||||
typedef union {
|
||||
struct TString *ts; /* TAG_STRING, TAG_USERDATA */
|
||||
struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_[CL]MARK */
|
||||
struct Hash *a; /* TAG_TABLE */
|
||||
Number n; /* TAG_NUMBER */
|
||||
int i; /* TAG_LINE */
|
||||
struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */
|
||||
struct Closure *cl; /* LUA_TFUNCTION */
|
||||
struct Hash *a; /* LUA_TTABLE */
|
||||
struct CallInfo *i; /* LUA_TLMARK */
|
||||
Number n; /* LUA_TNUMBER */
|
||||
} Value;
|
||||
|
||||
|
||||
/* Macros to access values */
|
||||
#define ttype(o) ((o)->ttype)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define svalue(o) ((o)->value.ts->str)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define clvalue(o) ((o)->value.cl)
|
||||
#define avalue(o) ((o)->value.a)
|
||||
#define hvalue(o) ((o)->value.a)
|
||||
#define infovalue(o) ((o)->value.i)
|
||||
#define svalue(o) (tsvalue(o)->str)
|
||||
|
||||
|
||||
typedef struct TObject {
|
||||
lua_Type ttype;
|
||||
typedef struct lua_TObject {
|
||||
int ttype;
|
||||
Value value;
|
||||
} TObject;
|
||||
|
||||
|
||||
typedef struct GlobalVar {
|
||||
TObject value;
|
||||
struct GlobalVar *next;
|
||||
struct TString *name;
|
||||
} GlobalVar;
|
||||
|
||||
|
||||
/*
|
||||
** String headers for string table
|
||||
*/
|
||||
|
||||
/*
|
||||
** most `malloc' libraries allocate memory in blocks of 8 bytes. TSPACK
|
||||
** tries to make sizeof(TString) a multiple of this granularity, to reduce
|
||||
** waste of space.
|
||||
*/
|
||||
#define TSPACK ((int)sizeof(int))
|
||||
|
||||
typedef struct TString {
|
||||
union {
|
||||
struct { /* for strings */
|
||||
GlobalVar *gv; /* eventual global value with this name */
|
||||
long len;
|
||||
unsigned long hash;
|
||||
int constindex; /* hint to reuse constants */
|
||||
} s;
|
||||
struct { /* for userdata */
|
||||
int tag;
|
||||
void *value;
|
||||
} d;
|
||||
} u;
|
||||
size_t len;
|
||||
struct TString *nexthash; /* chain for hash table */
|
||||
unsigned long hash;
|
||||
int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
|
||||
unsigned char marked;
|
||||
char str[1]; /* variable length string!! must be the last field! */
|
||||
int marked;
|
||||
char str[TSPACK]; /* variable length string!! must be the last field! */
|
||||
} TString;
|
||||
|
||||
|
||||
@@ -118,27 +101,33 @@ typedef struct TString {
|
||||
** Function Prototypes
|
||||
*/
|
||||
typedef struct Proto {
|
||||
struct Proto *next;
|
||||
int marked;
|
||||
struct TString **kstr; /* strings used by the function */
|
||||
int nkstr; /* size of `kstr' */
|
||||
Number *knum; /* Number numbers used by the function */
|
||||
int nknum; /* size of `knum' */
|
||||
struct TString **kstr; /* strings used by the function */
|
||||
int nkstr; /* size of `kstr' */
|
||||
struct Proto **kproto; /* functions defined inside the function */
|
||||
int nkproto; /* size of `kproto' */
|
||||
Instruction *code; /* ends with opcode ENDCODE */
|
||||
Instruction *code;
|
||||
int ncode; /* size of `code'; when 0 means an incomplete `Proto' */
|
||||
short numparams;
|
||||
short is_vararg;
|
||||
short maxstacksize;
|
||||
short marked;
|
||||
struct Proto *next;
|
||||
/* debug information */
|
||||
int *lineinfo; /* map from opcodes to source lines */
|
||||
int nlineinfo; /* size of `lineinfo' */
|
||||
int nlocvars;
|
||||
struct LocVar *locvars; /* information about local variables */
|
||||
int lineDefined;
|
||||
TString *source;
|
||||
int numparams;
|
||||
int is_vararg;
|
||||
int maxstacksize;
|
||||
struct LocVar *locvars; /* ends with line = -1 */
|
||||
} Proto;
|
||||
|
||||
|
||||
typedef struct LocVar {
|
||||
TString *varname; /* NULL signals end of scope */
|
||||
int line;
|
||||
TString *varname;
|
||||
int startpc; /* first point where variable is active */
|
||||
int endpc; /* first point where variable is dead */
|
||||
} LocVar;
|
||||
|
||||
|
||||
@@ -146,17 +135,21 @@ typedef struct LocVar {
|
||||
** Closures
|
||||
*/
|
||||
typedef struct Closure {
|
||||
struct Closure *next;
|
||||
int marked;
|
||||
union {
|
||||
lua_CFunction c; /* C functions */
|
||||
struct Proto *l; /* Lua functions */
|
||||
} f;
|
||||
int nelems;
|
||||
TObject consts[1];
|
||||
struct Closure *next;
|
||||
struct Closure *mark; /* marked closures (point to itself when not marked) */
|
||||
short isC; /* 0 for Lua functions, 1 for C functions */
|
||||
short nupvalues;
|
||||
TObject upvalue[1];
|
||||
} Closure;
|
||||
|
||||
|
||||
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
|
||||
|
||||
|
||||
typedef struct Node {
|
||||
TObject key;
|
||||
TObject val;
|
||||
@@ -164,26 +157,48 @@ typedef struct Node {
|
||||
} Node;
|
||||
|
||||
typedef struct Hash {
|
||||
int htag;
|
||||
Node *node;
|
||||
int htag;
|
||||
int size;
|
||||
Node *firstfree; /* this position is free; all positions after it are full */
|
||||
struct Hash *next;
|
||||
int marked;
|
||||
struct Hash *mark; /* marked tables (point to itself when not marked) */
|
||||
} Hash;
|
||||
|
||||
|
||||
extern const char *const luaO_typenames[];
|
||||
/* unmarked tables and closures are represented by pointing `mark' to
|
||||
** themselves
|
||||
*/
|
||||
#define ismarked(x) ((x)->mark != (x))
|
||||
|
||||
|
||||
/*
|
||||
** informations about a call (for debugging)
|
||||
*/
|
||||
typedef struct CallInfo {
|
||||
struct Closure *func; /* function being called */
|
||||
const Instruction **pc; /* current pc of called function */
|
||||
int lastpc; /* last pc traced */
|
||||
int line; /* current line */
|
||||
int refi; /* current index in `lineinfo' */
|
||||
} CallInfo;
|
||||
|
||||
|
||||
extern const TObject luaO_nilobject;
|
||||
extern const char *const luaO_typenames[];
|
||||
|
||||
#define luaO_typename(o) luaO_typenames[ttype(o)]
|
||||
|
||||
unsigned long luaO_power2 (unsigned long n);
|
||||
#define luaO_typename(o) (luaO_typenames[ttype(o)])
|
||||
|
||||
#define luaO_equalObj(t1,t2) (ttype(t1) == ttype(t2) && luaO_equalval(t1,t2))
|
||||
int luaO_equalval (const TObject *t1, const TObject *t2);
|
||||
int luaO_redimension (lua_State *L, int oldsize);
|
||||
|
||||
lint32 luaO_power2 (lint32 n);
|
||||
char *luaO_openspace (lua_State *L, size_t n);
|
||||
|
||||
int luaO_equalObj (const TObject *t1, const TObject *t2);
|
||||
int luaO_str2d (const char *s, Number *result);
|
||||
|
||||
void luaO_verror (lua_State *L, const char *fmt, ...);
|
||||
void luaO_chunkid (char *out, const char *source, int len);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
+19
-8
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.58 2000/04/13 16:51:01 roberto Exp roberto $
|
||||
** $Id: lopcodes.h,v 1.67 2000/08/29 14:48:16 roberto Exp roberto $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -30,7 +30,7 @@
|
||||
The size of each argument is defined in `llimits.h'. The usual is an
|
||||
instruction with 32 bits, U arguments with 26 bits (32-6), B arguments
|
||||
with 9 bits, and A arguments with 17 bits (32-6-9). For small
|
||||
instalations, the instruction size can be 16, so U has 10 bits,
|
||||
installations, the instruction size can be 16, so U has 10 bits,
|
||||
and A and B have 5 bits each.
|
||||
===========================================================================*/
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
#define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
|
||||
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
|
||||
|
||||
#define CREATE_U(o,u) ((Instruction)(o) | (Instruction)(u)<<POS_U)
|
||||
#define CREATE_U(o,u) ((Instruction)(o) | ((Instruction)(u)<<POS_U))
|
||||
#define GETARG_U(i) ((int)((i)>>POS_U))
|
||||
#define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
|
||||
((Instruction)(u)<<POS_U)))
|
||||
@@ -91,7 +91,7 @@ OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
|
||||
OP_PUSHNIL,/* U - nil_1-nil_u */
|
||||
OP_POP,/* U a_u-a_1 - */
|
||||
|
||||
OP_PUSHINT,/* S - (Number)s */
|
||||
OP_PUSHINT,/* S - (Number)s */
|
||||
OP_PUSHSTRING,/* K - KSTR[k] */
|
||||
OP_PUSHNUM,/* N - KNUM[n] */
|
||||
OP_PUSHNEGNUM,/* N - -KNUM[n] */
|
||||
@@ -112,8 +112,8 @@ OP_SETLOCAL,/* L x - LOC[l]=x */
|
||||
OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
|
||||
OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */
|
||||
|
||||
OP_SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */
|
||||
OP_SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */
|
||||
OP_SETLIST,/* A B v_b-v_1 t t t[i+a*FPF]=v_i */
|
||||
OP_SETMAP,/* U v_u k_u - v_1 k_1 t t t[k_i]=v_i */
|
||||
|
||||
OP_ADD,/* - y x x+y */
|
||||
OP_ADDI,/* S x x+s */
|
||||
@@ -143,15 +143,26 @@ OP_PUSHNILJMP,/* - - nil PC++; */
|
||||
OP_FORPREP,/* J */
|
||||
OP_FORLOOP,/* J */
|
||||
|
||||
OP_CLOSURE,/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
|
||||
OP_LFORPREP,/* J */
|
||||
OP_LFORLOOP,/* J */
|
||||
|
||||
OP_SETLINE/* U - - LINE=u */
|
||||
OP_CLOSURE/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
|
||||
|
||||
} OpCode;
|
||||
|
||||
#define NUM_OPCODES ((int)OP_CLOSURE+1)
|
||||
|
||||
|
||||
#define ISJUMP(o) (OP_JMPNE <= (o) && (o) <= OP_JMP)
|
||||
|
||||
|
||||
|
||||
/* special code to fit a LUA_MULTRET inside an argB */
|
||||
#define MULT_RET 255 /* (<=MAXARG_B) */
|
||||
#if MULT_RET>MAXARG_B
|
||||
#undef MULT_RET
|
||||
#define MULT_RET MAXARG_B
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.15 2000/04/05 17:51:58 roberto Exp roberto $
|
||||
** $Id: lparser.h,v 1.25 2000/09/29 12:42:13 roberto Exp roberto $
|
||||
** LL(1) Parser and code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -43,15 +43,14 @@ typedef struct FuncState {
|
||||
struct lua_State *L; /* copy of the Lua state */
|
||||
int pc; /* next position to code */
|
||||
int lasttarget; /* `pc' of last `jump target' */
|
||||
int jlt; /* list of jumps to `lasttarged' */
|
||||
int stacklevel; /* number of values on activation register */
|
||||
int nlocalvar; /* number of active local variables */
|
||||
int nupvalues; /* number of upvalues */
|
||||
int nvars; /* number of entries in f->locvars (-1 if no debug information) */
|
||||
int lastsetline; /* line where last SETLINE was issued */
|
||||
int jlt; /* list of jumps to `lasttarget' */
|
||||
short stacklevel; /* number of values on activation register */
|
||||
short nactloc; /* number of active local variables */
|
||||
short nupvalues; /* number of upvalues */
|
||||
int lastline; /* line where last `lineinfo' was generated */
|
||||
struct Breaklabel *bl; /* chain of breakable blocks */
|
||||
expdesc upvalues[MAXUPVALUES]; /* upvalues */
|
||||
TString *localvar[MAXLOCALS]; /* store local variable names */
|
||||
int actloc[MAXLOCALS]; /* local-variable stack (indices to locvars) */
|
||||
} FuncState;
|
||||
|
||||
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
** $Id: lref.c,v 1.10 2000/03/27 20:10:21 roberto Exp roberto $
|
||||
** reference mechanism
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lmem.h"
|
||||
#include "lref.h"
|
||||
#include "lstate.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
int lua_ref (lua_State *L, int lock) {
|
||||
int ref;
|
||||
luaA_checkCargs(L, 1);
|
||||
if (ttype(L->top-1) == TAG_NIL)
|
||||
ref = LUA_REFNIL;
|
||||
else {
|
||||
if (L->refFree != NONEXT) { /* is there a free place? */
|
||||
ref = L->refFree;
|
||||
L->refFree = L->refArray[ref].st;
|
||||
}
|
||||
else { /* no more free places */
|
||||
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref, refEM, MAX_INT);
|
||||
ref = L->refSize++;
|
||||
}
|
||||
L->refArray[ref].o = *(L->top-1);
|
||||
L->refArray[ref].st = lock ? LOCK : HOLD;
|
||||
}
|
||||
L->top--;
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
void lua_unref (lua_State *L, int ref) {
|
||||
if (ref >= 0) {
|
||||
if (ref >= L->refSize || L->refArray[ref].st >= 0)
|
||||
lua_error(L, "Lua API error - "
|
||||
"invalid argument for function `lua_unref'");
|
||||
L->refArray[ref].st = L->refFree;
|
||||
L->refFree = ref;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_getref (lua_State *L, int ref) {
|
||||
if (ref == LUA_REFNIL)
|
||||
return luaA_putluaObject(L, &luaO_nilobject);
|
||||
else if (0 <= ref && ref < L->refSize &&
|
||||
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
|
||||
return luaA_putluaObject(L, &L->refArray[ref].o);
|
||||
else
|
||||
return LUA_NOOBJECT;
|
||||
}
|
||||
|
||||
|
||||
void lua_beginblock (lua_State *L) {
|
||||
luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
|
||||
"too many nested blocks", L->stacksize);
|
||||
L->Cblocks[L->numCblocks] = L->Cstack;
|
||||
L->numCblocks++;
|
||||
}
|
||||
|
||||
|
||||
void lua_endblock (lua_State *L) {
|
||||
if (L->numCblocks <= 0)
|
||||
lua_error(L, "Lua API error - no block to end");
|
||||
--L->numCblocks;
|
||||
L->Cstack = L->Cblocks[L->numCblocks];
|
||||
L->top = L->Cstack.base;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static int ismarked (const TObject *o) {
|
||||
/* valid only for locked objects */
|
||||
switch (o->ttype) {
|
||||
case TAG_STRING: case TAG_USERDATA:
|
||||
return o->value.ts->marked;
|
||||
case TAG_TABLE:
|
||||
return o->value.a->marked;
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
return o->value.cl->marked;
|
||||
default: /* number */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* for internal debugging only; check if a link of free refs is valid */
|
||||
#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
|
||||
|
||||
void luaR_invalidaterefs (lua_State *L) {
|
||||
int n = L->refSize;
|
||||
int i;
|
||||
for (i=0; i<n; i++) {
|
||||
struct Ref *r = &L->refArray[i];
|
||||
if (r->st == HOLD && !ismarked(&r->o))
|
||||
r->st = COLLECTED;
|
||||
LUA_ASSERT(L, (r->st == LOCK && ismarked(&r->o)) ||
|
||||
r->st == COLLECTED ||
|
||||
r->st == NONEXT ||
|
||||
(r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
|
||||
"inconsistent ref table");
|
||||
}
|
||||
LUA_ASSERT(L, VALIDLINK(L, L->refFree, n), "inconsistent ref table");
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
** $Id: lref.h,v 1.5 1999/12/27 17:33:22 roberto Exp roberto $
|
||||
** reference mechanism
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lref_h
|
||||
#define lref_h
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define NONEXT -1 /* to end the free list */
|
||||
#define HOLD -2
|
||||
#define COLLECTED -3
|
||||
#define LOCK -4
|
||||
|
||||
|
||||
struct Ref {
|
||||
TObject o;
|
||||
int st; /* can be LOCK, HOLD, COLLECTED, or next (for free list) */
|
||||
};
|
||||
|
||||
|
||||
void luaR_invalidaterefs (lua_State *L);
|
||||
|
||||
#endif
|
||||
@@ -1,108 +1,121 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 1.24 2000/01/13 16:30:47 roberto Exp roberto $
|
||||
** $Id: lstate.c,v 1.47 2000/10/26 12:47:05 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lbuiltin.h"
|
||||
#include "ldo.h"
|
||||
#include "lgc.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lref.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
lua_State *lua_state = NULL;
|
||||
#ifdef LUA_DEBUG
|
||||
static lua_State *lua_state = NULL;
|
||||
void luaB_opentests (lua_State *L);
|
||||
#endif
|
||||
|
||||
|
||||
static lua_State *newstate_aux (int stacksize, int put_builtin) {
|
||||
lua_State *L = luaM_new(NULL, lua_State);
|
||||
L->errorJmp = NULL;
|
||||
L->Mbuffer = NULL;
|
||||
L->Mbuffbase = 0;
|
||||
L->Mbuffsize = 0;
|
||||
L->Mbuffnext = 0;
|
||||
L->Cblocks = NULL;
|
||||
L->numCblocks = 0;
|
||||
L->rootproto = NULL;
|
||||
L->rootcl = NULL;
|
||||
L->rootglobal = NULL;
|
||||
L->roottable = NULL;
|
||||
L->IMtable = NULL;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
L->refFree = NONEXT;
|
||||
L->nblocks = 0;
|
||||
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
||||
L->debug = 0;
|
||||
L->callhook = NULL;
|
||||
L->linehook = NULL;
|
||||
L->allowhooks = 1;
|
||||
/*
|
||||
** built-in implementation for ERRORMESSAGE. In a "correct" environment
|
||||
** ERRORMESSAGE should have an external definition, and so this function
|
||||
** would not be used.
|
||||
*/
|
||||
static int errormessage (lua_State *L) {
|
||||
const char *s = lua_tostring(L, 1);
|
||||
if (s == NULL) s = "(no message)";
|
||||
fprintf(stderr, "error: %s\n", s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** open parts that may cause memory-allocation errors
|
||||
*/
|
||||
static void f_luaopen (lua_State *L, void *ud) {
|
||||
int stacksize = *(int *)ud;
|
||||
if (stacksize == 0)
|
||||
stacksize = DEFAULT_STACK_SIZE;
|
||||
else
|
||||
stacksize += LUA_MINSTACK;
|
||||
L->gt = luaH_new(L, 10); /* table of globals */
|
||||
luaD_init(L, stacksize);
|
||||
luaS_init(L);
|
||||
luaX_init(L);
|
||||
luaT_init(L);
|
||||
if (put_builtin)
|
||||
luaB_predefine(L);
|
||||
L->GCthreshold = L->nblocks*4;
|
||||
lua_newtable(L);
|
||||
lua_ref(L, 1); /* create registry */
|
||||
lua_register(L, LUA_ERRORMESSAGE, errormessage);
|
||||
#ifdef LUA_DEBUG
|
||||
luaB_opentests(L);
|
||||
if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
|
||||
#endif
|
||||
LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack");
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_State *lua_open (int stacksize) {
|
||||
lua_State *L = luaM_new(NULL, lua_State);
|
||||
if (L == NULL) return NULL; /* memory allocation error */
|
||||
L->stack = NULL;
|
||||
L->strt.size = L->udt.size = 0;
|
||||
L->strt.nuse = L->udt.nuse = 0;
|
||||
L->strt.hash = NULL;
|
||||
L->udt.hash = NULL;
|
||||
L->Mbuffer = NULL;
|
||||
L->Mbuffsize = 0;
|
||||
L->rootproto = NULL;
|
||||
L->rootcl = NULL;
|
||||
L->roottable = NULL;
|
||||
L->TMtable = NULL;
|
||||
L->last_tag = -1;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
L->refFree = NONEXT;
|
||||
L->nblocks = sizeof(lua_State);
|
||||
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
||||
L->callhook = NULL;
|
||||
L->linehook = NULL;
|
||||
L->allowhooks = 1;
|
||||
L->errorJmp = NULL;
|
||||
if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
|
||||
/* memory allocation error: free partial state */
|
||||
lua_close(L);
|
||||
return NULL;
|
||||
}
|
||||
L->GCthreshold = 2*L->nblocks;
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
lua_State *lua_newstate (const char *s, ...) {
|
||||
static const char *const ops[] = {"stack", "builtin", NULL};
|
||||
va_list ap;
|
||||
int stacksize = DEFAULT_STACK_SIZE;
|
||||
int put_builtin = 1;
|
||||
va_start(ap, s);
|
||||
while (s) {
|
||||
switch (luaL_findstring(s, ops)) {
|
||||
case 0: /* stack */
|
||||
stacksize = va_arg(ap, int);
|
||||
break;
|
||||
case 1: /* builtin */
|
||||
put_builtin = va_arg(ap, int);
|
||||
break;
|
||||
default: /* invalid argument */
|
||||
va_end(ap);
|
||||
return NULL;
|
||||
}
|
||||
s = va_arg(ap, const char *);
|
||||
}
|
||||
va_end(ap);
|
||||
return newstate_aux(stacksize, put_builtin);
|
||||
}
|
||||
|
||||
|
||||
void lua_close (lua_State *L) {
|
||||
LUA_API void lua_close (lua_State *L) {
|
||||
LUA_ASSERT(L != lua_state || lua_gettop(L) == 0, "garbage in C stack");
|
||||
luaC_collect(L, 1); /* collect all elements */
|
||||
LUA_ASSERT(L, L->rootproto == NULL, "list should be empty");
|
||||
LUA_ASSERT(L, L->rootcl == NULL, "list should be empty");
|
||||
LUA_ASSERT(L, L->rootglobal == NULL, "list should be empty");
|
||||
LUA_ASSERT(L, L->roottable == NULL, "list should be empty");
|
||||
LUA_ASSERT(L->rootproto == NULL, "list should be empty");
|
||||
LUA_ASSERT(L->rootcl == NULL, "list should be empty");
|
||||
LUA_ASSERT(L->roottable == NULL, "list should be empty");
|
||||
luaS_freeall(L);
|
||||
if (L->stack)
|
||||
L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
|
||||
luaM_free(L, L->stack);
|
||||
luaM_free(L, L->IMtable);
|
||||
L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
|
||||
luaM_free(L, L->TMtable);
|
||||
L->nblocks -= (L->refSize)*sizeof(struct Ref);
|
||||
luaM_free(L, L->refArray);
|
||||
L->nblocks -= (L->Mbuffsize)*sizeof(char);
|
||||
luaM_free(L, L->Mbuffer);
|
||||
luaM_free(L, L->Cblocks);
|
||||
LUA_ASSERT(L, L->numCblocks == 0, "Cblocks still open");
|
||||
LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks");
|
||||
LUA_ASSERT(L, L->Cstack.base == L->top, "C2Lua not empty");
|
||||
LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks");
|
||||
luaM_free(L, L);
|
||||
if (L == lua_state) {
|
||||
LUA_ASSERT(L, memdebug_numblocks == 0, "memory leak!");
|
||||
LUA_ASSERT(L, memdebug_total == 0,"memory leak!");
|
||||
lua_state = NULL;
|
||||
}
|
||||
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
|
||||
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 1.30 2000/03/10 18:37:44 roberto Exp roberto $
|
||||
** $Id: lstate.h,v 1.40 2000/09/29 12:42:13 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,8 +7,6 @@
|
||||
#ifndef lstate_h
|
||||
#define lstate_h
|
||||
|
||||
#include <setjmp.h>
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
@@ -19,30 +17,27 @@ typedef TObject *StkId; /* index to stack elements */
|
||||
|
||||
|
||||
/*
|
||||
** `jmp_buf' may be an array, so it is better to make sure it has an
|
||||
** address (and not that it *is* an address...)
|
||||
** marks for Reference array
|
||||
*/
|
||||
struct lua_longjmp {
|
||||
jmp_buf b;
|
||||
#define NONEXT -1 /* to end the free list */
|
||||
#define HOLD -2
|
||||
#define COLLECTED -3
|
||||
#define LOCK -4
|
||||
|
||||
|
||||
struct Ref {
|
||||
TObject o;
|
||||
int st; /* can be LOCK, HOLD, COLLECTED, or next (for free list) */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** stack layout for C point of view:
|
||||
** [lua2C, lua2C+num) - `array' lua2C
|
||||
** [lua2C+num, base) - space for extra lua_Objects (limbo)
|
||||
** [base, L->top) - `stack' C2Lua
|
||||
*/
|
||||
struct C_Lua_Stack {
|
||||
StkId base;
|
||||
StkId lua2C;
|
||||
int num;
|
||||
};
|
||||
struct lua_longjmp; /* defined in ldo.c */
|
||||
struct TM; /* defined in ltm.h */
|
||||
|
||||
|
||||
typedef struct stringtable {
|
||||
int size;
|
||||
int nuse; /* number of elements (including EMPTYs) */
|
||||
lint32 nuse; /* number of elements */
|
||||
TString **hash;
|
||||
} stringtable;
|
||||
|
||||
@@ -54,36 +49,29 @@ struct lua_State {
|
||||
StkId stack; /* stack base */
|
||||
StkId stack_last; /* last free slot in the stack */
|
||||
int stacksize;
|
||||
struct C_Lua_Stack Cstack; /* C2lua struct */
|
||||
StkId Cbase; /* base for current C function */
|
||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||
char *Mbuffer; /* global buffer */
|
||||
int Mbuffbase; /* current first position of Mbuffer */
|
||||
int Mbuffsize; /* size of Mbuffer */
|
||||
int Mbuffnext; /* next position to fill in Mbuffer */
|
||||
struct C_Lua_Stack *Cblocks;
|
||||
int numCblocks; /* number of nested Cblocks */
|
||||
size_t Mbuffsize; /* size of Mbuffer */
|
||||
/* global state */
|
||||
Proto *rootproto; /* list of all prototypes */
|
||||
Closure *rootcl; /* list of all closures */
|
||||
Hash *roottable; /* list of all tables */
|
||||
GlobalVar *rootglobal; /* list of global variables */
|
||||
stringtable *string_root; /* array of hash tables for strings and udata */
|
||||
struct IM *IMtable; /* table for tag methods */
|
||||
int last_tag; /* last used tag in IMtable */
|
||||
stringtable strt; /* hash table for strings */
|
||||
stringtable udt; /* hash table for udata */
|
||||
Hash *gt; /* table for globals */
|
||||
struct TM *TMtable; /* table for tag methods */
|
||||
int last_tag; /* last used tag in TMtable */
|
||||
struct Ref *refArray; /* locked objects */
|
||||
int refSize; /* size of refArray */
|
||||
int refFree; /* list of free positions in refArray */
|
||||
unsigned long GCthreshold;
|
||||
unsigned long nblocks; /* number of `blocks' currently allocated */
|
||||
int debug;
|
||||
unsigned long nblocks; /* number of `bytes' currently allocated */
|
||||
lua_Hook callhook;
|
||||
lua_Hook linehook;
|
||||
int allowhooks;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 1.33 2000/03/10 14:38:10 roberto Exp roberto $
|
||||
** $Id: lstring.c,v 1.44 2000/10/26 12:47:05 roberto Exp roberto $
|
||||
** String table (keeps all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,47 +7,47 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
|
||||
#define gcsizestring(L, l) numblocks(L, 0, sizeof(TString)+l)
|
||||
#define gcsizeudata gcsizestring(L, 0)
|
||||
/*
|
||||
** type equivalent to TString, but with maximum alignment requirements
|
||||
*/
|
||||
union L_UTString {
|
||||
TString ts;
|
||||
union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
|
||||
};
|
||||
|
||||
|
||||
|
||||
void luaS_init (lua_State *L) {
|
||||
int i;
|
||||
L->string_root = luaM_newvector(L, NUM_HASHS, stringtable);
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
L->string_root[i].size = 1;
|
||||
L->string_root[i].nuse = 0;
|
||||
L->string_root[i].hash = luaM_newvector(L, 1, TString *);;
|
||||
L->string_root[i].hash[0] = NULL;
|
||||
}
|
||||
L->strt.hash = luaM_newvector(L, 1, TString *);
|
||||
L->udt.hash = luaM_newvector(L, 1, TString *);
|
||||
L->nblocks += 2*sizeof(TString *);
|
||||
L->strt.size = L->udt.size = 1;
|
||||
L->strt.nuse = L->udt.nuse = 0;
|
||||
L->strt.hash[0] = L->udt.hash[0] = NULL;
|
||||
}
|
||||
|
||||
|
||||
void luaS_freeall (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
LUA_ASSERT(L, L->string_root[i].nuse==0, "non-empty string table");
|
||||
luaM_free(L, L->string_root[i].hash);
|
||||
}
|
||||
luaM_free(L, L->string_root);
|
||||
LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
|
||||
L->nblocks -= (L->strt.size + L->udt.size)*sizeof(TString *);
|
||||
luaM_free(L, L->strt.hash);
|
||||
LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
|
||||
luaM_free(L, L->udt.hash);
|
||||
}
|
||||
|
||||
|
||||
static unsigned long hash_s (const char *s, long l) {
|
||||
static unsigned long hash_s (const char *s, size_t l) {
|
||||
unsigned long h = l; /* seed */
|
||||
long step = (l>>6)+1; /* if string is too long, don't hash all its chars */
|
||||
for (; l>0; l-=step)
|
||||
size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */
|
||||
for (; l>=step; l-=step)
|
||||
h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
|
||||
return h;
|
||||
}
|
||||
@@ -62,96 +62,82 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
|
||||
TString *p = tb->hash[i];
|
||||
while (p) { /* for each node in the list */
|
||||
TString *next = p->nexthash; /* save next */
|
||||
int h = p->hash&(newsize-1); /* new position */
|
||||
LUA_ASSERT(L, p->hash%newsize == (p->hash&(newsize-1)),
|
||||
unsigned long h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value);
|
||||
int h1 = h&(newsize-1); /* new position */
|
||||
LUA_ASSERT(h%newsize == (h&(newsize-1)),
|
||||
"a&(x-1) == a%x, for x power of 2");
|
||||
p->nexthash = newhash[h]; /* chain it in new position */
|
||||
newhash[h] = p;
|
||||
p->nexthash = newhash[h1]; /* chain it in new position */
|
||||
newhash[h1] = p;
|
||||
p = next;
|
||||
}
|
||||
}
|
||||
luaM_free(L, tb->hash);
|
||||
L->nblocks += (newsize - tb->size)*sizeof(TString *);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static TString *newone (lua_State *L, long l, unsigned long h) {
|
||||
TString *ts = (TString *)luaM_malloc(L,
|
||||
sizeof(TString)+l*sizeof(char));
|
||||
ts->marked = 0;
|
||||
ts->nexthash = NULL;
|
||||
ts->hash = h;
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static TString *newone_s (lua_State *L, const char *str,
|
||||
long l, unsigned long h) {
|
||||
TString *ts = newone(L, l, h);
|
||||
memcpy(ts->str, str, l);
|
||||
ts->str[l] = 0; /* ending 0 */
|
||||
ts->u.s.gv = NULL; /* no global value */
|
||||
ts->u.s.len = l;
|
||||
ts->constindex = 0;
|
||||
L->nblocks += gcsizestring(L, l);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static TString *newone_u (lua_State *L, void *buff,
|
||||
int tag, unsigned long h) {
|
||||
TString *ts = newone(L, 0, h);
|
||||
ts->u.d.value = buff;
|
||||
ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
|
||||
ts->constindex = -1; /* tag -> this is a userdata */
|
||||
L->nblocks += gcsizeudata;
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
|
||||
ts->nexthash = tb->hash[h]; /* chain new entry */
|
||||
tb->hash[h] = ts;
|
||||
tb->nuse++;
|
||||
if (tb->nuse > tb->size) /* too crowded? */
|
||||
if (tb->nuse > (lint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */
|
||||
luaS_resize(L, tb, tb->size*2);
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, long l) {
|
||||
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||
unsigned long h = hash_s(str, l);
|
||||
stringtable *tb = &L->string_root[(l==0) ? 0 :
|
||||
((unsigned int)(str[0]+str[l-1]))&(NUM_HASHSTR-1)];
|
||||
int h1 = h&(tb->size-1);
|
||||
int h1 = h & (L->strt.size-1);
|
||||
TString *ts;
|
||||
for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
|
||||
if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
|
||||
for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) {
|
||||
if (ts->len == l && (memcmp(str, ts->str, l) == 0))
|
||||
return ts;
|
||||
}
|
||||
/* not found */
|
||||
ts = newone_s(L, str, l, h); /* create new entry */
|
||||
newentry(L, tb, ts, h1); /* insert it on table */
|
||||
ts = (TString *)luaM_malloc(L, sizestring(l));
|
||||
ts->marked = 0;
|
||||
ts->nexthash = NULL;
|
||||
ts->len = l;
|
||||
ts->u.s.hash = h;
|
||||
ts->u.s.constindex = 0;
|
||||
memcpy(ts->str, str, l);
|
||||
ts->str[l] = 0; /* ending 0 */
|
||||
L->nblocks += sizestring(l);
|
||||
newentry(L, &L->strt, ts, h1); /* insert it on table */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
|
||||
union L_UTString *uts = (union L_UTString *)luaM_malloc(L,
|
||||
(lint32)sizeof(union L_UTString)+s);
|
||||
TString *ts = &uts->ts;
|
||||
ts->marked = 0;
|
||||
ts->nexthash = NULL;
|
||||
ts->len = s;
|
||||
ts->u.d.tag = 0;
|
||||
ts->u.d.value = (udata == NULL) ? uts+1 : udata;
|
||||
L->nblocks += sizestring(s);
|
||||
/* insert it on table */
|
||||
newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1));
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** uses '%' for one hashing with userdata because addresses are too regular,
|
||||
** so two '&' operations would be highly correlated
|
||||
*/
|
||||
TString *luaS_createudata (lua_State *L, void *udata, int tag) {
|
||||
unsigned long h = IntPoint(L, udata);
|
||||
stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
|
||||
int h1 = h&(tb->size-1);
|
||||
int h1 = IntPoint(udata) & (L->udt.size-1);
|
||||
TString *ts;
|
||||
for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
|
||||
for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
|
||||
if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
|
||||
return ts;
|
||||
}
|
||||
/* not found */
|
||||
ts = newone_u(L, udata, tag, h);
|
||||
newentry(L, tb, ts, h1);
|
||||
ts = luaS_newudata(L, 0, udata);
|
||||
if (tag != LUA_ANYTAG)
|
||||
ts->u.d.tag = tag;
|
||||
return ts;
|
||||
}
|
||||
|
||||
@@ -160,46 +146,10 @@ TString *luaS_new (lua_State *L, const char *str) {
|
||||
return luaS_newlstr(L, str, strlen(str));
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_newfixed (lua_State *L, const char *str) {
|
||||
TString *ts = luaS_new(L, str);
|
||||
if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
void luaS_free (lua_State *L, TString *t) {
|
||||
if (t->constindex == -1) /* is userdata? */
|
||||
L->nblocks -= gcsizeudata;
|
||||
else { /* is string */
|
||||
L->nblocks -= gcsizestring(L, t->u.s.len);
|
||||
luaM_free(L, t->u.s.gv);
|
||||
}
|
||||
luaM_free(L, t);
|
||||
}
|
||||
|
||||
|
||||
GlobalVar *luaS_assertglobal (lua_State *L, TString *ts) {
|
||||
GlobalVar *gv = ts->u.s.gv;
|
||||
if (!gv) { /* no global value yet? */
|
||||
gv = luaM_new(L, GlobalVar);
|
||||
gv->value.ttype = TAG_NIL; /* initial value */
|
||||
gv->name = ts;
|
||||
gv->next = L->rootglobal; /* chain in global list */
|
||||
L->rootglobal = gv;
|
||||
ts->u.s.gv = gv;
|
||||
}
|
||||
return gv;
|
||||
}
|
||||
|
||||
|
||||
GlobalVar *luaS_assertglobalbyname (lua_State *L, const char *name) {
|
||||
return luaS_assertglobal(L, luaS_new(L, name));
|
||||
}
|
||||
|
||||
|
||||
int luaS_globaldefined (lua_State *L, const char *name) {
|
||||
TString *ts = luaS_new(L, name);
|
||||
return ts->u.s.gv && ts->u.s.gv->value.ttype != TAG_NIL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.17 2000/03/10 14:38:10 roberto Exp roberto $
|
||||
** $Id: lstring.h,v 1.23 2000/10/26 12:47:05 roberto Exp roberto $
|
||||
** String table (keep all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,30 +12,26 @@
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
#define NUM_HASHSTR 32
|
||||
#define NUM_HASHUDATA 31
|
||||
#define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA)
|
||||
|
||||
|
||||
/*
|
||||
** any taggedstring with mark>=FIXMARK is never collected.
|
||||
** any TString with mark>=FIXMARK is never collected.
|
||||
** Marks>=RESERVEDMARK are used to identify reserved words.
|
||||
*/
|
||||
#define FIXMARK 2
|
||||
#define RESERVEDMARK 3
|
||||
|
||||
|
||||
#define sizestring(l) ((long)sizeof(TString) + \
|
||||
((long)(l+1)-TSPACK)*(long)sizeof(char))
|
||||
|
||||
|
||||
void luaS_init (lua_State *L);
|
||||
void luaS_resize (lua_State *L, stringtable *tb, int newsize);
|
||||
TString *luaS_newudata (lua_State *L, size_t s, void *udata);
|
||||
TString *luaS_createudata (lua_State *L, void *udata, int tag);
|
||||
void luaS_freeall (lua_State *L);
|
||||
void luaS_free (lua_State *L, TString *ts);
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, long l);
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||
TString *luaS_new (lua_State *L, const char *str);
|
||||
TString *luaS_newfixed (lua_State *L, const char *str);
|
||||
GlobalVar *luaS_assertglobal (lua_State *L, TString *ts);
|
||||
GlobalVar *luaS_assertglobalbyname (lua_State *L, const char *name);
|
||||
int luaS_globaldefined (lua_State *L, const char *name);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,111 +1,111 @@
|
||||
/*
|
||||
** $Id: lstrlib.c,v 1.40 2000/02/08 16:34:31 roberto Exp roberto $
|
||||
** $Id: lstrlib.c,v 1.55 2000/10/20 16:39:03 roberto Exp roberto $
|
||||
** Standard library for string operations and pattern-matching
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static void addnchar (lua_State *L, const char *s, int n) {
|
||||
char *b = luaL_openspace(L, n);
|
||||
memcpy(b, s, n);
|
||||
luaL_addsize(L, n);
|
||||
}
|
||||
|
||||
|
||||
static void str_len (lua_State *L) {
|
||||
long l;
|
||||
static int str_len (lua_State *L) {
|
||||
size_t l;
|
||||
luaL_check_lstr(L, 1, &l);
|
||||
lua_pushnumber(L, l);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void closeandpush (lua_State *L) {
|
||||
lua_pushlstring(L, luaL_buffer(L), luaL_getsize(L));
|
||||
}
|
||||
|
||||
|
||||
static long posrelat (long pos, long len) {
|
||||
static long posrelat (long pos, size_t len) {
|
||||
/* relative string position: negative means back from end */
|
||||
return (pos>=0) ? pos : len+pos+1;
|
||||
return (pos>=0) ? pos : (long)len+pos+1;
|
||||
}
|
||||
|
||||
|
||||
static void str_sub (lua_State *L) {
|
||||
long l;
|
||||
static int str_sub (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
long start = posrelat(luaL_check_long(L, 2), l);
|
||||
long end = posrelat(luaL_opt_long(L, 3, -1), l);
|
||||
if (start < 1) start = 1;
|
||||
if (end > l) end = l;
|
||||
if (end > (long)l) end = l;
|
||||
if (start <= end)
|
||||
lua_pushlstring(L, s+start-1, end-start+1);
|
||||
else lua_pushstring(L, "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void str_lower (lua_State *L) {
|
||||
long l;
|
||||
int i;
|
||||
static int str_lower (lua_State *L) {
|
||||
size_t l;
|
||||
size_t i;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
luaL_resetbuffer(L);
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=0; i<l; i++)
|
||||
luaL_addchar(L, tolower((unsigned char)(s[i])));
|
||||
closeandpush(L);
|
||||
luaL_putchar(&b, tolower((unsigned char)(s[i])));
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void str_upper (lua_State *L) {
|
||||
long l;
|
||||
int i;
|
||||
static int str_upper (lua_State *L) {
|
||||
size_t l;
|
||||
size_t i;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
luaL_resetbuffer(L);
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=0; i<l; i++)
|
||||
luaL_addchar(L, toupper((unsigned char)(s[i])));
|
||||
closeandpush(L);
|
||||
luaL_putchar(&b, toupper((unsigned char)(s[i])));
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void str_rep (lua_State *L) {
|
||||
long l;
|
||||
static int str_rep (lua_State *L) {
|
||||
size_t l;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
int n = luaL_check_int(L, 2);
|
||||
luaL_resetbuffer(L);
|
||||
luaL_buffinit(L, &b);
|
||||
while (n-- > 0)
|
||||
addnchar(L, s, l);
|
||||
closeandpush(L);
|
||||
luaL_addlstring(&b, s, l);
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void str_byte (lua_State *L) {
|
||||
long l;
|
||||
static int str_byte (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
long pos = posrelat(luaL_opt_long(L, 2, 1), l);
|
||||
luaL_arg_check(L, 0<pos && pos<=l, 2, "out of range");
|
||||
luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, "out of range");
|
||||
lua_pushnumber(L, (unsigned char)s[pos-1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void str_char (lua_State *L) {
|
||||
int i = 0;
|
||||
luaL_resetbuffer(L);
|
||||
while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
|
||||
static int str_char (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
int i;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=1; i<=n; i++) {
|
||||
int c = luaL_check_int(L, i);
|
||||
luaL_arg_check(L, (unsigned char)c == c, i, "invalid value");
|
||||
luaL_addchar(L, (unsigned char)c);
|
||||
luaL_putchar(&b, (unsigned char)c);
|
||||
}
|
||||
closeandpush(L);
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ struct Capture {
|
||||
int level; /* total number of captures (finished or unfinished) */
|
||||
struct {
|
||||
const char *init;
|
||||
int len; /* -1 signals unfinished capture */
|
||||
long len; /* -1 signals unfinished capture */
|
||||
} capture[MAX_CAPTURES];
|
||||
};
|
||||
|
||||
@@ -135,16 +135,6 @@ struct Capture {
|
||||
#define SPECIALS "^$*+?.([%-"
|
||||
|
||||
|
||||
static void push_captures (lua_State *L, struct Capture *cap) {
|
||||
int i;
|
||||
for (i=0; i<cap->level; i++) {
|
||||
int l = cap->capture[i].len;
|
||||
if (l == -1) lua_error(L, "unfinished capture");
|
||||
lua_pushlstring(L, cap->capture[i].init, l);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int check_capture (lua_State *L, int l, struct Capture *cap) {
|
||||
l -= '1';
|
||||
if (!(0 <= l && l < cap->level && cap->capture[l].len != -1))
|
||||
@@ -238,7 +228,8 @@ int luaI_singlematch (int c, const char *p, const char *ep) {
|
||||
}
|
||||
|
||||
|
||||
static const char *match (lua_State *L, const char *s, const char *p, struct Capture *cap);
|
||||
static const char *match (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap);
|
||||
|
||||
|
||||
static const char *matchbalance (lua_State *L, const char *s, const char *p,
|
||||
@@ -261,12 +252,12 @@ static const char *matchbalance (lua_State *L, const char *s, const char *p,
|
||||
}
|
||||
|
||||
|
||||
static const char *max_expand (lua_State *L, const char *s, const char *p, const char *ep,
|
||||
struct Capture *cap) {
|
||||
int i = 0; /* counts maximum expand for item */
|
||||
static const char *max_expand (lua_State *L, const char *s, const char *p,
|
||||
const char *ep, struct Capture *cap) {
|
||||
long i = 0; /* counts maximum expand for item */
|
||||
while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
|
||||
i++;
|
||||
/* keeps trying to match mith the maximum repetitions */
|
||||
/* keeps trying to match with the maximum repetitions */
|
||||
while (i>=0) {
|
||||
const char *res = match(L, (s+i), ep+1, cap);
|
||||
if (res) return res;
|
||||
@@ -276,8 +267,8 @@ static const char *max_expand (lua_State *L, const char *s, const char *p, const
|
||||
}
|
||||
|
||||
|
||||
static const char *min_expand (lua_State *L, const char *s, const char *p, const char *ep,
|
||||
struct Capture *cap) {
|
||||
static const char *min_expand (lua_State *L, const char *s, const char *p,
|
||||
const char *ep, struct Capture *cap) {
|
||||
for (;;) {
|
||||
const char *res = match(L, s, ep+1, cap);
|
||||
if (res != NULL)
|
||||
@@ -317,15 +308,16 @@ static const char *end_capture (lua_State *L, const char *s, const char *p,
|
||||
static const char *match_capture (lua_State *L, const char *s, int level,
|
||||
struct Capture *cap) {
|
||||
int l = check_capture(L, level, cap);
|
||||
int len = cap->capture[l].len;
|
||||
if (cap->src_end-s >= len &&
|
||||
size_t len = cap->capture[l].len;
|
||||
if ((size_t)(cap->src_end-s) >= len &&
|
||||
memcmp(cap->capture[l].init, s, len) == 0)
|
||||
return s+len;
|
||||
else return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char *match (lua_State *L, const char *s, const char *p, struct Capture *cap) {
|
||||
static const char *match (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap) {
|
||||
init: /* using goto's to optimize tail recursion */
|
||||
switch (*p) {
|
||||
case '(': /* start capture */
|
||||
@@ -376,8 +368,10 @@ static const char *match (lua_State *L, const char *s, const char *p, struct Cap
|
||||
|
||||
|
||||
|
||||
static const char *memfind (const char *s1, long l1, const char *s2, long l2) {
|
||||
if (l2 == 0) return s1; /* empty strings are everywhere ;-) */
|
||||
static const char *lmemfind (const char *s1, size_t l1,
|
||||
const char *s2, size_t l2) {
|
||||
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
|
||||
else {
|
||||
const char *init; /* to search for a `*s2' inside `s1' */
|
||||
l2--; /* 1st char will be checked by `memchr' */
|
||||
@@ -396,20 +390,32 @@ static const char *memfind (const char *s1, long l1, const char *s2, long l2) {
|
||||
}
|
||||
|
||||
|
||||
static void str_find (lua_State *L) {
|
||||
long l1, l2;
|
||||
static int push_captures (lua_State *L, struct Capture *cap) {
|
||||
int i;
|
||||
luaL_checkstack(L, cap->level, "too many captures");
|
||||
for (i=0; i<cap->level; i++) {
|
||||
int l = cap->capture[i].len;
|
||||
if (l == -1) lua_error(L, "unfinished capture");
|
||||
lua_pushlstring(L, cap->capture[i].init, l);
|
||||
}
|
||||
return cap->level; /* number of strings pushed */
|
||||
}
|
||||
|
||||
|
||||
static int str_find (lua_State *L) {
|
||||
size_t l1, l2;
|
||||
const char *s = luaL_check_lstr(L, 1, &l1);
|
||||
const char *p = luaL_check_lstr(L, 2, &l2);
|
||||
long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
|
||||
struct Capture cap;
|
||||
luaL_arg_check(L, 0 <= init && init <= l1, 3, "out of range");
|
||||
if (lua_getparam(L, 4) != LUA_NOOBJECT ||
|
||||
strpbrk(p, SPECIALS) == NULL) { /* no special characters? */
|
||||
const char *s2 = memfind(s+init, l1, p, l2);
|
||||
luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range");
|
||||
if (lua_gettop(L) > 3 || /* extra argument? */
|
||||
strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
|
||||
const char *s2 = lmemfind(s+init, l1-init, p, l2);
|
||||
if (s2) {
|
||||
lua_pushnumber(L, s2-s+1);
|
||||
lua_pushnumber(L, s2-s+l2);
|
||||
return;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -422,69 +428,60 @@ static void str_find (lua_State *L) {
|
||||
if ((res=match(L, s1, p, &cap)) != NULL) {
|
||||
lua_pushnumber(L, s1-s+1); /* start */
|
||||
lua_pushnumber(L, res-s); /* end */
|
||||
push_captures(L, &cap);
|
||||
return;
|
||||
return push_captures(L, &cap) + 2;
|
||||
}
|
||||
} while (s1++<cap.src_end && !anchor);
|
||||
}
|
||||
lua_pushnil(L); /* not found */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) {
|
||||
if (lua_isstring(L, newp)) {
|
||||
const char *news = lua_getstring(L, newp);
|
||||
int l = lua_strlen(L, newp);
|
||||
int i;
|
||||
static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||
if (lua_isstring(L, 3)) {
|
||||
const char *news = lua_tostring(L, 3);
|
||||
size_t l = lua_strlen(L, 3);
|
||||
size_t i;
|
||||
for (i=0; i<l; i++) {
|
||||
if (news[i] != ESC)
|
||||
luaL_addchar(L, news[i]);
|
||||
luaL_putchar(b, news[i]);
|
||||
else {
|
||||
i++; /* skip ESC */
|
||||
if (!isdigit((unsigned char)news[i]))
|
||||
luaL_addchar(L, news[i]);
|
||||
luaL_putchar(b, news[i]);
|
||||
else {
|
||||
int level = check_capture(L, news[i], cap);
|
||||
addnchar(L, cap->capture[level].init, cap->capture[level].len);
|
||||
luaL_addlstring(b, cap->capture[level].init, cap->capture[level].len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else { /* is a function */
|
||||
lua_Object res;
|
||||
int status;
|
||||
int oldbuff;
|
||||
lua_beginblock(L);
|
||||
push_captures(L, cap);
|
||||
/* function may use buffer, so save it and create a new one */
|
||||
oldbuff = luaL_newbuffer(L, 0);
|
||||
status = lua_callfunction(L, newp);
|
||||
/* restore old buffer */
|
||||
luaL_oldbuffer(L, oldbuff);
|
||||
if (status != 0) {
|
||||
lua_endblock(L);
|
||||
lua_error(L, NULL);
|
||||
}
|
||||
res = lua_getresult(L, 1);
|
||||
if (lua_isstring(L, res))
|
||||
addnchar(L, lua_getstring(L, res), lua_strlen(L, res));
|
||||
lua_endblock(L);
|
||||
int n;
|
||||
lua_pushvalue(L, 3);
|
||||
n = push_captures(L, cap);
|
||||
lua_rawcall(L, n, 1);
|
||||
if (lua_isstring(L, -1))
|
||||
luaL_addvalue(b); /* add return to accumulated result */
|
||||
else
|
||||
lua_pop(L, 1); /* function result is not a string: pop it */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void str_gsub (lua_State *L) {
|
||||
long srcl;
|
||||
static int str_gsub (lua_State *L) {
|
||||
size_t srcl;
|
||||
const char *src = luaL_check_lstr(L, 1, &srcl);
|
||||
const char *p = luaL_check_string(L, 2);
|
||||
lua_Object newp = lua_getparam(L, 3);
|
||||
int max_s = luaL_opt_int(L, 4, srcl+1);
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
int n = 0;
|
||||
struct Capture cap;
|
||||
luaL_arg_check(L, lua_isstring(L, newp) || lua_isfunction(L, newp), 3,
|
||||
"string or function expected");
|
||||
luaL_resetbuffer(L);
|
||||
luaL_Buffer b;
|
||||
luaL_arg_check(L,
|
||||
lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
|
||||
3, "string or function expected");
|
||||
luaL_buffinit(L, &b);
|
||||
cap.src_end = src+srcl;
|
||||
while (n < max_s) {
|
||||
const char *e;
|
||||
@@ -492,57 +489,61 @@ static void str_gsub (lua_State *L) {
|
||||
e = match(L, src, p, &cap);
|
||||
if (e) {
|
||||
n++;
|
||||
add_s(L, newp, &cap);
|
||||
add_s(L, &b, &cap);
|
||||
}
|
||||
if (e && e>src) /* non empty match? */
|
||||
src = e; /* skip it */
|
||||
else if (src < cap.src_end)
|
||||
luaL_addchar(L, *src++);
|
||||
luaL_putchar(&b, *src++);
|
||||
else break;
|
||||
if (anchor) break;
|
||||
}
|
||||
addnchar(L, src, cap.src_end-src);
|
||||
closeandpush(L);
|
||||
luaL_addlstring(&b, src, cap.src_end-src);
|
||||
luaL_pushresult(&b);
|
||||
lua_pushnumber(L, n); /* number of substitutions */
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static void luaI_addquoted (lua_State *L, int arg) {
|
||||
long l;
|
||||
static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, arg, &l);
|
||||
luaL_addchar(L, '"');
|
||||
luaL_putchar(b, '"');
|
||||
while (l--) {
|
||||
switch (*s) {
|
||||
case '"': case '\\': case '\n':
|
||||
luaL_addchar(L, '\\');
|
||||
luaL_addchar(L, *s);
|
||||
luaL_putchar(b, '\\');
|
||||
luaL_putchar(b, *s);
|
||||
break;
|
||||
case '\0': addnchar(L, "\\000", 4); break;
|
||||
default: luaL_addchar(L, *s);
|
||||
case '\0': luaL_addlstring(b, "\\000", 4); break;
|
||||
default: luaL_putchar(b, *s);
|
||||
}
|
||||
s++;
|
||||
}
|
||||
luaL_addchar(L, '"');
|
||||
luaL_putchar(b, '"');
|
||||
}
|
||||
|
||||
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
|
||||
#define MAX_ITEM 512
|
||||
/* maximum size of each format specification (such as '%-099.99d') */
|
||||
#define MAX_FORMAT 20 /* arbitrary limit */
|
||||
#define MAX_FORMAT 20
|
||||
|
||||
static void str_format (lua_State *L) {
|
||||
static int str_format (lua_State *L) {
|
||||
int arg = 1;
|
||||
const char *strfrmt = luaL_check_string(L, arg);
|
||||
luaL_resetbuffer(L);
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
while (*strfrmt) {
|
||||
if (*strfrmt != '%')
|
||||
luaL_addchar(L, *strfrmt++);
|
||||
luaL_putchar(&b, *strfrmt++);
|
||||
else if (*++strfrmt == '%')
|
||||
luaL_addchar(L, *strfrmt++); /* %% */
|
||||
luaL_putchar(&b, *strfrmt++); /* %% */
|
||||
else { /* format item */
|
||||
struct Capture cap;
|
||||
char form[MAX_FORMAT]; /* to store the format ('%...') */
|
||||
char *buff; /* to store the formatted item */
|
||||
char buff[MAX_ITEM]; /* to store the formatted item */
|
||||
const char *initf = strfrmt;
|
||||
form[0] = '%';
|
||||
if (isdigit((unsigned char)*initf) && *(initf+1) == '$') {
|
||||
@@ -558,7 +559,6 @@ static void str_format (lua_State *L) {
|
||||
lua_error(L, "invalid format (width or precision too long)");
|
||||
strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
|
||||
form[strfrmt-initf+2] = 0;
|
||||
buff = luaL_openspace(L, 512); /* 512 > len(format('%99.99f', -1e308)) */
|
||||
switch (*strfrmt++) {
|
||||
case 'c': case 'd': case 'i':
|
||||
sprintf(buff, form, luaL_check_int(L, arg));
|
||||
@@ -570,15 +570,16 @@ static void str_format (lua_State *L) {
|
||||
sprintf(buff, form, luaL_check_number(L, arg));
|
||||
break;
|
||||
case 'q':
|
||||
luaI_addquoted(L, arg);
|
||||
luaI_addquoted(L, &b, arg);
|
||||
continue; /* skip the "addsize" at the end */
|
||||
case 's': {
|
||||
long l;
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, arg, &l);
|
||||
if (cap.capture[1].len == 0 && l >= 100) {
|
||||
/* no precision and string is too long to be formatted;
|
||||
keep original string */
|
||||
addnchar(L, s, l);
|
||||
lua_pushvalue(L, arg);
|
||||
luaL_addvalue(&b);
|
||||
continue; /* skip the "addsize" at the end */
|
||||
}
|
||||
else {
|
||||
@@ -589,10 +590,11 @@ static void str_format (lua_State *L) {
|
||||
default: /* also treat cases 'pnLlh' */
|
||||
lua_error(L, "invalid option in `format'");
|
||||
}
|
||||
luaL_addsize(L, strlen(buff));
|
||||
luaL_addlstring(&b, buff, strlen(buff));
|
||||
}
|
||||
}
|
||||
closeandpush(L); /* push the result */
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -614,6 +616,6 @@ static const struct luaL_reg strlib[] = {
|
||||
/*
|
||||
** Open string library
|
||||
*/
|
||||
void lua_strlibopen (lua_State *L) {
|
||||
LUALIB_API void lua_strlibopen (lua_State *L) {
|
||||
luaL_openl(L, strlib);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 1.38 2000/03/29 20:19:20 roberto Exp roberto $
|
||||
** $Id: ltable.c,v 1.57 2000/10/05 12:14:08 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -18,21 +18,20 @@
|
||||
*/
|
||||
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define gcsize(L, n) numblocks(L, n*2, sizeof(Hash))
|
||||
#define gcsize(L, n) (sizeof(Hash)+(n)*sizeof(Node))
|
||||
|
||||
|
||||
|
||||
#define TagDefault TAG_TABLE
|
||||
#define TagDefault LUA_TTABLE
|
||||
|
||||
|
||||
|
||||
@@ -43,31 +42,35 @@
|
||||
Node *luaH_mainposition (const Hash *t, const TObject *key) {
|
||||
unsigned long h;
|
||||
switch (ttype(key)) {
|
||||
case TAG_NUMBER:
|
||||
case LUA_TNUMBER:
|
||||
h = (unsigned long)(long)nvalue(key);
|
||||
break;
|
||||
case TAG_STRING: case TAG_USERDATA:
|
||||
h = tsvalue(key)->hash;
|
||||
case LUA_TSTRING:
|
||||
h = tsvalue(key)->u.s.hash;
|
||||
break;
|
||||
case TAG_TABLE:
|
||||
h = IntPoint(L, avalue(key));
|
||||
case LUA_TUSERDATA:
|
||||
h = IntPoint(tsvalue(key));
|
||||
break;
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
h = IntPoint(L, clvalue(key));
|
||||
case LUA_TTABLE:
|
||||
h = IntPoint(hvalue(key));
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
h = IntPoint(clvalue(key));
|
||||
break;
|
||||
default:
|
||||
return NULL; /* invalid key */
|
||||
}
|
||||
LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
|
||||
LUA_ASSERT(h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
|
||||
"a&(x-1) == a%x, for x power of 2");
|
||||
return &t->node[h&((unsigned int)t->size-1)];
|
||||
return &t->node[h&(t->size-1)];
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
||||
static const TObject *luaH_getany (lua_State *L, const Hash *t,
|
||||
const TObject *key) {
|
||||
Node *n = luaH_mainposition(t, key);
|
||||
if (!n)
|
||||
lua_error(L, "unexpected type to index table");
|
||||
lua_error(L, "table index is nil");
|
||||
else do {
|
||||
if (luaO_equalObj(key, &n->key))
|
||||
return &n->val;
|
||||
@@ -77,40 +80,109 @@ const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
||||
}
|
||||
|
||||
|
||||
int luaH_pos (lua_State *L, const Hash *t, const TObject *key) {
|
||||
const TObject *v = luaH_get(L, t, key);
|
||||
return (v == &luaO_nilobject) ? -1 : /* key not found */
|
||||
(int)(((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node));
|
||||
/* specialized version for numbers */
|
||||
const TObject *luaH_getnum (const Hash *t, Number key) {
|
||||
Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
|
||||
do {
|
||||
if (ttype(&n->key) == LUA_TNUMBER && nvalue(&n->key) == key)
|
||||
return &n->val;
|
||||
n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject; /* key not found */
|
||||
}
|
||||
|
||||
|
||||
/* specialized version for strings */
|
||||
const TObject *luaH_getstr (const Hash *t, TString *key) {
|
||||
Node *n = &t->node[key->u.s.hash&(t->size-1)];
|
||||
do {
|
||||
if (ttype(&n->key) == LUA_TSTRING && tsvalue(&n->key) == key)
|
||||
return &n->val;
|
||||
n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject; /* key not found */
|
||||
}
|
||||
|
||||
static Node *hashnodecreate (lua_State *L, int nhash) {
|
||||
Node *v = luaM_newvector(L, nhash, Node);
|
||||
int i;
|
||||
for (i=0; i<nhash; i++) {
|
||||
ttype(&v[i].key) = ttype(&v[i].val) = TAG_NIL;
|
||||
v[i].next = NULL;
|
||||
|
||||
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TNUMBER: return luaH_getnum(t, nvalue(key));
|
||||
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
|
||||
default: return luaH_getany(L, t, key);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
static void setnodevector (lua_State *L, Hash *t, int size) {
|
||||
t->node = hashnodecreate(L, size);
|
||||
Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
|
||||
int i;
|
||||
if (ttype(key) == LUA_TNIL)
|
||||
i = 0; /* first iteration */
|
||||
else {
|
||||
const TObject *v = luaH_get(L, t, key);
|
||||
if (v == &luaO_nilobject)
|
||||
lua_error(L, "invalid key for `next'");
|
||||
i = (int)(((const char *)v -
|
||||
(const char *)(&t->node[0].val)) / sizeof(Node)) + 1;
|
||||
}
|
||||
for (; i<t->size; i++) {
|
||||
Node *n = node(t, i);
|
||||
if (ttype(val(n)) != LUA_TNIL)
|
||||
return n;
|
||||
}
|
||||
return NULL; /* no more elements */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** try to remove a key without value from a table. To avoid problems with
|
||||
** hash, change `key' for a number with the same hash.
|
||||
*/
|
||||
void luaH_remove (Hash *t, TObject *key) {
|
||||
if (ttype(key) == LUA_TNUMBER ||
|
||||
(ttype(key) == LUA_TSTRING && tsvalue(key)->len <= 30))
|
||||
return; /* do not remove numbers nor small strings */
|
||||
else {
|
||||
/* try to find a number `n' with the same hash as `key' */
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
int n = mp - &t->node[0];
|
||||
/* make sure `n' is not in `t' */
|
||||
while (luaH_getnum(t, n) != &luaO_nilobject) {
|
||||
if (n >= MAX_INT - t->size)
|
||||
return; /* give up; (to avoid overflow) */
|
||||
n += t->size;
|
||||
}
|
||||
ttype(key) = LUA_TNUMBER;
|
||||
nvalue(key) = n;
|
||||
LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void setnodevector (lua_State *L, Hash *t, lint32 size) {
|
||||
int i;
|
||||
if (size > MAX_INT)
|
||||
lua_error(L, "table overflow");
|
||||
t->node = luaM_newvector(L, size, Node);
|
||||
for (i=0; i<(int)size; i++) {
|
||||
ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL;
|
||||
t->node[i].next = NULL;
|
||||
}
|
||||
L->nblocks += gcsize(L, size) - gcsize(L, t->size);
|
||||
t->size = size;
|
||||
t->firstfree = &t->node[size-1]; /* first free position to be used */
|
||||
L->nblocks += gcsize(L, size);
|
||||
}
|
||||
|
||||
|
||||
Hash *luaH_new (lua_State *L, int size) {
|
||||
Hash *t = luaM_new(L, Hash);
|
||||
setnodevector(L, t, luaO_power2(size));
|
||||
t->htag = TagDefault;
|
||||
t->next = L->roottable;
|
||||
L->roottable = t;
|
||||
t->marked = 0;
|
||||
t->mark = t;
|
||||
t->size = 0;
|
||||
L->nblocks += gcsize(L, 0);
|
||||
t->node = NULL;
|
||||
setnodevector(L, t, luaO_power2(size));
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -122,65 +194,65 @@ void luaH_free (lua_State *L, Hash *t) {
|
||||
}
|
||||
|
||||
|
||||
static int newsize (const Hash *t) {
|
||||
static int numuse (const Hash *t) {
|
||||
Node *v = t->node;
|
||||
int size = t->size;
|
||||
int realuse = 0;
|
||||
int i;
|
||||
for (i=0; i<size; i++) {
|
||||
if (ttype(&v[i].val) != TAG_NIL)
|
||||
if (ttype(&v[i].val) != LUA_TNIL)
|
||||
realuse++;
|
||||
}
|
||||
return luaO_power2(realuse+realuse/4+1);
|
||||
return realuse;
|
||||
}
|
||||
|
||||
|
||||
static void rehash (lua_State *L, Hash *t) {
|
||||
int oldsize = t->size;
|
||||
Node *nold = t->node;
|
||||
int nelems = numuse(t);
|
||||
int i;
|
||||
L->nblocks -= gcsize(L, oldsize);
|
||||
setnodevector(L, t, newsize(t)); /* create new array of nodes */
|
||||
LUA_ASSERT(nelems<=oldsize, "wrong count");
|
||||
if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
|
||||
setnodevector(L, t, (lint32)oldsize*2);
|
||||
else if (nelems <= oldsize/4 && /* less than 1/4? */
|
||||
oldsize > MINPOWER2)
|
||||
setnodevector(L, t, oldsize/2);
|
||||
else
|
||||
setnodevector(L, t, oldsize);
|
||||
for (i=0; i<oldsize; i++) {
|
||||
Node *old = nold+i;
|
||||
if (ttype(&old->val) != TAG_NIL)
|
||||
luaH_set(L, t, &old->key, &old->val);
|
||||
if (ttype(&old->val) != LUA_TNIL)
|
||||
*luaH_set(L, t, &old->key) = old->val;
|
||||
}
|
||||
luaM_free(L, nold); /* free old array */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** sets a pair key-value in a hash table; first, check whether key is
|
||||
** inserts a key into a hash table; first, check whether key is
|
||||
** already present; if not, check whether key's main position is free;
|
||||
** if not, check whether colliding node is in its main position or not;
|
||||
** if it is not, move colliding node to an empty place and put new pair
|
||||
** if it is not, move colliding node to an empty place and put new key
|
||||
** in its main position; otherwise (colliding node is in its main position),
|
||||
** new pair goes to an empty position.
|
||||
** Tricky point: the only place where an old element is moved is when
|
||||
** we move the colliding node to an empty place; nevertheless, its old
|
||||
** value is still in that position until we set the value for the new
|
||||
** pair; therefore, even when `val' points to an element of this table
|
||||
** (this happens when we use `luaH_move'), there is no problem.
|
||||
** new key goes to an empty position.
|
||||
*/
|
||||
void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
Node *n = mp;
|
||||
if (!mp)
|
||||
lua_error(L, "unexpected type to index table");
|
||||
lua_error(L, "table index is nil");
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_equalObj(key, &n->key)) {
|
||||
n->val = *val; /* update value */
|
||||
return; /* that's all */
|
||||
}
|
||||
if (luaO_equalObj(key, &n->key))
|
||||
return &n->val; /* that's all */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
/* `key' not found; must insert it */
|
||||
if (ttype(&mp->key) != TAG_NIL) { /* main position is not free? */
|
||||
if (ttype(&mp->key) != LUA_TNIL) { /* main position is not free? */
|
||||
Node *othern; /* main position of colliding node */
|
||||
n = t->firstfree; /* get a free place */
|
||||
/* is colliding node out of its main position? (can only happens if
|
||||
its position if after "firstfree") */
|
||||
its position is after "firstfree") */
|
||||
if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
|
||||
/* yes; move colliding node into free position */
|
||||
while (othern->next != mp) othern = othern->next; /* find previous */
|
||||
@@ -196,29 +268,36 @@ void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
|
||||
}
|
||||
}
|
||||
mp->key = *key;
|
||||
mp->val = *val;
|
||||
for (;;) { /* check free places */
|
||||
if (ttype(&t->firstfree->key) == TAG_NIL)
|
||||
return; /* OK; table still has a free place */
|
||||
for (;;) { /* correct `firstfree' */
|
||||
if (ttype(&t->firstfree->key) == LUA_TNIL)
|
||||
return &mp->val; /* OK; table still has a free place */
|
||||
else if (t->firstfree == t->node) break; /* cannot decrement from here */
|
||||
else (t->firstfree)--;
|
||||
}
|
||||
rehash(L, t); /* no more free places */
|
||||
return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
|
||||
}
|
||||
|
||||
|
||||
void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) {
|
||||
TObject *luaH_setint (lua_State *L, Hash *t, int key) {
|
||||
TObject index;
|
||||
ttype(&index) = TAG_NUMBER;
|
||||
ttype(&index) = LUA_TNUMBER;
|
||||
nvalue(&index) = key;
|
||||
luaH_set(L, t, &index, val);
|
||||
return luaH_set(L, t, &index);
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaH_getint (lua_State *L, const Hash *t, int key) {
|
||||
TObject index;
|
||||
ttype(&index) = TAG_NUMBER;
|
||||
nvalue(&index) = key;
|
||||
return luaH_get(L, t, &index);
|
||||
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) {
|
||||
TObject *value, index;
|
||||
ttype(&index) = LUA_TSTRING;
|
||||
tsvalue(&index) = key;
|
||||
value = luaH_set(L, t, &index);
|
||||
ttype(value) = LUA_TNUMBER;
|
||||
nvalue(value) = val;
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaH_getglobal (lua_State *L, const char *name) {
|
||||
return luaH_getstr(L->gt, luaS_new(L, name));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 1.17 1999/11/23 13:58:02 roberto Exp roberto $
|
||||
** $Id: ltable.h,v 1.23 2000/06/06 16:31:41 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -14,16 +14,18 @@
|
||||
#define key(n) (&(n)->key)
|
||||
#define val(n) (&(n)->val)
|
||||
|
||||
#define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getint(L, t, from)))
|
||||
|
||||
Hash *luaH_new (lua_State *L, int nhash);
|
||||
void luaH_free (lua_State *L, Hash *t);
|
||||
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key);
|
||||
void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val);
|
||||
int luaH_pos (lua_State *L, const Hash *t, const TObject *r);
|
||||
void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val);
|
||||
const TObject *luaH_getint (lua_State *L, const Hash *t, int key);
|
||||
const TObject *luaH_getnum (const Hash *t, Number key);
|
||||
const TObject *luaH_getstr (const Hash *t, TString *key);
|
||||
void luaH_remove (Hash *t, TObject *key);
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key);
|
||||
Node * luaH_next (lua_State *L, const Hash *t, const TObject *r);
|
||||
TObject *luaH_setint (lua_State *L, Hash *t, int key);
|
||||
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val);
|
||||
unsigned long luaH_hash (lua_State *L, const TObject *key);
|
||||
const TObject *luaH_getglobal (lua_State *L, const char *name);
|
||||
|
||||
/* exported only for debugging */
|
||||
Node *luaH_mainposition (const Hash *t, const TObject *key);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 1.15 2000/04/13 16:51:01 roberto Exp roberto $
|
||||
** $Id: ltests.c,v 1.53 2000/10/30 16:29:59 roberto Exp roberto $
|
||||
** Internal Module for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,34 +10,38 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lcode.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
void luaB_opentests (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** The whole module only makes sense with DEBUG on
|
||||
** The whole module only makes sense with LUA_DEBUG on
|
||||
*/
|
||||
#ifdef DEBUG
|
||||
#ifdef LUA_DEBUG
|
||||
|
||||
|
||||
|
||||
static void setnameval (lua_State *L, lua_Object t, const char *name, int val) {
|
||||
lua_pushobject(L, t);
|
||||
static void setnameval (lua_State *L, const char *name, int val) {
|
||||
lua_pushstring(L, name);
|
||||
lua_pushnumber(L, val);
|
||||
lua_settable(L);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
@@ -48,168 +52,296 @@ static void setnameval (lua_State *L, lua_Object t, const char *name, int val) {
|
||||
*/
|
||||
|
||||
|
||||
#define O(o) sprintf(buff, "%s", o)
|
||||
#define U(o) sprintf(buff, "%-12s%4u", o, GETARG_U(i))
|
||||
#define S(o) sprintf(buff, "%-12s%4d", o, GETARG_S(i))
|
||||
#define AB(o) sprintf(buff, "%-12s%4d %4d", o, GETARG_A(i), GETARG_B(i))
|
||||
static const char *const instrname[NUM_OPCODES] = {
|
||||
"END", "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
|
||||
"PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
|
||||
"GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
|
||||
"CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
|
||||
"ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
|
||||
"JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
|
||||
"JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
|
||||
"LFORLOOP", "CLOSURE"
|
||||
};
|
||||
|
||||
|
||||
|
||||
static int pushop (lua_State *L, Instruction i) {
|
||||
static int pushop (lua_State *L, Proto *p, int pc) {
|
||||
char buff[100];
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_END: O("END"); lua_pushstring(L, buff); return 0;
|
||||
case OP_RETURN: U("RETURN"); break;
|
||||
case OP_CALL: AB("CALL"); break;
|
||||
case OP_TAILCALL: AB("TAILCALL"); break;
|
||||
case OP_PUSHNIL: U("PUSHNIL"); break;
|
||||
case OP_POP: U("POP"); break;
|
||||
case OP_PUSHINT: S("PUSHINT"); break;
|
||||
case OP_PUSHSTRING: U("PUSHSTRING"); break;
|
||||
case OP_PUSHNUM: U("PUSHNUM"); break;
|
||||
case OP_PUSHNEGNUM: U("PUSHNEGNUM"); break;
|
||||
case OP_PUSHUPVALUE: U("PUSHUPVALUE"); break;
|
||||
case OP_GETLOCAL: U("GETLOCAL"); break;
|
||||
case OP_GETGLOBAL: U("GETGLOBAL"); break;
|
||||
case OP_GETTABLE: O("GETTABLE"); break;
|
||||
case OP_GETDOTTED: U("GETDOTTED"); break;
|
||||
case OP_GETINDEXED: U("GETINDEXED"); break;
|
||||
case OP_PUSHSELF: U("PUSHSELF"); break;
|
||||
case OP_CREATETABLE: U("CREATETABLE"); break;
|
||||
case OP_SETLOCAL: U("SETLOCAL"); break;
|
||||
case OP_SETGLOBAL: U("SETGLOBAL"); break;
|
||||
case OP_SETTABLE: AB("SETTABLE"); break;
|
||||
case OP_SETLIST: AB("SETLIST"); break;
|
||||
case OP_SETMAP: U("SETMAP"); break;
|
||||
case OP_ADD: O("ADD"); break;
|
||||
case OP_ADDI: S("ADDI"); break;
|
||||
case OP_SUB: O("SUB"); break;
|
||||
case OP_MULT: O("MULT"); break;
|
||||
case OP_DIV: O("DIV"); break;
|
||||
case OP_POW: O("POW"); break;
|
||||
case OP_CONCAT: U("CONCAT"); break;
|
||||
case OP_MINUS: O("MINUS"); break;
|
||||
case OP_NOT: O("NOT"); break;
|
||||
case OP_JMPNE: S("JMPNE"); break;
|
||||
case OP_JMPEQ: S("JMPEQ"); break;
|
||||
case OP_JMPLT: S("JMPLT"); break;
|
||||
case OP_JMPLE: S("JMPLE"); break;
|
||||
case OP_JMPGT: S("JMPGT"); break;
|
||||
case OP_JMPGE: S("JMPGE"); break;
|
||||
case OP_JMPT: S("JMPT"); break;
|
||||
case OP_JMPF: S("JMPF"); break;
|
||||
case OP_JMPONT: S("JMPONT"); break;
|
||||
case OP_JMPONF: S("JMPONF"); break;
|
||||
case OP_JMP: S("JMP"); break;
|
||||
case OP_PUSHNILJMP: O("PUSHNILJMP"); break;
|
||||
case OP_FORPREP: S("OP_FORPREP"); break;
|
||||
case OP_FORLOOP: S("OP_FORLOOP"); break;
|
||||
case OP_CLOSURE: AB("CLOSURE"); break;
|
||||
case OP_SETLINE: U("SETLINE"); break;
|
||||
Instruction i = p->code[pc];
|
||||
OpCode o = GET_OPCODE(i);
|
||||
const char *name = instrname[o];
|
||||
sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
|
||||
switch ((enum Mode)luaK_opproperties[o].mode) {
|
||||
case iO:
|
||||
sprintf(buff+8, "%-12s", name);
|
||||
break;
|
||||
case iU:
|
||||
sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
|
||||
break;
|
||||
case iS:
|
||||
sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
|
||||
break;
|
||||
case iAB:
|
||||
sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
|
||||
break;
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
return (o != OP_END);
|
||||
}
|
||||
|
||||
|
||||
static int listcode (lua_State *L) {
|
||||
int pc;
|
||||
Proto *p;
|
||||
int res;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, "Lua function expected");
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
lua_newtable(L);
|
||||
setnameval(L, "maxstack", p->maxstacksize);
|
||||
setnameval(L, "numparams", p->numparams);
|
||||
pc = 0;
|
||||
do {
|
||||
lua_pushnumber(L, pc+1);
|
||||
res = pushop(L, p, pc++);
|
||||
lua_settable(L, -3);
|
||||
} while (res);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void listcode (lua_State *L) {
|
||||
lua_Object o = luaL_nonnullarg(L, 1);
|
||||
lua_Object t = lua_createtable(L);
|
||||
Instruction *pc;
|
||||
|
||||
static int liststrings (lua_State *L) {
|
||||
Proto *p;
|
||||
int res;
|
||||
luaL_arg_check(L, ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
|
||||
p = clvalue(o)->f.l;
|
||||
setnameval(L, t, "maxstack", p->maxstacksize);
|
||||
setnameval(L, t, "numparams", p->numparams);
|
||||
pc = p->code;
|
||||
do {
|
||||
lua_pushobject(L, t);
|
||||
lua_pushnumber(L, pc - p->code + 1);
|
||||
res = pushop(L, *pc++);
|
||||
lua_settable(L);
|
||||
} while (res);
|
||||
lua_pushobject(L, t);
|
||||
int i;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, "Lua function expected");
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
lua_newtable(L);
|
||||
for (i=0; i<p->nkstr; i++) {
|
||||
lua_pushnumber(L, i+1);
|
||||
lua_pushstring(L, p->kstr[i]->str);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int listlocals (lua_State *L) {
|
||||
Proto *p;
|
||||
int pc = luaL_check_int(L, 2) - 1;
|
||||
int i = 0;
|
||||
const char *name;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, "Lua function expected");
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
||||
lua_pushstring(L, name);
|
||||
return i-1;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
static void get_limits (lua_State *L) {
|
||||
lua_Object t = lua_createtable(L);
|
||||
setnameval(L, t, "SIZE_OP", SIZE_OP);
|
||||
setnameval(L, t, "SIZE_U", SIZE_U);
|
||||
setnameval(L, t, "SIZE_A", SIZE_A);
|
||||
setnameval(L, t, "SIZE_B", SIZE_B);
|
||||
setnameval(L, t, "MAXARG_U", MAXARG_U);
|
||||
setnameval(L, t, "MAXARG_S", MAXARG_S);
|
||||
setnameval(L, t, "MAXARG_A", MAXARG_A);
|
||||
setnameval(L, t, "MAXARG_B", MAXARG_B);
|
||||
setnameval(L, t, "MAXSTACK", MAXSTACK);
|
||||
setnameval(L, t, "MAXLOCALS", MAXLOCALS);
|
||||
setnameval(L, t, "MAXUPVALUES", MAXUPVALUES);
|
||||
setnameval(L, t, "MAXVARSLH", MAXVARSLH);
|
||||
setnameval(L, t, "MAXPARAMS", MAXPARAMS);
|
||||
setnameval(L, t, "LFPF", LFIELDS_PER_FLUSH);
|
||||
setnameval(L, t, "RFPF", RFIELDS_PER_FLUSH);
|
||||
lua_pushobject(L, t);
|
||||
static int get_limits (lua_State *L) {
|
||||
lua_newtable(L);
|
||||
setnameval(L, "BITS_INT", BITS_INT);
|
||||
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
||||
setnameval(L, "MAXARG_A", MAXARG_A);
|
||||
setnameval(L, "MAXARG_B", MAXARG_B);
|
||||
setnameval(L, "MAXARG_S", MAXARG_S);
|
||||
setnameval(L, "MAXARG_U", MAXARG_U);
|
||||
setnameval(L, "MAXLOCALS", MAXLOCALS);
|
||||
setnameval(L, "MAXPARAMS", MAXPARAMS);
|
||||
setnameval(L, "MAXSTACK", MAXSTACK);
|
||||
setnameval(L, "MAXUPVALUES", MAXUPVALUES);
|
||||
setnameval(L, "MAXVARSLH", MAXVARSLH);
|
||||
setnameval(L, "RFPF", RFIELDS_PER_FLUSH);
|
||||
setnameval(L, "SIZE_A", SIZE_A);
|
||||
setnameval(L, "SIZE_B", SIZE_B);
|
||||
setnameval(L, "SIZE_OP", SIZE_OP);
|
||||
setnameval(L, "SIZE_U", SIZE_U);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void mem_query (lua_State *L) {
|
||||
lua_pushnumber(L, memdebug_total);
|
||||
lua_pushnumber(L, memdebug_numblocks);
|
||||
lua_pushnumber(L, memdebug_maxmem);
|
||||
}
|
||||
|
||||
|
||||
static void hash_query (lua_State *L) {
|
||||
lua_Object o = luaL_nonnullarg(L, 1);
|
||||
if (lua_getparam(L, 2) == LUA_NOOBJECT) {
|
||||
luaL_arg_check(L, ttype(o) == TAG_STRING, 1, "string expected");
|
||||
lua_pushnumber(L, tsvalue(o)->hash);
|
||||
static int mem_query (lua_State *L) {
|
||||
if (lua_isnull(L, 1)) {
|
||||
lua_pushnumber(L, memdebug_total);
|
||||
lua_pushnumber(L, memdebug_numblocks);
|
||||
lua_pushnumber(L, memdebug_maxmem);
|
||||
return 3;
|
||||
}
|
||||
else {
|
||||
const Hash *t = avalue(luaL_tablearg(L, 2));
|
||||
lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
|
||||
memdebug_memlimit = luaL_check_int(L, 1);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void table_query (lua_State *L) {
|
||||
const Hash *t = avalue(luaL_tablearg(L, 1));
|
||||
static int hash_query (lua_State *L) {
|
||||
if (lua_isnull(L, 2)) {
|
||||
luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, "string expected");
|
||||
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
|
||||
}
|
||||
else {
|
||||
Hash *t;
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
t = hvalue(luaA_index(L, 2));
|
||||
lua_pushnumber(L, luaH_mainposition(t, luaA_index(L, 1)) - t->node);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int table_query (lua_State *L) {
|
||||
const Hash *t;
|
||||
int i = luaL_opt_int(L, 2, -1);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
t = hvalue(luaA_index(L, 1));
|
||||
if (i == -1) {
|
||||
lua_pushnumber(L, t->size);
|
||||
lua_pushnumber(L, t->firstfree - t->node);
|
||||
return 2;
|
||||
}
|
||||
else if (i < t->size) {
|
||||
luaA_pushobject(L, &t->node[i].key);
|
||||
luaA_pushobject(L, &t->node[i].val);
|
||||
if (t->node[i].next)
|
||||
if (t->node[i].next) {
|
||||
lua_pushnumber(L, t->node[i].next - t->node);
|
||||
return 3;
|
||||
}
|
||||
else
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void string_query (lua_State *L) {
|
||||
int h = luaL_check_int(L, 1) - 1;
|
||||
static int string_query (lua_State *L) {
|
||||
stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &L->strt : &L->udt;
|
||||
int s = luaL_opt_int(L, 2, 0) - 1;
|
||||
if (s==-1) {
|
||||
if (h < NUM_HASHS) {
|
||||
lua_pushnumber(L, L->string_root[h].nuse);
|
||||
lua_pushnumber(L, L->string_root[h].size);
|
||||
lua_pushnumber(L ,tb->nuse);
|
||||
lua_pushnumber(L ,tb->size);
|
||||
return 2;
|
||||
}
|
||||
else if (s < tb->size) {
|
||||
TString *ts;
|
||||
int n = 0;
|
||||
for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
|
||||
ttype(L->top) = LUA_TSTRING;
|
||||
tsvalue(L->top) = ts;
|
||||
incr_top;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int tref (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getref (lua_State *L) {
|
||||
if (lua_getref(L, luaL_check_int(L, 1)))
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unref (lua_State *L) {
|
||||
lua_unref(L, luaL_check_int(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int newuserdata (lua_State *L) {
|
||||
if (lua_isnumber(L, 2))
|
||||
lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
|
||||
else
|
||||
lua_newuserdata(L, luaL_check_int(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int udataval (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
lua_pushnumber(L, (int)lua_touserdata(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int newstate (lua_State *L) {
|
||||
lua_State *L1 = lua_open(luaL_check_int(L, 1));
|
||||
if (L1)
|
||||
lua_pushuserdata(L, L1);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int loadlib (lua_State *L) {
|
||||
lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
|
||||
switch (*luaL_check_string(L, 2)) {
|
||||
case 'm': lua_mathlibopen(L1); break;
|
||||
case 's': lua_strlibopen(L1); break;
|
||||
case 'i': lua_iolibopen(L1); break;
|
||||
case 'd': lua_dblibopen(L1); break;
|
||||
case 'b': lua_baselibopen(L1); break;
|
||||
default: luaL_argerror(L, 2, "invalid option");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int closestate (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
lua_close((lua_State *)lua_touserdata(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int doremote (lua_State *L) {
|
||||
lua_State *L1;
|
||||
const char *code = luaL_check_string(L, 2);
|
||||
int status;
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
L1 = (lua_State *)lua_touserdata(L, 1);
|
||||
status = lua_dostring(L1, code);
|
||||
if (status != 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushnumber(L, status);
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
TString *ts = L->string_root[h].hash[s];
|
||||
for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) {
|
||||
if (ts->constindex == -1) lua_pushstring(L, "<USERDATA>");
|
||||
else lua_pushstring(L, ts->str);
|
||||
}
|
||||
int i = 0;
|
||||
while (!lua_isnull(L1, ++i))
|
||||
lua_pushstring(L, lua_tostring(L1, i));
|
||||
return i-1;
|
||||
}
|
||||
}
|
||||
|
||||
static int settagmethod (lua_State *L) {
|
||||
int tag = luaL_check_int(L, 1);
|
||||
const char *event = luaL_check_string(L, 2);
|
||||
luaL_checkany(L, 3);
|
||||
lua_gettagmethod(L, tag, event);
|
||||
lua_pushvalue(L, 3);
|
||||
lua_settagmethod(L, tag, event);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int pushbool (lua_State *L, int b) {
|
||||
if (b) lua_pushnumber(L, 1);
|
||||
else lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int equal (lua_State *L) {
|
||||
return pushbool(L, lua_equal(L, 1, 2));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
@@ -224,21 +356,25 @@ static void skip (const char **pc) {
|
||||
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
||||
}
|
||||
|
||||
static int getnum (const char **pc) {
|
||||
static int getnum (lua_State *L, const char **pc) {
|
||||
int res = 0;
|
||||
int sig = 1;
|
||||
skip(pc);
|
||||
if (**pc == '.') {
|
||||
res = (int)lua_tonumber(L, -1);
|
||||
lua_pop(L, 1);
|
||||
(*pc)++;
|
||||
return res;
|
||||
}
|
||||
else if (**pc == '-') {
|
||||
sig = -1;
|
||||
(*pc)++;
|
||||
}
|
||||
while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
|
||||
return res;
|
||||
return sig*res;
|
||||
}
|
||||
|
||||
static int getreg (lua_State *L, const char **pc) {
|
||||
skip(pc);
|
||||
if (*(*pc)++ != 'r') lua_error(L, "`testC' expecting a register");
|
||||
return getnum(pc);
|
||||
}
|
||||
|
||||
static const char *getname (const char **pc) {
|
||||
static char buff[30];
|
||||
static const char *getname (char *buff, const char **pc) {
|
||||
int i = 0;
|
||||
skip(pc);
|
||||
while (**pc != '\0' && !strchr(delimits, **pc))
|
||||
@@ -250,149 +386,120 @@ static const char *getname (const char **pc) {
|
||||
|
||||
#define EQ(s1) (strcmp(s1, inst) == 0)
|
||||
|
||||
#define getnum ((getnum)(L, &pc))
|
||||
#define getname ((getname)(buff, &pc))
|
||||
|
||||
static void testC (lua_State *L) {
|
||||
lua_Object reg[10];
|
||||
|
||||
static int testC (lua_State *L) {
|
||||
char buff[30];
|
||||
const char *pc = luaL_check_string(L, 1);
|
||||
for (;;) {
|
||||
const char *inst = getname(&pc);
|
||||
if EQ("") return;
|
||||
else if EQ("pushnum") {
|
||||
lua_pushnumber(L, getnum(&pc));
|
||||
const char *inst = getname;
|
||||
if EQ("") return 0;
|
||||
else if EQ("isnumber") {
|
||||
lua_pushnumber(L, lua_isnumber(L, getnum));
|
||||
}
|
||||
else if EQ("createtable") {
|
||||
reg[getreg(L, &pc)] = lua_createtable(L);
|
||||
else if EQ("isstring") {
|
||||
lua_pushnumber(L, lua_isstring(L, getnum));
|
||||
}
|
||||
else if EQ("closure") {
|
||||
lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(&pc)));
|
||||
lua_pushcclosure(L, f, getnum(&pc));
|
||||
else if EQ("istable") {
|
||||
lua_pushnumber(L, lua_istable(L, getnum));
|
||||
}
|
||||
else if EQ("iscfunction") {
|
||||
lua_pushnumber(L, lua_iscfunction(L, getnum));
|
||||
}
|
||||
else if EQ("isfunction") {
|
||||
lua_pushnumber(L, lua_isfunction(L, getnum));
|
||||
}
|
||||
else if EQ("isuserdata") {
|
||||
lua_pushnumber(L, lua_isuserdata(L, getnum));
|
||||
}
|
||||
else if EQ("isnil") {
|
||||
lua_pushnumber(L, lua_isnil(L, getnum));
|
||||
}
|
||||
else if EQ("isnull") {
|
||||
lua_pushnumber(L, lua_isnull(L, getnum));
|
||||
}
|
||||
else if EQ("tonumber") {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
}
|
||||
else if EQ("tostring") {
|
||||
lua_pushstring(L, lua_tostring(L, getnum));
|
||||
}
|
||||
else if EQ("tonumber") {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
}
|
||||
else if EQ("strlen") {
|
||||
lua_pushnumber(L, lua_strlen(L, getnum));
|
||||
}
|
||||
else if EQ("tocfunction") {
|
||||
lua_pushcfunction(L, lua_tocfunction(L, getnum));
|
||||
}
|
||||
else if EQ("return") {
|
||||
return getnum;
|
||||
}
|
||||
else if EQ("gettop") {
|
||||
lua_pushnumber(L, lua_gettop(L));
|
||||
}
|
||||
else if EQ("settop") {
|
||||
lua_settop(L, getnum);
|
||||
}
|
||||
else if EQ("pop") {
|
||||
reg[getreg(L, &pc)] = lua_pop(L);
|
||||
lua_pop(L, getnum);
|
||||
}
|
||||
else if EQ("getglobal") {
|
||||
int n = getreg(L, &pc);
|
||||
reg[n] = lua_getglobal(L, getname(&pc));
|
||||
else if EQ("pushnum") {
|
||||
lua_pushnumber(L, getnum);
|
||||
}
|
||||
else if EQ("rawgetglobal") {
|
||||
int n = getreg(L, &pc);
|
||||
reg[n] = lua_rawgetglobal(L, getname(&pc));
|
||||
else if EQ("pushvalue") {
|
||||
lua_pushvalue(L, getnum);
|
||||
}
|
||||
else if EQ("ref") {
|
||||
lua_pushnumber(L, lua_ref(L, 0));
|
||||
reg[getreg(L, &pc)] = lua_pop(L);
|
||||
else if EQ("remove") {
|
||||
lua_remove(L, getnum);
|
||||
}
|
||||
else if EQ("reflock") {
|
||||
lua_pushnumber(L, lua_ref(L, 1));
|
||||
reg[getreg(L, &pc)] = lua_pop(L);
|
||||
}
|
||||
else if EQ("getref") {
|
||||
int n = getreg(L, &pc);
|
||||
reg[n] = lua_getref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
|
||||
}
|
||||
else if EQ("unref") {
|
||||
lua_unref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
|
||||
}
|
||||
else if EQ("getparam") {
|
||||
int n = getreg(L, &pc);
|
||||
reg[n] = lua_getparam(L, getnum(&pc)+1); /* skips the command itself */
|
||||
}
|
||||
else if EQ("getresult") {
|
||||
int n = getreg(L, &pc);
|
||||
reg[n] = lua_getparam(L, getnum(&pc));
|
||||
}
|
||||
else if EQ("setglobal") {
|
||||
lua_setglobal(L, getname(&pc));
|
||||
}
|
||||
else if EQ("rawsetglobal") {
|
||||
lua_rawsetglobal(L, getname(&pc));
|
||||
}
|
||||
else if EQ("pushstring") {
|
||||
lua_pushstring(L, getname(&pc));
|
||||
}
|
||||
else if EQ("pushreg") {
|
||||
lua_pushobject(L, reg[getreg(L, &pc)]);
|
||||
}
|
||||
else if EQ("call") {
|
||||
if (lua_call(L, getname(&pc))) lua_error(L, NULL);
|
||||
else if EQ("insert") {
|
||||
lua_insert(L, getnum);
|
||||
}
|
||||
else if EQ("gettable") {
|
||||
reg[getreg(L, &pc)] = lua_gettable(L);
|
||||
}
|
||||
else if EQ("rawgettable") {
|
||||
reg[getreg(L, &pc)] = lua_rawgettable(L);
|
||||
lua_gettable(L, getnum);
|
||||
}
|
||||
else if EQ("settable") {
|
||||
lua_settable(L);
|
||||
}
|
||||
else if EQ("rawsettable") {
|
||||
lua_rawsettable(L);
|
||||
}
|
||||
else if EQ("tag") {
|
||||
lua_pushnumber(L, lua_tag(L, reg[getreg(L, &pc)]));
|
||||
}
|
||||
else if EQ("type") {
|
||||
lua_pushstring(L, lua_type(L, reg[getreg(L, &pc)]));
|
||||
}
|
||||
else if EQ("nextvar") {
|
||||
lua_pushstring(L, lua_nextvar(L, lua_getstring(L, reg[getreg(L, &pc)])));
|
||||
lua_settable(L, getnum);
|
||||
}
|
||||
else if EQ("next") {
|
||||
int n = getreg(L, &pc);
|
||||
n = lua_next(L, reg[n], (int)lua_getnumber(L, reg[getreg(L, &pc)]));
|
||||
lua_pushnumber(L, n);
|
||||
lua_next(L, -2);
|
||||
}
|
||||
else if EQ("equal") {
|
||||
int n1 = getreg(L, &pc);
|
||||
int n2 = getreg(L, &pc);
|
||||
lua_pushnumber(L, lua_equal(L, reg[n1], reg[n2]));
|
||||
else if EQ("concat") {
|
||||
lua_concat(L, getnum);
|
||||
}
|
||||
else if EQ("pushusertag") {
|
||||
int val = getreg(L, &pc);
|
||||
int tag = getreg(L, &pc);
|
||||
lua_pushusertag(L, (void *)(int)lua_getnumber(L, reg[val]),
|
||||
(int)lua_getnumber(L, reg[tag]));
|
||||
else if EQ("rawcall") {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_rawcall(L, narg, nres);
|
||||
}
|
||||
else if EQ("udataval") {
|
||||
int n = getreg(L, &pc);
|
||||
lua_pushnumber(L, (int)lua_getuserdata(L, reg[getreg(L, &pc)]));
|
||||
reg[n] = lua_pop(L);
|
||||
else if EQ("call") {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_call(L, narg, nres);
|
||||
}
|
||||
else if EQ("dostring") {
|
||||
lua_dostring(L, luaL_check_string(L, getnum));
|
||||
}
|
||||
else if EQ("settagmethod") {
|
||||
int n = getreg(L, &pc);
|
||||
lua_settagmethod(L, (int)lua_getnumber(L, reg[n]), getname(&pc));
|
||||
int tag = getnum;
|
||||
const char *event = getname;
|
||||
lua_settagmethod(L, tag, event);
|
||||
}
|
||||
else if EQ("beginblock") {
|
||||
lua_beginblock(L);
|
||||
else if EQ("gettagmethod") {
|
||||
int tag = getnum;
|
||||
const char *event = getname;
|
||||
lua_gettagmethod(L, tag, event);
|
||||
}
|
||||
else if EQ("endblock") {
|
||||
lua_endblock(L);
|
||||
else if EQ("type") {
|
||||
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
|
||||
}
|
||||
else if EQ("newstate") {
|
||||
int stacksize = getnum(&pc);
|
||||
lua_State *L1 = lua_newstate("stack", stacksize,
|
||||
"builtin", getnum(&pc), NULL);
|
||||
lua_pushuserdata(L, L1);
|
||||
}
|
||||
else if EQ("closestate") {
|
||||
lua_close((lua_State *)lua_getuserdata(L, reg[getreg(L, &pc)]));
|
||||
}
|
||||
else if EQ("doremote") {
|
||||
lua_Object ol1 = reg[getreg(L, &pc)];
|
||||
lua_Object str = reg[getreg(L, &pc)];
|
||||
lua_State *L1;
|
||||
lua_Object temp;
|
||||
int i;
|
||||
if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str))
|
||||
lua_error(L, "bad arguments for `doremote'");
|
||||
L1 = (lua_State *)lua_getuserdata(L, ol1);
|
||||
lua_dostring(L1, lua_getstring(L, str));
|
||||
i = 1;
|
||||
while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT)
|
||||
lua_pushstring(L, lua_getstring(L1, temp));
|
||||
}
|
||||
else luaL_verror(L, "unknown command in `testC': %.20s", inst);
|
||||
else luaL_verror(L, "unknown instruction %.30s", buff);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
@@ -403,15 +510,34 @@ static const struct luaL_reg tests_funcs[] = {
|
||||
{"hash", hash_query},
|
||||
{"limits", get_limits},
|
||||
{"listcode", listcode},
|
||||
{"liststrings", liststrings},
|
||||
{"listlocals", listlocals},
|
||||
{"loadlib", loadlib},
|
||||
{"querystr", string_query},
|
||||
{"querytab", table_query},
|
||||
{"testC", testC},
|
||||
{"ref", tref},
|
||||
{"getref", getref},
|
||||
{"unref", unref},
|
||||
{"newuserdata", newuserdata},
|
||||
{"udataval", udataval},
|
||||
{"newstate", newstate},
|
||||
{"closestate", closestate},
|
||||
{"doremote", doremote},
|
||||
{"settagmethod", settagmethod},
|
||||
{"equal", equal},
|
||||
{"totalmem", mem_query}
|
||||
};
|
||||
|
||||
|
||||
void luaB_opentests (lua_State *L) {
|
||||
luaL_openl(L, tests_funcs);
|
||||
lua_newtable(L);
|
||||
lua_getglobals(L);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_setglobals(L);
|
||||
luaL_openl(L, tests_funcs); /* open functions inside new table */
|
||||
lua_setglobals(L); /* restore old table of globals */
|
||||
lua_setglobal(L, "T"); /* set new table as global T */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltm.c,v 1.38 2000/03/29 20:19:20 roberto Exp roberto $
|
||||
** $Id: ltm.c,v 1.55 2000/10/20 16:39:03 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,16 +8,16 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
const char *const luaT_eventname[] = { /* ORDER IM */
|
||||
const char *const luaT_eventname[] = { /* ORDER TM */
|
||||
"gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
|
||||
"mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
|
||||
"le", "gt", "ge", /* deprecated options!! */
|
||||
@@ -25,57 +25,69 @@ const char *const luaT_eventname[] = { /* ORDER IM */
|
||||
};
|
||||
|
||||
|
||||
static int findevent (const char *name) {
|
||||
int i;
|
||||
for (i=0; luaT_eventname[i]; i++)
|
||||
if (strcmp(luaT_eventname[i], name) == 0)
|
||||
return i;
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
|
||||
static int luaI_checkevent (lua_State *L, const char *name, int t) {
|
||||
int e = luaL_findstring(name, luaT_eventname);
|
||||
if (e >= IM_N)
|
||||
luaL_verror(L, "event `%.50s' is deprecated", name);
|
||||
if (e == IM_GC && t == TAG_TABLE)
|
||||
luaL_verror(L, "event `gc' for tables is deprecated");
|
||||
int e = findevent(name);
|
||||
if (e >= TM_N)
|
||||
luaO_verror(L, "event `%.50s' is deprecated", name);
|
||||
if (e == TM_GC && t == LUA_TTABLE)
|
||||
luaO_verror(L, "event `gc' for tables is deprecated");
|
||||
if (e < 0)
|
||||
luaL_verror(L, "`%.50s' is not a valid event name", name);
|
||||
luaO_verror(L, "`%.50s' is not a valid event name", name);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* events in TAG_NIL are all allowed, since this is used as a
|
||||
/* events in LUA_TNIL are all allowed, since this is used as a
|
||||
* 'placeholder' for "default" fallbacks
|
||||
*/
|
||||
/* ORDER LUA_T, ORDER IM */
|
||||
static const char luaT_validevents[NUM_TAGS][IM_N] = {
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_USERDATA */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CCLOSURE */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
|
||||
/* ORDER LUA_T, ORDER TM */
|
||||
static const char luaT_validevents[NUM_TAGS][TM_N] = {
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_TNIL */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_TSTRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TTABLE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0} /* LUA_TFUNCTION */
|
||||
};
|
||||
|
||||
int luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
||||
return (t > TAG_NIL) ? 1 : luaT_validevents[t][e];
|
||||
return (t >= NUM_TAGS) ? 1 : luaT_validevents[t][e];
|
||||
}
|
||||
|
||||
|
||||
static void init_entry (lua_State *L, int tag) {
|
||||
int i;
|
||||
for (i=0; i<IM_N; i++)
|
||||
ttype(luaT_getim(L, tag, i)) = TAG_NIL;
|
||||
for (i=0; i<TM_N; i++)
|
||||
luaT_gettm(L, tag, i) = NULL;
|
||||
L->TMtable[tag].collected = NULL;
|
||||
}
|
||||
|
||||
|
||||
void luaT_init (lua_State *L) {
|
||||
int t;
|
||||
luaM_growvector(L, L->TMtable, 0, NUM_TAGS, struct TM, "", MAX_INT);
|
||||
L->nblocks += NUM_TAGS*sizeof(struct TM);
|
||||
L->last_tag = NUM_TAGS-1;
|
||||
luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
init_entry(L, t);
|
||||
}
|
||||
|
||||
|
||||
int lua_newtag (lua_State *L) {
|
||||
++L->last_tag;
|
||||
luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM, arrEM, MAX_INT);
|
||||
LUA_API int lua_newtag (lua_State *L) {
|
||||
luaM_growvector(L, L->TMtable, L->last_tag, 1, struct TM,
|
||||
"tag table overflow", MAX_INT);
|
||||
L->nblocks += sizeof(struct TM);
|
||||
L->last_tag++;
|
||||
init_entry(L, L->last_tag);
|
||||
return L->last_tag;
|
||||
}
|
||||
@@ -83,76 +95,69 @@ int lua_newtag (lua_State *L) {
|
||||
|
||||
static void checktag (lua_State *L, int tag) {
|
||||
if (!(0 <= tag && tag <= L->last_tag))
|
||||
luaL_verror(L, "%d is not a valid tag", tag);
|
||||
luaO_verror(L, "%d is not a valid tag", tag);
|
||||
}
|
||||
|
||||
void luaT_realtag (lua_State *L, int tag) {
|
||||
if (!(NUM_TAGS <= tag && tag <= L->last_tag))
|
||||
luaL_verror(L, "tag %d was not created by `newtag'", tag);
|
||||
if (!validtag(tag))
|
||||
luaO_verror(L, "tag %d was not created by `newtag'", tag);
|
||||
}
|
||||
|
||||
|
||||
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
|
||||
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
|
||||
int e;
|
||||
checktag(L, tagto);
|
||||
checktag(L, tagfrom);
|
||||
for (e=0; e<IM_N; e++) {
|
||||
for (e=0; e<TM_N; e++) {
|
||||
if (luaT_validevent(tagto, e))
|
||||
*luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
|
||||
luaT_gettm(L, tagto, e) = luaT_gettm(L, tagfrom, e);
|
||||
}
|
||||
return tagto;
|
||||
}
|
||||
|
||||
|
||||
int luaT_effectivetag (lua_State *L, const TObject *o) {
|
||||
lua_Type t = ttype(o);
|
||||
int luaT_tag (const TObject *o) {
|
||||
int t = ttype(o);
|
||||
switch (t) {
|
||||
case TAG_USERDATA: {
|
||||
int tag = o->value.ts->u.d.tag;
|
||||
return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
|
||||
}
|
||||
case TAG_TABLE: return o->value.a->htag;
|
||||
default: return t;
|
||||
case LUA_TUSERDATA: return tsvalue(o)->u.d.tag;
|
||||
case LUA_TTABLE: return hvalue(o)->htag;
|
||||
default: return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
|
||||
LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
|
||||
int e;
|
||||
e = luaI_checkevent(L, event, t);
|
||||
checktag(L, t);
|
||||
if (luaT_validevent(t, e))
|
||||
return luaT_getim(L, t,e);
|
||||
if (luaT_validevent(t, e) && luaT_gettm(L, t, e)) {
|
||||
clvalue(L->top) = luaT_gettm(L, t, e);
|
||||
ttype(L->top) = LUA_TFUNCTION;
|
||||
}
|
||||
else
|
||||
return &luaO_nilobject;
|
||||
ttype(L->top) = LUA_TNIL;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
|
||||
TObject temp;
|
||||
int e;
|
||||
e = luaI_checkevent(L, event, t);
|
||||
LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
|
||||
int e = luaI_checkevent(L, event, t);
|
||||
checktag(L, t);
|
||||
if (!luaT_validevent(t, e))
|
||||
luaL_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
|
||||
luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
|
||||
luaT_eventname[e], luaO_typenames[t],
|
||||
(t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
|
||||
: "");
|
||||
temp = *func;
|
||||
*func = *luaT_getim(L, t,e);
|
||||
*luaT_getim(L, t, e) = temp;
|
||||
}
|
||||
|
||||
|
||||
const char *luaT_travtagmethods (lua_State *L,
|
||||
int (*fn)(lua_State *, TObject *)) { /* ORDER IM */
|
||||
int e;
|
||||
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
if (fn(L, luaT_getim(L, t,e)))
|
||||
return luaT_eventname[e];
|
||||
(t == LUA_TTABLE || t == LUA_TUSERDATA) ?
|
||||
" with default tag" : "");
|
||||
switch (ttype(L->top - 1)) {
|
||||
case LUA_TNIL:
|
||||
luaT_gettm(L, t, e) = NULL;
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
luaT_gettm(L, t, e) = clvalue(L->top - 1);
|
||||
break;
|
||||
default:
|
||||
lua_error(L, "tag method must be a function (or nil)");
|
||||
}
|
||||
return NULL;
|
||||
L->top--;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltm.h,v 1.11 2000/03/20 19:14:54 roberto Exp roberto $
|
||||
** $Id: ltm.h,v 1.17 2000/10/05 12:14:08 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,45 +13,46 @@
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER IM"
|
||||
* grep "ORDER TM"
|
||||
*/
|
||||
typedef enum {
|
||||
IM_GETTABLE = 0,
|
||||
IM_SETTABLE,
|
||||
IM_INDEX,
|
||||
IM_GETGLOBAL,
|
||||
IM_SETGLOBAL,
|
||||
IM_ADD,
|
||||
IM_SUB,
|
||||
IM_MUL,
|
||||
IM_DIV,
|
||||
IM_POW,
|
||||
IM_UNM,
|
||||
IM_LT,
|
||||
IM_CONCAT,
|
||||
IM_GC,
|
||||
IM_FUNCTION,
|
||||
IM_N /* number of elements in the enum */
|
||||
} IMS;
|
||||
TM_GETTABLE = 0,
|
||||
TM_SETTABLE,
|
||||
TM_INDEX,
|
||||
TM_GETGLOBAL,
|
||||
TM_SETGLOBAL,
|
||||
TM_ADD,
|
||||
TM_SUB,
|
||||
TM_MUL,
|
||||
TM_DIV,
|
||||
TM_POW,
|
||||
TM_UNM,
|
||||
TM_LT,
|
||||
TM_CONCAT,
|
||||
TM_GC,
|
||||
TM_FUNCTION,
|
||||
TM_N /* number of elements in the enum */
|
||||
} TMS;
|
||||
|
||||
|
||||
struct IM {
|
||||
TObject int_method[IM_N];
|
||||
struct TM {
|
||||
Closure *method[TM_N];
|
||||
TString *collected; /* list of garbage-collected udata with this tag */
|
||||
};
|
||||
|
||||
|
||||
#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event])
|
||||
#define luaT_getimbyObj(L,o,e) (luaT_getim(L, luaT_effectivetag(L, o),(e)))
|
||||
#define luaT_gettm(L,tag,event) (L->TMtable[tag].method[event])
|
||||
#define luaT_gettmbyObj(L,o,e) (luaT_gettm((L),luaT_tag(o),(e)))
|
||||
|
||||
|
||||
#define validtag(t) (NUM_TAGS <= (t) && (t) <= L->last_tag)
|
||||
|
||||
extern const char *const luaT_eventname[];
|
||||
|
||||
|
||||
void luaT_init (lua_State *L);
|
||||
void luaT_realtag (lua_State *L, int tag);
|
||||
int luaT_effectivetag (lua_State *L, const TObject *o);
|
||||
void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func);
|
||||
const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event);
|
||||
const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *));
|
||||
int luaT_tag (const TObject *o);
|
||||
int luaT_validevent (int t, int e); /* used by compatibility module */
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.36 2000/03/30 17:19:48 roberto Exp roberto $
|
||||
** $Id: lua.c,v 1.54 2000/10/17 13:36:24 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,10 +11,14 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
static lua_State *L = NULL;
|
||||
|
||||
|
||||
#ifndef PROMPT
|
||||
#define PROMPT "> "
|
||||
#endif
|
||||
@@ -25,6 +29,16 @@
|
||||
static int isatty (int x) { return x==0; } /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** global options
|
||||
*/
|
||||
struct Options {
|
||||
int toclose;
|
||||
int stacksize;
|
||||
};
|
||||
|
||||
|
||||
typedef void (*handler)(int); /* type for signal actions */
|
||||
|
||||
static void laction (int i);
|
||||
@@ -34,16 +48,26 @@ static lua_Hook old_linehook = NULL;
|
||||
static lua_Hook old_callhook = NULL;
|
||||
|
||||
|
||||
static void userinit (void) {
|
||||
lua_baselibopen(L);
|
||||
lua_iolibopen(L);
|
||||
lua_strlibopen(L);
|
||||
lua_mathlibopen(L);
|
||||
lua_dblibopen(L);
|
||||
/* add your libraries here */
|
||||
}
|
||||
|
||||
|
||||
static handler lreset (void) {
|
||||
return signal(SIGINT, laction);
|
||||
}
|
||||
|
||||
|
||||
static void lstop (void) {
|
||||
lua_setlinehook(lua_state, old_linehook);
|
||||
lua_setcallhook(lua_state, old_callhook);
|
||||
lua_setlinehook(L, old_linehook);
|
||||
lua_setcallhook(L, old_callhook);
|
||||
lreset();
|
||||
lua_error("interrupted!");
|
||||
lua_error(L, "interrupted!");
|
||||
}
|
||||
|
||||
|
||||
@@ -51,16 +75,24 @@ static void laction (int i) {
|
||||
(void)i; /* to avoid warnings */
|
||||
signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
|
||||
terminate process (default action) */
|
||||
old_linehook = lua_setlinehook(lua_state, (lua_Hook)lstop);
|
||||
old_callhook = lua_setcallhook(lua_state, (lua_Hook)lstop);
|
||||
old_linehook = lua_setlinehook(L, (lua_Hook)lstop);
|
||||
old_callhook = lua_setcallhook(L, (lua_Hook)lstop);
|
||||
}
|
||||
|
||||
|
||||
static int ldo (int (*f)(lua_State *L, const char *), const char *name) {
|
||||
static int ldo (int (*f)(lua_State *l, const char *), const char *name) {
|
||||
int res;
|
||||
handler h = lreset();
|
||||
res = f(lua_state, name); /* dostring | dofile */
|
||||
int top = lua_gettop(L);
|
||||
res = f(L, name); /* dostring | dofile */
|
||||
lua_settop(L, top); /* remove eventual results */
|
||||
signal(SIGINT, h); /* restore old action */
|
||||
/* Lua gives no message in such cases, so lua.c provides one */
|
||||
if (res == LUA_ERRMEM) {
|
||||
fprintf(stderr, "lua: memory allocation error\n");
|
||||
}
|
||||
else if (res == LUA_ERRERR)
|
||||
fprintf(stderr, "lua: error in error message\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -69,12 +101,12 @@ static void print_message (void) {
|
||||
fprintf(stderr,
|
||||
"usage: lua [options]. Available options are:\n"
|
||||
" - execute stdin as a file\n"
|
||||
" -d turn debug on\n"
|
||||
" -c close Lua when exiting\n"
|
||||
" -e stat execute string `stat'\n"
|
||||
" -f name execute file `name' with remaining arguments in table `arg'\n"
|
||||
" -i enter interactive mode with prompt\n"
|
||||
" -q enter interactive mode without prompt\n"
|
||||
" -sNUM set stack size to NUM (must be first option)\n"
|
||||
" -sNUM set stack size to NUM (must be the first option)\n"
|
||||
" -v print version information\n"
|
||||
" a=b set global `a' to string `b'\n"
|
||||
" name execute file `name'\n"
|
||||
@@ -83,62 +115,73 @@ static void print_message (void) {
|
||||
|
||||
|
||||
static void print_version (void) {
|
||||
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
|
||||
printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
|
||||
}
|
||||
|
||||
|
||||
static void assign (char *arg) {
|
||||
char *eq = strchr(arg, '=');
|
||||
*eq = '\0'; /* spilt `arg' in two strings (name & value) */
|
||||
lua_pushstring(eq+1);
|
||||
lua_setglobal(arg);
|
||||
lua_pushstring(L, eq+1);
|
||||
lua_setglobal(L, arg);
|
||||
}
|
||||
|
||||
|
||||
static lua_Object getargs (char *argv[]) {
|
||||
lua_Object args = lua_createtable();
|
||||
static void getargs (char *argv[]) {
|
||||
int i;
|
||||
lua_newtable(L);
|
||||
for (i=0; argv[i]; i++) {
|
||||
/* arg[i] = argv[i] */
|
||||
lua_pushobject(args); lua_pushnumber(i);
|
||||
lua_pushstring(argv[i]); lua_settable();
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
/* arg.n = maximum index in table `arg' */
|
||||
lua_pushobject(args); lua_pushstring("n");
|
||||
lua_pushnumber(i-1); lua_settable();
|
||||
return args;
|
||||
lua_pushstring(L, "n");
|
||||
lua_pushnumber(L, i-1);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void l_getargs (void) {
|
||||
char **argv = (char **)lua_getuserdata(lua_getparam(1));
|
||||
lua_pushobject(getargs(argv));
|
||||
static int l_getargs (lua_State *l) {
|
||||
char **argv = (char **)lua_touserdata(l, -1);
|
||||
getargs(argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void file_input (const char *argv) {
|
||||
static int file_input (const char *argv) {
|
||||
int result = ldo(lua_dofile, argv);
|
||||
if (result) {
|
||||
if (result == 2) {
|
||||
if (result == LUA_ERRFILE) {
|
||||
fprintf(stderr, "lua: cannot execute file ");
|
||||
perror(argv);
|
||||
}
|
||||
exit(1);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
else
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/* maximum length of an input string */
|
||||
#ifndef MAXINPUT
|
||||
#define MAXINPUT BUFSIZ
|
||||
#endif
|
||||
|
||||
static void manual_input (int version, int prompt) {
|
||||
int cont = 1;
|
||||
if (version) print_version();
|
||||
while (cont) {
|
||||
char buffer[BUFSIZ];
|
||||
char buffer[MAXINPUT];
|
||||
int i = 0;
|
||||
lua_beginblock();
|
||||
if (prompt) {
|
||||
const char *s = lua_getstring(lua_getglobal("_PROMPT"));
|
||||
const char *s;
|
||||
lua_getglobal(L, "_PROMPT");
|
||||
s = lua_tostring(L, -1);
|
||||
if (!s) s = PROMPT;
|
||||
fputs(s, stdout);
|
||||
lua_pop(L, 1); /* remove global */
|
||||
}
|
||||
for(;;) {
|
||||
int c = getchar();
|
||||
@@ -151,7 +194,7 @@ static void manual_input (int version, int prompt) {
|
||||
buffer[i-1] = '\n';
|
||||
else break;
|
||||
}
|
||||
else if (i >= BUFSIZ-1) {
|
||||
else if (i >= MAXINPUT-1) {
|
||||
fprintf(stderr, "lua: input line too long\n");
|
||||
break;
|
||||
}
|
||||
@@ -159,96 +202,121 @@ static void manual_input (int version, int prompt) {
|
||||
}
|
||||
buffer[i] = '\0';
|
||||
ldo(lua_dostring, buffer);
|
||||
lua_endblock();
|
||||
lua_settop(L, 0); /* remove eventual results */
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
int i = 1;
|
||||
if (i < argc && argv[1][0] == '-' && argv[1][1] == 's') {
|
||||
int stacksize = atoi(&argv[1][2]);
|
||||
if (stacksize == 0) {
|
||||
fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]);
|
||||
exit(1);
|
||||
}
|
||||
i++;
|
||||
lua_state = lua_newstate("stack", stacksize, NULL);
|
||||
}
|
||||
else
|
||||
lua_state = lua_newstate(NULL);
|
||||
lua_userinit();
|
||||
lua_pushuserdata(argv);
|
||||
lua_pushcclosure(l_getargs, 1);
|
||||
lua_setglobal("getargs");
|
||||
if (i >= argc) { /* no other arguments? */
|
||||
static int handle_argv (char *argv[], struct Options *opt) {
|
||||
if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */
|
||||
if (*argv == NULL) { /* no more arguments? */
|
||||
if (isatty(0)) {
|
||||
manual_input(1, 1);
|
||||
}
|
||||
else
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
}
|
||||
else for (; i<argc; i++) {
|
||||
if (argv[i][0] == '-') { /* option? */
|
||||
switch (argv[i][1]) {
|
||||
case 0:
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
break;
|
||||
case 'i':
|
||||
manual_input(0, 1);
|
||||
break;
|
||||
case 'q':
|
||||
manual_input(0, 0);
|
||||
break;
|
||||
case 'd':
|
||||
lua_setdebug(lua_state, 1);
|
||||
if (i+1 >= argc) { /* last argument? */
|
||||
manual_input(1, 1);
|
||||
else { /* other arguments; loop over them */
|
||||
int i;
|
||||
for (i = 0; argv[i] != NULL; i++) {
|
||||
if (argv[i][0] != '-') { /* not an option? */
|
||||
if (strchr(argv[i], '='))
|
||||
assign(argv[i]);
|
||||
else
|
||||
if (file_input(argv[i]) != EXIT_SUCCESS)
|
||||
return EXIT_FAILURE; /* stop if file fails */
|
||||
}
|
||||
else switch (argv[i][1]) { /* option */
|
||||
case 0: {
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'v':
|
||||
print_version();
|
||||
break;
|
||||
case 'e':
|
||||
i++;
|
||||
if (i >= argc) {
|
||||
case 'i': {
|
||||
manual_input(0, 1);
|
||||
break;
|
||||
}
|
||||
case 'q': {
|
||||
manual_input(0, 0);
|
||||
break;
|
||||
}
|
||||
case 'c': {
|
||||
opt->toclose = 1;
|
||||
break;
|
||||
}
|
||||
case 'v': {
|
||||
print_version();
|
||||
break;
|
||||
}
|
||||
case 'e': {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (ldo(lua_dostring, argv[i]) != 0) {
|
||||
fprintf(stderr, "lua: error running argument `%.99s'\n", argv[i]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'f': {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
getargs(argv+i); /* collect remaining arguments */
|
||||
lua_setglobal(L, "arg");
|
||||
return file_input(argv[i]); /* stop scanning arguments */
|
||||
}
|
||||
case 's': {
|
||||
fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
default: {
|
||||
print_message();
|
||||
exit(1);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (ldo(lua_dostring, argv[i]) != 0) {
|
||||
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
i++;
|
||||
if (i >= argc) {
|
||||
print_message();
|
||||
exit(1);
|
||||
}
|
||||
lua_pushobject(getargs(argv+i)); /* collect remaining arguments */
|
||||
lua_setglobal("arg");
|
||||
file_input(argv[i]);
|
||||
goto endloop; /* stop scanning arguments */
|
||||
break;
|
||||
case 's':
|
||||
fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
|
||||
exit(1);
|
||||
default:
|
||||
print_message();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (strchr(argv[i], '='))
|
||||
assign(argv[i]);
|
||||
else
|
||||
file_input(argv[i]);
|
||||
}
|
||||
endloop:
|
||||
#ifdef DEBUG
|
||||
lua_close();
|
||||
#endif
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static void getstacksize (int argc, char *argv[], struct Options *opt) {
|
||||
if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 's') {
|
||||
int stacksize = atoi(&argv[1][2]);
|
||||
if (stacksize <= 0) {
|
||||
fprintf(stderr, "lua: invalid stack size ('%.20s')\n", &argv[1][2]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
opt->stacksize = stacksize;
|
||||
}
|
||||
else
|
||||
opt->stacksize = 0; /* no stack size */
|
||||
}
|
||||
|
||||
|
||||
static void register_getargs (char *argv[]) {
|
||||
lua_pushuserdata(L, argv);
|
||||
lua_pushcclosure(L, l_getargs, 1);
|
||||
lua_setglobal(L, "getargs");
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
struct Options opt;
|
||||
int status;
|
||||
opt.toclose = 0;
|
||||
getstacksize(argc, argv, &opt); /* handle option `-s' */
|
||||
L = lua_open(opt.stacksize); /* create state */
|
||||
userinit(); /* open libraries */
|
||||
register_getargs(argv); /* create `getargs' function */
|
||||
status = handle_argv(argv+1, &opt);
|
||||
if (opt.toclose)
|
||||
lua_close(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.47 2000/04/14 17:48:20 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.78 2000/10/30 12:38:50 roberto Exp roberto $
|
||||
** Lua - An Extensible Extension Language
|
||||
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
|
||||
** e-mail: lua@tecgraf.puc-rio.br
|
||||
@@ -11,106 +11,179 @@
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
#define LUA_VERSION "Lua 4.0 (alpha)"
|
||||
|
||||
/* definition of `size_t' */
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
/* mark for all API functions */
|
||||
#ifndef LUA_API
|
||||
#define LUA_API extern
|
||||
#endif
|
||||
|
||||
|
||||
#define LUA_VERSION "Lua 4.0"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2000 TeCGraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
|
||||
|
||||
/* name of global variable with error handler */
|
||||
#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
|
||||
|
||||
|
||||
/* pre-defined references */
|
||||
#define LUA_NOREF (-2)
|
||||
#define LUA_REFNIL (-1)
|
||||
#define LUA_REFREGISTRY 0
|
||||
|
||||
/* pre-defined tags */
|
||||
#define LUA_ANYTAG (-1)
|
||||
#define LUA_NOTAG (-2)
|
||||
|
||||
|
||||
/* option for multiple returns in lua_call */
|
||||
#define LUA_MULTRET (-1)
|
||||
|
||||
|
||||
/* minimum stack available for a C function */
|
||||
#define LUA_MINSTACK 20
|
||||
|
||||
|
||||
/* error codes for lua_do* */
|
||||
#define LUA_ERRRUN 1
|
||||
#define LUA_ERRFILE 2
|
||||
#define LUA_ERRSYNTAX 3
|
||||
#define LUA_ERRMEM 4
|
||||
#define LUA_ERRERR 5
|
||||
|
||||
|
||||
typedef struct lua_State lua_State;
|
||||
|
||||
typedef void (*lua_CFunction) (lua_State *L);
|
||||
typedef int (*lua_CFunction) (lua_State *L);
|
||||
|
||||
typedef struct TObject *lua_Object;
|
||||
/*
|
||||
** types returned by `lua_type'
|
||||
*/
|
||||
#define LUA_TNONE (-1)
|
||||
|
||||
#define LUA_NOOBJECT ((lua_Object)0)
|
||||
#define LUA_TUSERDATA 0
|
||||
#define LUA_TNIL 1
|
||||
#define LUA_TNUMBER 2
|
||||
#define LUA_TSTRING 3
|
||||
#define LUA_TTABLE 4
|
||||
#define LUA_TFUNCTION 5
|
||||
|
||||
|
||||
lua_State *lua_newstate (const char *s, ...);
|
||||
void lua_close (lua_State *L);
|
||||
|
||||
lua_Object lua_settagmethod (lua_State *L, int tag, const char *event);
|
||||
/* In: new method */
|
||||
lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event);
|
||||
|
||||
int lua_newtag (lua_State *L);
|
||||
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
||||
void lua_settag (lua_State *L, int tag); /* In: object */
|
||||
|
||||
void lua_error (lua_State *L, const char *s);
|
||||
int lua_dofile (lua_State *L, const char *filename);
|
||||
/* Out: returns */
|
||||
int lua_dostring (lua_State *L, const char *str);
|
||||
/* Out: returns */
|
||||
int lua_dobuffer (lua_State *L, const char *buff, int size,
|
||||
const char *name); /* Out: returns */
|
||||
int lua_callfunction (lua_State *L, lua_Object f);
|
||||
/* In: parameters; Out: returns */
|
||||
|
||||
void lua_beginblock (lua_State *L);
|
||||
void lua_endblock (lua_State *L);
|
||||
|
||||
lua_Object lua_lua2C (lua_State *L, int number);
|
||||
#define lua_getparam lua_lua2C
|
||||
#define lua_getresult lua_lua2C
|
||||
|
||||
const char *lua_type (lua_State *L, lua_Object obj);
|
||||
|
||||
int lua_isnil (lua_State *L, lua_Object obj);
|
||||
int lua_istable (lua_State *L, lua_Object obj);
|
||||
int lua_isuserdata (lua_State *L, lua_Object obj);
|
||||
int lua_iscfunction (lua_State *L, lua_Object obj);
|
||||
int lua_isnumber (lua_State *L, lua_Object obj);
|
||||
int lua_isstring (lua_State *L, lua_Object obj);
|
||||
int lua_isfunction (lua_State *L, lua_Object obj);
|
||||
|
||||
int lua_equal (lua_State *L, lua_Object o1, lua_Object o2);
|
||||
|
||||
double lua_getnumber (lua_State *L, lua_Object obj);
|
||||
const char *lua_getstring (lua_State *L, lua_Object obj);
|
||||
long lua_strlen (lua_State *L, lua_Object obj);
|
||||
lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj);
|
||||
void *lua_getuserdata (lua_State *L, lua_Object obj);
|
||||
/*
|
||||
** state manipulation
|
||||
*/
|
||||
LUA_API lua_State *lua_open (int stacksize);
|
||||
LUA_API void lua_close (lua_State *L);
|
||||
|
||||
|
||||
void lua_pushnil (lua_State *L);
|
||||
void lua_pushnumber (lua_State *L, double n);
|
||||
void lua_pushlstring (lua_State *L, const char *s, long len);
|
||||
void lua_pushstring (lua_State *L, const char *s);
|
||||
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
|
||||
void lua_pushusertag (lua_State *L, void *u, int tag);
|
||||
void lua_pushobject (lua_State *L, lua_Object obj);
|
||||
/*
|
||||
** basic stack manipulation
|
||||
*/
|
||||
LUA_API int lua_gettop (lua_State *L);
|
||||
LUA_API void lua_settop (lua_State *L, int index);
|
||||
LUA_API void lua_pushvalue (lua_State *L, int index);
|
||||
LUA_API void lua_remove (lua_State *L, int index);
|
||||
LUA_API void lua_insert (lua_State *L, int index);
|
||||
LUA_API int lua_stackspace (lua_State *L);
|
||||
|
||||
lua_Object lua_pop (lua_State *L);
|
||||
|
||||
lua_Object lua_getglobal (lua_State *L, const char *name);
|
||||
lua_Object lua_rawgetglobal (lua_State *L, const char *name);
|
||||
void lua_setglobal (lua_State *L, const char *name); /* In: value */
|
||||
void lua_rawsetglobal (lua_State *L, const char *name);/* In: value */
|
||||
/*
|
||||
** access functions (stack -> C)
|
||||
*/
|
||||
|
||||
void lua_settable (lua_State *L); /* In: table, index, value */
|
||||
void lua_rawsettable (lua_State *L); /* In: table, index, value */
|
||||
lua_Object lua_gettable (lua_State *L); /* In: table, index */
|
||||
lua_Object lua_rawgettable (lua_State *L); /* In: table, index */
|
||||
LUA_API int lua_type (lua_State *L, int index);
|
||||
LUA_API const char *lua_typename (lua_State *L, int t);
|
||||
LUA_API int lua_isnumber (lua_State *L, int index);
|
||||
LUA_API int lua_isstring (lua_State *L, int index);
|
||||
LUA_API int lua_iscfunction (lua_State *L, int index);
|
||||
LUA_API int lua_tag (lua_State *L, int index);
|
||||
|
||||
int lua_tag (lua_State *L, lua_Object obj);
|
||||
LUA_API int lua_equal (lua_State *L, int index1, int index2);
|
||||
LUA_API int lua_lessthan (lua_State *L, int index1, int index2);
|
||||
|
||||
const char *lua_nextvar (lua_State *L, const char *varname); /* Out: value */
|
||||
int lua_next (lua_State *L, lua_Object o, int i);
|
||||
/* Out: ref, value */
|
||||
LUA_API double lua_tonumber (lua_State *L, int index);
|
||||
LUA_API const char *lua_tostring (lua_State *L, int index);
|
||||
LUA_API size_t lua_strlen (lua_State *L, int index);
|
||||
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index);
|
||||
LUA_API void *lua_touserdata (lua_State *L, int index);
|
||||
LUA_API const void *lua_topointer (lua_State *L, int index);
|
||||
|
||||
int lua_ref (lua_State *L, int lock); /* In: value */
|
||||
lua_Object lua_getref (lua_State *L, int ref);
|
||||
void lua_unref (lua_State *L, int ref);
|
||||
|
||||
lua_Object lua_createtable (lua_State *L);
|
||||
/*
|
||||
** push functions (C -> stack)
|
||||
*/
|
||||
LUA_API void lua_pushnil (lua_State *L);
|
||||
LUA_API void lua_pushnumber (lua_State *L, double n);
|
||||
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len);
|
||||
LUA_API void lua_pushstring (lua_State *L, const char *s);
|
||||
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
|
||||
LUA_API void lua_pushusertag (lua_State *L, void *u, int tag);
|
||||
|
||||
long lua_collectgarbage (lua_State *L, long limit);
|
||||
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
LUA_API void lua_getglobal (lua_State *L, const char *name);
|
||||
LUA_API void lua_gettable (lua_State *L, int index);
|
||||
LUA_API void lua_rawget (lua_State *L, int index);
|
||||
LUA_API void lua_rawgeti (lua_State *L, int index, int n);
|
||||
LUA_API void lua_getglobals (lua_State *L);
|
||||
LUA_API void lua_gettagmethod (lua_State *L, int tag, const char *event);
|
||||
LUA_API int lua_getref (lua_State *L, int ref);
|
||||
LUA_API void lua_newtable (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
LUA_API void lua_setglobal (lua_State *L, const char *name);
|
||||
LUA_API void lua_settable (lua_State *L, int index);
|
||||
LUA_API void lua_rawset (lua_State *L, int index);
|
||||
LUA_API void lua_rawseti (lua_State *L, int index, int n);
|
||||
LUA_API void lua_setglobals (lua_State *L);
|
||||
LUA_API void lua_settagmethod (lua_State *L, int tag, const char *event);
|
||||
LUA_API int lua_ref (lua_State *L, int lock);
|
||||
|
||||
|
||||
/*
|
||||
** "do" functions (run Lua code)
|
||||
*/
|
||||
LUA_API int lua_call (lua_State *L, int nargs, int nresults);
|
||||
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults);
|
||||
LUA_API int lua_dofile (lua_State *L, const char *filename);
|
||||
LUA_API int lua_dostring (lua_State *L, const char *str);
|
||||
LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name);
|
||||
|
||||
/*
|
||||
** Garbage-collection functions
|
||||
*/
|
||||
LUA_API int lua_getgcthreshold (lua_State *L);
|
||||
LUA_API int lua_getgccount (lua_State *L);
|
||||
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
|
||||
|
||||
/*
|
||||
** miscellaneous functions
|
||||
*/
|
||||
LUA_API int lua_newtag (lua_State *L);
|
||||
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
||||
LUA_API void lua_settag (lua_State *L, int tag);
|
||||
|
||||
LUA_API void lua_error (lua_State *L, const char *s);
|
||||
|
||||
LUA_API void lua_unref (lua_State *L, int ref);
|
||||
|
||||
LUA_API int lua_next (lua_State *L, int index);
|
||||
LUA_API int lua_getn (lua_State *L, int index);
|
||||
|
||||
LUA_API void lua_concat (lua_State *L, int n);
|
||||
|
||||
LUA_API void *lua_newuserdata (lua_State *L, size_t size);
|
||||
|
||||
|
||||
/*
|
||||
@@ -119,107 +192,25 @@ long lua_collectgarbage (lua_State *L, long limit);
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#ifdef LUA_REENTRANT
|
||||
#define lua_pop(L,n) lua_settop(L, -(n)-1)
|
||||
|
||||
#define lua_call(L,name) lua_callfunction(L, lua_getglobal(L, name))
|
||||
#define lua_pushref(L,ref) lua_pushobject(L, lua_getref(L, ref))
|
||||
#define lua_refobject(L,o,l) (lua_pushobject(L, o), lua_ref(L, l))
|
||||
#define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n))
|
||||
#define lua_pushuserdata(L,u) lua_pushusertag(L, u, 0)
|
||||
#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
|
||||
#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
|
||||
|
||||
#else
|
||||
#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
|
||||
#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
|
||||
#define lua_isuserdata(L,n) (lua_type(L,n) == LUA_TUSERDATA)
|
||||
#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
|
||||
#define lua_isnull(L,n) (lua_type(L,n) == LUA_TNONE)
|
||||
|
||||
#define lua_call(name) lua_callfunction(lua_getglobal(name))
|
||||
#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
|
||||
#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))
|
||||
#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n))
|
||||
#define lua_pushuserdata(u) lua_pushusertag(u, 0)
|
||||
#define lua_pushcfunction(f) lua_pushcclosure(f, 0)
|
||||
#define lua_clonetag(t) lua_copytagmethods(lua_newtag(), (t))
|
||||
#define lua_getregistry(L) lua_getref(L, LUA_REFREGISTRY)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef LUA_REENTRANT
|
||||
/*
|
||||
** ===============================================================
|
||||
** Macros for single-state use
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
extern lua_State *lua_state;
|
||||
|
||||
#define lua_open() ((void)(lua_state?0:(lua_state=lua_newstate(0))))
|
||||
|
||||
#define lua_close() (lua_close)(lua_state)
|
||||
#define lua_settagmethod(tag,event) (lua_settagmethod)(lua_state, tag,event)
|
||||
#define lua_gettagmethod(tag,event) (lua_gettagmethod)(lua_state, tag,event)
|
||||
#define lua_newtag() (lua_newtag)(lua_state)
|
||||
#define lua_copytagmethods(tagto,tagfrom) \
|
||||
(lua_copytagmethods)(lua_state, tagto,tagfrom)
|
||||
#define lua_settag(tag) (lua_settag)(lua_state, tag)
|
||||
#define lua_error(s) (lua_error)(lua_state, s)
|
||||
#define lua_dofile(filename) (lua_dofile)(lua_state, filename)
|
||||
#define lua_dostring(str) (lua_dostring)(lua_state, str)
|
||||
#define lua_callfunction(f) (lua_callfunction)(lua_state, f)
|
||||
#define lua_beginblock() (lua_beginblock)(lua_state)
|
||||
#define lua_endblock() (lua_endblock)(lua_state)
|
||||
#define lua_lua2C(number) (lua_lua2C)(lua_state, number)
|
||||
#define lua_type(obj) (lua_type)(lua_state, obj)
|
||||
#define lua_isnil(obj) (lua_isnil)(lua_state, obj)
|
||||
#define lua_istable(obj) (lua_istable)(lua_state, obj)
|
||||
#define lua_isuserdata(obj) (lua_isuserdata)(lua_state, obj)
|
||||
#define lua_iscfunction(obj) (lua_iscfunction)(lua_state, obj)
|
||||
#define lua_isnumber(obj) (lua_isnumber)(lua_state, obj)
|
||||
#define lua_isstring(obj) (lua_isstring)(lua_state, obj)
|
||||
#define lua_isfunction(obj) (lua_isfunction)(lua_state, obj)
|
||||
#define lua_equal(o1,o2) (lua_equal)(lua_state, o1,o2)
|
||||
#define lua_getnumber(obj) (lua_getnumber)(lua_state, obj)
|
||||
#define lua_getstring(obj) (lua_getstring)(lua_state, obj)
|
||||
#define lua_strlen(obj) (lua_strlen)(lua_state, obj)
|
||||
#define lua_getcfunction(obj) (lua_getcfunction)(lua_state, obj)
|
||||
#define lua_getuserdata(obj) (lua_getuserdata)(lua_state, obj)
|
||||
#define lua_pushnil() (lua_pushnil)(lua_state)
|
||||
#define lua_pushnumber(n) (lua_pushnumber)(lua_state, n)
|
||||
#define lua_pushlstring(s,len) (lua_pushlstring)(lua_state, s,len)
|
||||
#define lua_pushstring(s) (lua_pushstring)(lua_state, s)
|
||||
#define lua_pushusertag(u,tag) (lua_pushusertag)(lua_state, u,tag)
|
||||
#define lua_pushobject(obj) (lua_pushobject)(lua_state, obj)
|
||||
#define lua_pop() (lua_pop)(lua_state)
|
||||
#define lua_getglobal(name) (lua_getglobal)(lua_state, name)
|
||||
#define lua_rawgetglobal(name) (lua_rawgetglobal)(lua_state, name)
|
||||
#define lua_setglobal(name) (lua_setglobal)(lua_state, name)
|
||||
#define lua_rawsetglobal(name) (lua_rawsetglobal)(lua_state, name)
|
||||
#define lua_settable() (lua_settable)(lua_state)
|
||||
#define lua_rawsettable() (lua_rawsettable)(lua_state)
|
||||
#define lua_gettable() (lua_gettable)(lua_state)
|
||||
#define lua_rawgettable() (lua_rawgettable)(lua_state)
|
||||
#define lua_tag(obj) (lua_tag)(lua_state, obj)
|
||||
#define lua_nextvar(varname) (lua_nextvar)(lua_state, varname)
|
||||
#define lua_next(o,i) (lua_next)(lua_state, o,i)
|
||||
#define lua_ref(lock) (lua_ref)(lua_state, lock)
|
||||
#define lua_getref(ref) (lua_getref)(lua_state, ref)
|
||||
#define lua_unref(ref) (lua_unref)(lua_state, ref)
|
||||
#define lua_createtable() (lua_createtable)(lua_state)
|
||||
#define lua_collectgarbage(limit) (lua_collectgarbage)(lua_state, limit)
|
||||
/*
|
||||
** the following typecast is a little dirty, but we know of no other
|
||||
** way to keep compatibility with old definition of `lua_CFunction'
|
||||
*/
|
||||
#define lua_pushcclosure(fn,n) \
|
||||
(lua_pushcclosure)(lua_state, (lua_CFunction)(fn), n)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (C) 1994-2000 TeCGraf, PUC-Rio. All rights reserved.
|
||||
*
|
||||
@@ -254,3 +245,4 @@ extern lua_State *lua_state;
|
||||
*
|
||||
* This implementation contains no third-party code.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
+14
-22
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: luadebug.h,v 1.9 2000/01/19 12:00:45 roberto Exp roberto $
|
||||
** $Id: luadebug.h,v 1.16 2000/10/20 16:39:03 roberto Exp roberto $
|
||||
** Debugging API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -17,37 +17,29 @@ typedef struct lua_Localvar lua_Localvar;
|
||||
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
||||
|
||||
|
||||
int lua_getstack (lua_State *L, int level, lua_Debug *ar);
|
||||
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
|
||||
int lua_getlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v);
|
||||
int lua_setlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v);
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
|
||||
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
|
||||
LUA_API 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);
|
||||
|
||||
int lua_setdebug (lua_State *L, int debug);
|
||||
|
||||
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
|
||||
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
|
||||
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
|
||||
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
|
||||
|
||||
|
||||
#define LUA_IDSIZE 60
|
||||
|
||||
struct lua_Debug {
|
||||
const char *event; /* `call', `return' */
|
||||
const char *source; /* (S) */
|
||||
int linedefined; /* (S) */
|
||||
const char *what; /* (S) `Lua' function, `C' function, Lua `main' */
|
||||
int currentline; /* (l) */
|
||||
const char *name; /* (n) */
|
||||
const char *namewhat; /* (n) global, tag method, local, field */
|
||||
const char *namewhat; /* (n) `global', `tag method', `local', `field' */
|
||||
int nups; /* (u) number of upvalues */
|
||||
lua_Object func; /* (f) function being executed */
|
||||
int linedefined; /* (S) */
|
||||
const char *what; /* (S) `Lua' function, `C' function, Lua `main' */
|
||||
const char *source; /* (S) */
|
||||
char short_src[LUA_IDSIZE]; /* (S) */
|
||||
/* private part */
|
||||
lua_Object _func; /* active function */
|
||||
};
|
||||
|
||||
|
||||
struct lua_Localvar {
|
||||
int index;
|
||||
const char *name;
|
||||
lua_Object value;
|
||||
struct lua_TObject *_func; /* active function */
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lualib.h,v 1.7 1999/08/16 20:52:00 roberto Exp roberto $
|
||||
** $Id: lualib.h,v 1.13 2000/10/20 16:39:03 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,32 +10,21 @@
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
void lua_iolibopen (lua_State *L);
|
||||
void lua_strlibopen (lua_State *L);
|
||||
void lua_mathlibopen (lua_State *L);
|
||||
void lua_dblibopen (lua_State *L);
|
||||
|
||||
|
||||
void lua_userinit (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** Macros for single-state use
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#ifndef LUA_REENTRANT
|
||||
|
||||
#define lua_iolibopen() (lua_iolibopen)(lua_state)
|
||||
#define lua_strlibopen() (lua_strlibopen)(lua_state)
|
||||
#define lua_mathlibopen() (lua_mathlibopen)(lua_state)
|
||||
#define lua_dblibopen() (lua_dblibopen)(lua_state)
|
||||
#define lua_userinit() (lua_userinit)(lua_state)
|
||||
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API extern
|
||||
#endif
|
||||
|
||||
|
||||
#define LUA_ALERT "_ALERT"
|
||||
|
||||
LUALIB_API void lua_baselibopen (lua_State *L);
|
||||
LUALIB_API void lua_iolibopen (lua_State *L);
|
||||
LUALIB_API void lua_strlibopen (lua_State *L);
|
||||
LUALIB_API void lua_mathlibopen (lua_State *L);
|
||||
LUALIB_API void lua_dblibopen (lua_State *L);
|
||||
|
||||
|
||||
|
||||
/* Auxiliary functions (private) */
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.28 2000/04/24 19:32:58 lhf Exp $
|
||||
** $Id: lundump.c,v 1.33 2000/10/31 16:57:23 lhf Exp $
|
||||
** load bytecodes from files
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,19 +7,15 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstring.h"
|
||||
#include "lundump.h"
|
||||
|
||||
#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
|
||||
#define LoadByte ezgetc
|
||||
|
||||
static const char* ZNAME(ZIO* Z)
|
||||
static const char* ZNAME (ZIO* Z)
|
||||
{
|
||||
const char* s=zname(Z);
|
||||
return (*s=='@') ? s+1 : s;
|
||||
@@ -27,7 +23,7 @@ static const char* ZNAME(ZIO* Z)
|
||||
|
||||
static void unexpectedEOZ (lua_State* L, ZIO* Z)
|
||||
{
|
||||
luaL_verror(L,"unexpected end of file in `%.255s'",ZNAME(Z));
|
||||
luaO_verror(L,"unexpected end of file in `%.99s'",ZNAME(Z));
|
||||
}
|
||||
|
||||
static int ezgetc (lua_State* L, ZIO* Z)
|
||||
@@ -43,152 +39,129 @@ static void ezread (lua_State* L, ZIO* Z, void* b, int n)
|
||||
if (r!=0) unexpectedEOZ(L,Z);
|
||||
}
|
||||
|
||||
static unsigned int LoadWord (lua_State* L, ZIO* Z)
|
||||
static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
|
||||
{
|
||||
unsigned int hi=ezgetc(L,Z);
|
||||
unsigned int lo=ezgetc(L,Z);
|
||||
return (hi<<8)|lo;
|
||||
if (swap)
|
||||
{
|
||||
char *p=(char *) b+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(L,Z);
|
||||
}
|
||||
else
|
||||
ezread(L,Z,b,size);
|
||||
}
|
||||
|
||||
static unsigned long LoadLong (lua_State* L, ZIO* Z)
|
||||
static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
|
||||
{
|
||||
unsigned long hi=LoadWord(L,Z);
|
||||
unsigned long lo=LoadWord(L,Z);
|
||||
return (hi<<16)|lo;
|
||||
if (swap)
|
||||
{
|
||||
char *q=(char *) b;
|
||||
while (m--)
|
||||
{
|
||||
char *p=q+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(L,Z);
|
||||
q+=size;
|
||||
}
|
||||
}
|
||||
else
|
||||
ezread(L,Z,b,m*size);
|
||||
}
|
||||
|
||||
static Number LoadNumber (lua_State* L, ZIO* Z)
|
||||
static int LoadInt (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
char b[256];
|
||||
int size=ezgetc(L,Z);
|
||||
LoadBlock(L,b,size,Z);
|
||||
b[size]=0;
|
||||
return luaU_str2d(L,b,ZNAME(Z));
|
||||
int x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
return x;
|
||||
}
|
||||
|
||||
static int LoadInt (lua_State* L, ZIO* Z, const char* message)
|
||||
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
unsigned long l=LoadLong(L,Z);
|
||||
unsigned int i=l;
|
||||
if (i!=l) luaL_verror(L,"%s in `%.255s';\n"
|
||||
" read %lu; expected %u",message,ZNAME(Z),l,-1);
|
||||
return i;
|
||||
size_t x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
return x;
|
||||
}
|
||||
|
||||
static TString* LoadString (lua_State* L, ZIO* Z)
|
||||
static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
long size=LoadLong(L,Z);
|
||||
Number x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
return x;
|
||||
}
|
||||
|
||||
static TString* LoadString (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
size_t size=LoadSize(L,Z,swap);
|
||||
if (size==0)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
char* s=luaL_openspace(L,size);
|
||||
LoadBlock(L,s,size,Z);
|
||||
char* s=luaO_openspace(L,size);
|
||||
LoadBlock(L,s,size,Z,0);
|
||||
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
|
||||
}
|
||||
}
|
||||
|
||||
static void SwapCode (lua_State* L, Instruction* code, int size, ZIO* Z)
|
||||
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
{
|
||||
unsigned char* p;
|
||||
int c;
|
||||
if (sizeof(Instruction)==4)
|
||||
while (size--)
|
||||
{
|
||||
p=(unsigned char *) code++;
|
||||
c=p[0]; p[0]=p[3]; p[3]=c;
|
||||
c=p[1]; p[1]=p[2]; p[2]=c;
|
||||
}
|
||||
else if (sizeof(Instruction)==2)
|
||||
while (size--)
|
||||
{
|
||||
p=(unsigned char *) code++;
|
||||
c=p[0]; p[0]=p[1]; p[1]=c;
|
||||
}
|
||||
else
|
||||
luaL_verror(L,"cannot swap code with %d-byte instructions in `%.255s'",
|
||||
(int)sizeof(Instruction),ZNAME(Z));
|
||||
}
|
||||
|
||||
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z)
|
||||
{
|
||||
int size=LoadInt(L,Z,"code too long");
|
||||
Instruction t=0,tt=TEST_CODE;
|
||||
int size=LoadInt(L,Z,swap);
|
||||
tf->code=luaM_newvector(L,size,Instruction);
|
||||
LoadBlock(L,tf->code,size*sizeof(*tf->code),Z);
|
||||
if (tf->code[size-1]!=OP_END) luaL_verror(L,"bad code in `%.255s'",ZNAME(Z));
|
||||
LoadBlock(L,&t,sizeof(t),Z);
|
||||
if (t!=tt)
|
||||
{
|
||||
Instruction ot=t;
|
||||
SwapCode(L,&t,1,Z);
|
||||
if (t!=tt) luaL_verror(L,"cannot swap code in `%.255s';\n"
|
||||
" read 0x%08X; swapped to 0x%08X; expected 0x%08X",
|
||||
ZNAME(Z),(unsigned long)ot,(unsigned long)t,(unsigned long)tt);
|
||||
SwapCode(L,tf->code,size,Z);
|
||||
}
|
||||
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
|
||||
if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.99s'",ZNAME(Z));
|
||||
luaF_protook(L,tf,size);
|
||||
}
|
||||
|
||||
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z)
|
||||
{
|
||||
int i,n=LoadInt(L,Z,"too many locals");
|
||||
if (n==0) return;
|
||||
tf->locvars=luaM_newvector(L,n+1,LocVar);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->locvars[i].line=LoadInt(L,Z,"too many lines");
|
||||
tf->locvars[i].varname=LoadString(L,Z);
|
||||
}
|
||||
tf->locvars[i].line=-1; /* flag end of vector */
|
||||
tf->locvars[i].varname=NULL;
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int native);
|
||||
|
||||
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int native)
|
||||
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
{
|
||||
int i,n;
|
||||
tf->nkstr=n=LoadInt(L,Z,"too many strings");
|
||||
if (n>0)
|
||||
tf->nlocvars=n=LoadInt(L,Z,swap);
|
||||
tf->locvars=luaM_newvector(L,n,LocVar);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->kstr=luaM_newvector(L,n,TString*);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
TString* s=LoadString(L,Z);
|
||||
int isglobal=LoadByte(L,Z);
|
||||
if (isglobal) luaS_assertglobal(L,s);
|
||||
tf->kstr[i]=s;
|
||||
}
|
||||
}
|
||||
tf->nknum=n=LoadInt(L,Z,"too many numbers");
|
||||
if (n>0)
|
||||
{
|
||||
tf->knum=luaM_newvector(L,n,Number);
|
||||
if (native)
|
||||
LoadBlock(L,tf->knum,n*sizeof(*tf->knum),Z);
|
||||
else
|
||||
for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z);
|
||||
}
|
||||
tf->nkproto=n=LoadInt(L,Z,"too many functions");
|
||||
if (n>0)
|
||||
{
|
||||
tf->kproto=luaM_newvector(L,n,Proto*);
|
||||
for (i=0; i<n; i++) tf->kproto[i]=LoadFunction(L,Z,native);
|
||||
tf->locvars[i].varname=LoadString(L,Z,swap);
|
||||
tf->locvars[i].startpc=LoadInt(L,Z,swap);
|
||||
tf->locvars[i].endpc=LoadInt(L,Z,swap);
|
||||
}
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int native)
|
||||
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
{
|
||||
int n;
|
||||
tf->nlineinfo=n=LoadInt(L,Z,swap);
|
||||
tf->lineinfo=luaM_newvector(L,n,int);
|
||||
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
|
||||
|
||||
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
{
|
||||
int i,n;
|
||||
tf->nkstr=n=LoadInt(L,Z,swap);
|
||||
tf->kstr=luaM_newvector(L,n,TString*);
|
||||
for (i=0; i<n; i++)
|
||||
tf->kstr[i]=LoadString(L,Z,swap);
|
||||
tf->nknum=n=LoadInt(L,Z,swap);
|
||||
tf->knum=luaM_newvector(L,n,Number);
|
||||
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
|
||||
tf->nkproto=n=LoadInt(L,Z,swap);
|
||||
tf->kproto=luaM_newvector(L,n,Proto*);
|
||||
for (i=0; i<n; i++)
|
||||
tf->kproto[i]=LoadFunction(L,Z,swap);
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
Proto* tf=luaF_newproto(L);
|
||||
tf->source=LoadString(L,Z);
|
||||
if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
|
||||
tf->lineDefined=LoadInt(L,Z,"lineDefined too large");
|
||||
tf->numparams=LoadInt(L,Z,"too many parameters");
|
||||
tf->source=LoadString(L,Z,swap);
|
||||
tf->lineDefined=LoadInt(L,Z,swap);
|
||||
tf->numparams=LoadInt(L,Z,swap);
|
||||
tf->is_vararg=LoadByte(L,Z);
|
||||
tf->maxstacksize=LoadInt(L,Z,"too much stack needed");
|
||||
LoadCode(L,tf,Z);
|
||||
LoadLocals(L,tf,Z);
|
||||
LoadConstants(L,tf,Z,native);
|
||||
tf->maxstacksize=LoadInt(L,Z,swap);
|
||||
LoadLocals(L,tf,Z,swap);
|
||||
LoadLines(L,tf,Z,swap);
|
||||
LoadConstants(L,tf,Z,swap);
|
||||
LoadCode(L,tf,Z,swap);
|
||||
return tf;
|
||||
}
|
||||
|
||||
@@ -197,54 +170,47 @@ static void LoadSignature (lua_State* L, ZIO* Z)
|
||||
const char* s=SIGNATURE;
|
||||
while (*s!=0 && ezgetc(L,Z)==*s)
|
||||
++s;
|
||||
if (*s!=0) luaL_verror(L,"bad signature in `%.255s'",ZNAME(Z));
|
||||
if (*s!=0) luaO_verror(L,"bad signature in `%.99s'",ZNAME(Z));
|
||||
}
|
||||
|
||||
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
|
||||
{
|
||||
int r=ezgetc(L,Z);
|
||||
if (r!=s)
|
||||
luaO_verror(L,"virtual machine mismatch in `%.99s':\n"
|
||||
" %.20s is %d but read %d",ZNAME(Z),what,s,r);
|
||||
}
|
||||
|
||||
#define TESTSIZE(s) TestSize(L,s,#s,Z)
|
||||
#define V(v) v/16,v%16
|
||||
|
||||
static int LoadHeader (lua_State* L, ZIO* Z)
|
||||
{
|
||||
int version,sizeofR,native;
|
||||
int version,swap;
|
||||
Number f=0,tf=TEST_NUMBER;
|
||||
LoadSignature(L,Z);
|
||||
version=ezgetc(L,Z);
|
||||
if (version>VERSION)
|
||||
luaL_verror(L,"`%.255s' too new:\n"
|
||||
luaO_verror(L,"`%.99s' too new:\n"
|
||||
" read version %d.%d; expected at most %d.%d",
|
||||
ZNAME(Z),V(version),V(VERSION));
|
||||
if (version<VERSION0) /* check last major change */
|
||||
luaL_verror(L,"`%.255s' too old:\n"
|
||||
luaO_verror(L,"`%.99s' too old:\n"
|
||||
" read version %d.%d; expected at least %d.%d",
|
||||
ZNAME(Z),V(version),V(VERSION));
|
||||
{
|
||||
int I=ezgetc(L,Z);
|
||||
int i=ezgetc(L,Z);
|
||||
int op=ezgetc(L,Z);
|
||||
int b=ezgetc(L,Z);
|
||||
if (I!=sizeof(Instruction) || i!=SIZE_INSTRUCTION || op!=SIZE_OP || b!=SIZE_B)
|
||||
luaL_verror(L,"virtual machine mismatch in `%.255s':\n"
|
||||
" read %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments;\n"
|
||||
" expected %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments",
|
||||
ZNAME(Z),i,I,op,b,SIZE_INSTRUCTION,(int)sizeof(Instruction),SIZE_OP,SIZE_B);
|
||||
}
|
||||
sizeofR=ezgetc(L,Z);
|
||||
native=(sizeofR!=0);
|
||||
if (native) /* test number representation */
|
||||
{
|
||||
if (sizeofR!=sizeof(Number))
|
||||
luaL_verror(L,"native number size mismatch in `%.255s':\n"
|
||||
" read %d; expected %d",
|
||||
ZNAME(Z),sizeofR,(int)sizeof(Number));
|
||||
else
|
||||
{
|
||||
Number f=0,tf=TEST_NUMBER;
|
||||
LoadBlock(L,&f,sizeof(f),Z);
|
||||
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
||||
luaL_verror(L,"unknown number format in `%.255s':\n"
|
||||
" read " NUMBER_FMT "; expected " NUMBER_FMT,
|
||||
ZNAME(Z),f,tf);
|
||||
}
|
||||
}
|
||||
return native;
|
||||
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
|
||||
TESTSIZE(sizeof(int));
|
||||
TESTSIZE(sizeof(size_t));
|
||||
TESTSIZE(sizeof(Instruction));
|
||||
TESTSIZE(SIZE_INSTRUCTION);
|
||||
TESTSIZE(SIZE_OP);
|
||||
TESTSIZE(SIZE_B);
|
||||
TESTSIZE(sizeof(Number));
|
||||
f=LoadNumber(L,Z,swap);
|
||||
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
||||
luaO_verror(L,"unknown number format in `%.99s':\n"
|
||||
" read " NUMBER_FMT "; expected " NUMBER_FMT, ZNAME(Z),f,tf);
|
||||
return swap;
|
||||
}
|
||||
|
||||
static Proto* LoadChunk (lua_State* L, ZIO* Z)
|
||||
@@ -256,23 +222,23 @@ static Proto* LoadChunk (lua_State* L, ZIO* Z)
|
||||
** load one chunk from a file or buffer
|
||||
** return main if ok and NULL at EOF
|
||||
*/
|
||||
Proto* luaU_undump1 (lua_State* L, ZIO* Z)
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z)
|
||||
{
|
||||
Proto* tf=NULL;
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_CHUNK)
|
||||
return LoadChunk(L,Z);
|
||||
else if (c!=EOZ)
|
||||
luaL_verror(L,"`%.255s' is not a Lua binary file",ZNAME(Z));
|
||||
return NULL;
|
||||
tf=LoadChunk(L,Z);
|
||||
c=zgetc(Z);
|
||||
if (c!=EOZ)
|
||||
luaO_verror(L,"`%.99s' apparently contains more than one chunk",ZNAME(Z));
|
||||
return tf;
|
||||
}
|
||||
|
||||
/*
|
||||
* convert number from text
|
||||
** find byte order
|
||||
*/
|
||||
double luaU_str2d (lua_State* L, const char* b, const char* where)
|
||||
int luaU_endianess (void)
|
||||
{
|
||||
double x;
|
||||
if (!luaO_str2d(b,&x))
|
||||
luaL_verror(L,"cannot convert number `%.255s' in `%.255s'",b,where);
|
||||
return x;
|
||||
int x=1;
|
||||
return *(char*)&x;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.19 2000/04/24 17:32:29 lhf Exp $
|
||||
** $Id: lundump.h,v 1.21 2000/10/31 16:57:23 lhf Exp $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,10 +11,10 @@
|
||||
#include "lzio.h"
|
||||
|
||||
/* load one chunk */
|
||||
Proto* luaU_undump1 (lua_State* L, ZIO* Z);
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z);
|
||||
|
||||
/* convert number from text */
|
||||
double luaU_str2d (lua_State* L, const char* b, const char* where);
|
||||
/* find byte order */
|
||||
int luaU_endianess (void);
|
||||
|
||||
/* definitions for headers of binary files */
|
||||
#define VERSION 0x40 /* last format change was in 4.0 */
|
||||
@@ -23,21 +23,13 @@ double luaU_str2d (lua_State* L, const char* b, const char* where);
|
||||
#define SIGNATURE "Lua" /* ...followed by this signature */
|
||||
|
||||
/* formats for error messages */
|
||||
#define xSOURCE "<%d:%.255s>"
|
||||
#define SOURCE "<%.255s:%d>"
|
||||
#define IN " in %p " SOURCE
|
||||
#define INLOC tf,tf->source->str,tf->lineDefined
|
||||
|
||||
/* format for numbers in listings and error messages */
|
||||
#ifndef NUMBER_FMT
|
||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||
#endif
|
||||
#define SOURCE_FMT "<%d:%.99s>"
|
||||
#define SOURCE tf->lineDefined,tf->source->str
|
||||
#define IN_FMT " in %p " SOURCE_FMT
|
||||
#define IN tf,SOURCE
|
||||
|
||||
/* a multiple of PI for testing native format */
|
||||
/* multiplying by 1E8 gives non-trivial integer values */
|
||||
#define TEST_NUMBER 3.14159265358979323846E8
|
||||
|
||||
/* something for testing byte order in instructions */
|
||||
#define TEST_CODE 0x01020304
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 1.20 2000/03/29 20:19:20 roberto Exp roberto $
|
||||
** $Id: lvm.h,v 1.26 2000/09/05 19:33:32 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,22 +13,20 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
#define tonumber(o) ((ttype(o) != TAG_NUMBER) && (luaV_tonumber(o) != 0))
|
||||
#define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0))
|
||||
#define tonumber(o) ((ttype(o) != LUA_TNUMBER) && (luaV_tonumber(o) != 0))
|
||||
#define tostring(L,o) ((ttype(o) != LUA_TSTRING) && (luaV_tostring(L, o) != 0))
|
||||
|
||||
|
||||
void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab);
|
||||
int luaV_tonumber (TObject *obj);
|
||||
int luaV_tostring (lua_State *L, TObject *obj);
|
||||
void luaV_setn (lua_State *L, Hash *t, int val);
|
||||
void luaV_gettable (lua_State *L, StkId top);
|
||||
void luaV_settable (lua_State *L, StkId t, StkId top);
|
||||
void luaV_rawsettable (lua_State *L, StkId t);
|
||||
void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top);
|
||||
void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top);
|
||||
const TObject *luaV_gettable (lua_State *L, StkId t);
|
||||
void luaV_settable (lua_State *L, StkId t, StkId key);
|
||||
const TObject *luaV_getglobal (lua_State *L, TString *s);
|
||||
void luaV_setglobal (lua_State *L, TString *s);
|
||||
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base);
|
||||
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems);
|
||||
void luaV_Lclosure (lua_State *L, Proto *l, int nelems);
|
||||
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top);
|
||||
void luaV_strconc (lua_State *L, int total, StkId top);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.10 2000/02/08 16:39:42 roberto Exp roberto $
|
||||
** $Id: lzio.c,v 1.12 2000/05/24 13:54:49 roberto Exp roberto $
|
||||
** a generic input stream interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
@@ -21,7 +23,7 @@ static int zmfilbuf (ZIO* z) {
|
||||
}
|
||||
|
||||
|
||||
ZIO* zmopen (ZIO* z, const char* b, int size, const char *name) {
|
||||
ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name) {
|
||||
if (b==NULL) return NULL;
|
||||
z->n = size;
|
||||
z->p = (const unsigned char *)b;
|
||||
@@ -41,7 +43,7 @@ ZIO* zsopen (ZIO* z, const char* s, const char *name) {
|
||||
/* -------------------------------------------------------------- FILEs --- */
|
||||
|
||||
static int zffilbuf (ZIO* z) {
|
||||
int n;
|
||||
size_t n;
|
||||
if (feof((FILE *)z->u)) return EOZ;
|
||||
n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u);
|
||||
if (n==0) return EOZ;
|
||||
@@ -63,9 +65,9 @@ ZIO* zFopen (ZIO* z, FILE* f, const char *name) {
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- read --- */
|
||||
int zread (ZIO *z, void *b, int n) {
|
||||
size_t zread (ZIO *z, void *b, size_t n) {
|
||||
while (n) {
|
||||
int m;
|
||||
size_t m;
|
||||
if (z->n == 0) {
|
||||
if (z->filbuf(z) == EOZ)
|
||||
return n; /* return number of missing bytes */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.h,v 1.4 1998/01/09 14:57:43 roberto Exp roberto $
|
||||
** $Id: lzio.h,v 1.6 2000/05/24 13:54:49 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -24,21 +24,24 @@ typedef struct zio ZIO;
|
||||
|
||||
ZIO* zFopen (ZIO* z, FILE* f, const char *name); /* open FILEs */
|
||||
ZIO* zsopen (ZIO* z, const char* s, const char *name); /* string */
|
||||
ZIO* zmopen (ZIO* z, const char* b, int size, const char *name); /* memory */
|
||||
ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name); /* memory */
|
||||
|
||||
int zread (ZIO* z, void* b, int n); /* read next n bytes */
|
||||
size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
|
||||
|
||||
#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||
#define zgetc(z) (((z)->n--)>0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||
#define zungetc(z) (++(z)->n,--(z)->p)
|
||||
#define zname(z) ((z)->name)
|
||||
|
||||
|
||||
|
||||
/* --------- Private Part ------------------ */
|
||||
|
||||
#ifndef ZBSIZE
|
||||
#define ZBSIZE 256 /* buffer size */
|
||||
#endif
|
||||
|
||||
struct zio {
|
||||
int n; /* bytes still unread */
|
||||
size_t n; /* bytes still unread */
|
||||
const unsigned char* p; /* current position in buffer */
|
||||
int (*filbuf)(ZIO* z);
|
||||
void* u; /* additional data */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#
|
||||
## $Id: makefile,v 1.24 2000/04/14 17:52:09 roberto Exp roberto $
|
||||
## $Id: makefile,v 1.29 2000/10/30 17:50:00 roberto Exp roberto $
|
||||
## Makefile
|
||||
## See Copyright Notice in lua.h
|
||||
#
|
||||
@@ -17,17 +17,32 @@
|
||||
#
|
||||
# define LUA_NUM_TYPE if you need numbers to be different from double
|
||||
# (for instance, -DLUA_NUM_TYPE=float)
|
||||
# you may need to adapat the code, too.
|
||||
#
|
||||
# define LUA_COMPAT_READPATTERN if you need read patterns
|
||||
# (only for compatibility with previous versions)
|
||||
# define LUA_COMPAT_ARGRET for compatibility in the way function results
|
||||
# are passed as arguments
|
||||
# define LUA_DEPRECATEDFUNCS to include obsolete functions
|
||||
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||
#CONFIG = -DOLD_ANSI -DDEBUG -DLUA_COMPAT_READPATTERN
|
||||
#CONFIG = -DOLD_ANSI -DLUA_DEBUG -DLUA_COMPAT_READPATTERN -DLUA_COMPAT_ARGRET
|
||||
# -DLUA_DEPRECATEDFUNCS
|
||||
|
||||
|
||||
# Compilation parameters
|
||||
CC = gcc
|
||||
CWARNS = -Wall -W -Wmissing-prototypes -Wshadow -pedantic -Wpointer-arith -Wcast-align -Waggregate-return -Wcast-qual -Wnested-externs -Wwrite-strings
|
||||
CWARNS = -Wall -W -pedantic \
|
||||
-Waggregate-return \
|
||||
-Wcast-align \
|
||||
-Wmissing-prototypes \
|
||||
-Wnested-externs \
|
||||
-Wpointer-arith \
|
||||
-Wshadow \
|
||||
-Wwrite-strings
|
||||
# -Wcast-qual
|
||||
# -Wtraditional
|
||||
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O2
|
||||
|
||||
|
||||
@@ -42,10 +57,7 @@ ARFLAGS = rvl
|
||||
# Aplication modules
|
||||
LUAOBJS = \
|
||||
lstate.o \
|
||||
lref.o \
|
||||
lapi.o \
|
||||
lauxlib.o \
|
||||
lbuiltin.o \
|
||||
lmem.o \
|
||||
lstring.o \
|
||||
ltable.o \
|
||||
@@ -53,7 +65,6 @@ LUAOBJS = \
|
||||
lvm.o \
|
||||
ldo.o \
|
||||
lobject.o \
|
||||
lbuffer.o \
|
||||
lfunc.o \
|
||||
lgc.o \
|
||||
lcode.o \
|
||||
@@ -65,11 +76,12 @@ LUAOBJS = \
|
||||
ltests.o
|
||||
|
||||
LIBOBJS = \
|
||||
lauxlib.o \
|
||||
lbaselib.o \
|
||||
liolib.o \
|
||||
lmathlib.o \
|
||||
lstrlib.o \
|
||||
ldblib.o \
|
||||
linit.o
|
||||
ldblib.o
|
||||
|
||||
|
||||
lua : lua.o liblua.a liblualib.a
|
||||
@@ -99,56 +111,50 @@ clear :
|
||||
co $(CO_OPTIONS) $@
|
||||
|
||||
|
||||
lapi.o: lapi.c lapi.h lobject.h llimits.h lua.h lauxlib.h ldo.h \
|
||||
lstate.h luadebug.h lfunc.h lgc.h lmem.h lref.h lstring.h ltable.h \
|
||||
ltm.h lvm.h
|
||||
lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h
|
||||
lbuffer.o: lbuffer.c lauxlib.h lua.h lmem.h lstate.h lobject.h \
|
||||
llimits.h luadebug.h
|
||||
lbuiltin.o: lbuiltin.c lapi.h lobject.h llimits.h lua.h lauxlib.h \
|
||||
lbuiltin.h ldo.h lstate.h luadebug.h lfunc.h lmem.h lstring.h \
|
||||
ltable.h ltm.h lundump.h lzio.h lvm.h
|
||||
lcode.o: lcode.c /usr/include/stdlib.h lcode.h llex.h lobject.h \
|
||||
llimits.h lua.h lzio.h lopcodes.h lparser.h ldo.h lstate.h luadebug.h \
|
||||
lmem.h lstring.h
|
||||
ldblib.o: ldblib.c lauxlib.h lua.h luadebug.h lualib.h
|
||||
ldebug.o: ldebug.c lapi.h lobject.h llimits.h lua.h lauxlib.h ldebug.h \
|
||||
luadebug.h ldo.h lstate.h lfunc.h ltable.h ltm.h
|
||||
ldo.o: ldo.c lauxlib.h lua.h ldebug.h lobject.h llimits.h luadebug.h \
|
||||
ldo.h lstate.h lgc.h lmem.h lparser.h lzio.h lstring.h ltm.h \
|
||||
lapi.o: lapi.c lua.h lapi.h lobject.h llimits.h ldo.h lstate.h \
|
||||
luadebug.h lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h lvm.h
|
||||
lauxlib.o: lauxlib.c lua.h lauxlib.h luadebug.h
|
||||
lbaselib.o: lbaselib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
lcode.o: lcode.c /usr/include/stdlib.h lua.h lcode.h llex.h lobject.h \
|
||||
llimits.h lzio.h lopcodes.h lparser.h ldo.h lstate.h luadebug.h \
|
||||
lmem.h
|
||||
ldblib.o: ldblib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
ldebug.o: ldebug.c lua.h lapi.h lobject.h llimits.h lcode.h llex.h \
|
||||
lzio.h lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h \
|
||||
lfunc.h lstring.h ltable.h ltm.h
|
||||
ldo.o: ldo.c lua.h ldebug.h lstate.h lobject.h llimits.h luadebug.h \
|
||||
ldo.h lgc.h lmem.h lparser.h lzio.h lstring.h ltable.h ltm.h \
|
||||
lundump.h lvm.h
|
||||
lfunc.o: lfunc.c lfunc.h lobject.h llimits.h lua.h lmem.h lstate.h \
|
||||
lfunc.o: lfunc.c lua.h lfunc.h lobject.h llimits.h lmem.h lstate.h \
|
||||
luadebug.h
|
||||
lgc.o: lgc.c ldo.h lobject.h llimits.h lua.h lstate.h luadebug.h \
|
||||
lfunc.h lgc.h lmem.h lref.h lstring.h ltable.h ltm.h
|
||||
linit.o: linit.c lua.h lualib.h
|
||||
liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h
|
||||
llex.o: llex.c lauxlib.h lua.h llex.h lobject.h llimits.h lzio.h \
|
||||
lmem.h lparser.h lstate.h luadebug.h lstring.h
|
||||
lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h
|
||||
lmem.o: lmem.c lmem.h lua.h lobject.h llimits.h lstate.h luadebug.h
|
||||
lobject.o: lobject.c lobject.h llimits.h lua.h
|
||||
lparser.o: lparser.c lcode.h llex.h lobject.h llimits.h lua.h lzio.h \
|
||||
lopcodes.h lparser.h ldo.h lstate.h luadebug.h lfunc.h lmem.h \
|
||||
lstring.h
|
||||
lref.o: lref.c lapi.h lobject.h llimits.h lua.h lmem.h lref.h lstate.h \
|
||||
lgc.o: lgc.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h
|
||||
liolib.o: liolib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
llex.o: llex.c lua.h llex.h lobject.h llimits.h lzio.h lmem.h \
|
||||
lparser.h lstate.h luadebug.h lstring.h ltable.h
|
||||
lmathlib.o: lmathlib.c lua.h lauxlib.h lualib.h
|
||||
lmem.o: lmem.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h
|
||||
lobject.o: lobject.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h
|
||||
lstate.o: lstate.c lauxlib.h lua.h lbuiltin.h ldo.h lobject.h \
|
||||
llimits.h lstate.h luadebug.h lgc.h llex.h lzio.h lmem.h lref.h \
|
||||
lstring.h ltm.h
|
||||
lstring.o: lstring.c lmem.h lua.h lobject.h llimits.h lstate.h \
|
||||
lparser.o: lparser.c lua.h lcode.h llex.h lobject.h llimits.h lzio.h \
|
||||
lopcodes.h lparser.h lfunc.h lmem.h lstate.h luadebug.h lstring.h
|
||||
lstate.o: lstate.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lgc.h llex.h lzio.h lmem.h lstring.h ltable.h ltm.h
|
||||
lstring.o: lstring.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h lstring.h
|
||||
lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h
|
||||
ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h llimits.h lstate.h \
|
||||
luadebug.h ltable.h
|
||||
ltests.o: ltests.c lapi.h lobject.h llimits.h lua.h lauxlib.h lmem.h \
|
||||
lopcodes.h lstate.h luadebug.h lstring.h ltable.h
|
||||
ltm.o: ltm.c lauxlib.h lua.h lmem.h lobject.h llimits.h lstate.h \
|
||||
luadebug.h ltm.h
|
||||
lstrlib.o: lstrlib.c lua.h lauxlib.h lualib.h
|
||||
ltable.o: ltable.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h lstring.h ltable.h
|
||||
ltests.o: ltests.c lua.h lapi.h lobject.h llimits.h lauxlib.h lcode.h \
|
||||
llex.h lzio.h lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h \
|
||||
lfunc.h lmem.h lstring.h ltable.h lualib.h
|
||||
ltm.o: ltm.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h ltm.h
|
||||
lua.o: lua.c lua.h luadebug.h lualib.h
|
||||
lundump.o: lundump.c lauxlib.h lua.h lfunc.h lobject.h llimits.h \
|
||||
lmem.h lopcodes.h lstring.h lstate.h luadebug.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lauxlib.h lua.h ldebug.h lobject.h llimits.h luadebug.h \
|
||||
ldo.h lstate.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h ltm.h \
|
||||
lundump.o: lundump.c lfunc.h lobject.h llimits.h lua.h lmem.h \
|
||||
lopcodes.h lstring.h lstate.h luadebug.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lua.h lapi.h lobject.h llimits.h ldebug.h lstate.h \
|
||||
luadebug.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h ltm.h \
|
||||
lvm.h
|
||||
lzio.o: lzio.c lzio.h
|
||||
lzio.o: lzio.c lua.h lzio.h
|
||||
|
||||
+2377
-1898
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user