first version for new API
This commit is contained in:
504
lapi.c
504
lapi.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 1.86 2000/08/09 19:16:57 roberto Exp roberto $
|
||||
** $Id: lapi.c,v 1.87 2000/08/14 19:10:14 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -16,7 +16,6 @@
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lref.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
@@ -29,246 +28,148 @@ 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) (++L->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;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
if (L->Cstack.base != L->top-1) {
|
||||
luaD_openstack(L, L->Cstack.base);
|
||||
*L->Cstack.base = *(--L->top);
|
||||
}
|
||||
return L->Cstack.base++;
|
||||
}
|
||||
|
||||
|
||||
void lua_pushglobals (lua_State *L) {
|
||||
hvalue(L->top) = L->gt;
|
||||
ttype(L->top) = TAG_TABLE;
|
||||
void luaA_pushobject (lua_State *L, const TObject *o) {
|
||||
*L->top = *o;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
void lua_setglobals (lua_State *L, lua_Object newtable) {
|
||||
if (lua_type(L, newtable)[0] != 't') /* type == "table"? */
|
||||
lua_error(L, "Lua API error - invalid value for global table");
|
||||
L->gt = hvalue(newtable);
|
||||
|
||||
/*
|
||||
** basic stack manipulation
|
||||
*/
|
||||
|
||||
|
||||
int lua_gettop (lua_State *L) {
|
||||
return (L->top - L->Cbase);
|
||||
}
|
||||
|
||||
|
||||
void lua_settop (lua_State *L, int index) {
|
||||
if (index >= 0)
|
||||
luaD_adjusttop(L, L->Cbase, index);
|
||||
else
|
||||
L->top += index; /* index is negative */
|
||||
}
|
||||
|
||||
|
||||
void lua_pushobject (lua_State *L, int index) {
|
||||
*L->top = *Index(L, index);
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
|
||||
** `number' must be 1 to get the first parameter.
|
||||
** access functions (stack -> C)
|
||||
*/
|
||||
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;
|
||||
|
||||
|
||||
#define btest(L,i,value,default) { \
|
||||
StkId o; \
|
||||
if ((i) >= 0) { \
|
||||
o = L->Cbase+((i)-1); \
|
||||
if (o >= L->top) return (default); \
|
||||
} \
|
||||
else o = L->top+(i); \
|
||||
return (value); }
|
||||
|
||||
|
||||
#define access(L,i,test,default,value) { \
|
||||
StkId o; \
|
||||
if ((i) >= 0) { \
|
||||
o = L->Cbase+((i)-1); \
|
||||
if (o >= L->top) return (default); \
|
||||
} \
|
||||
else o = L->top+(i); \
|
||||
return ((test) ? (value) : (default)); }
|
||||
|
||||
|
||||
const char *lua_type (lua_State *L, int index) {
|
||||
btest(L, index, luaO_typename(o), "NO VALUE");
|
||||
}
|
||||
|
||||
int lua_iscfunction (lua_State *L, int index) {
|
||||
btest(L, index, (ttype(o) == TAG_CCLOSURE), 0);
|
||||
}
|
||||
|
||||
int lua_isnumber (lua_State *L, int index) {
|
||||
btest(L, index, (tonumber(Index(L, index)) == 0), 0);
|
||||
}
|
||||
|
||||
int lua_tag (lua_State *L, int index) {
|
||||
btest(L, index,
|
||||
((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag : luaT_effectivetag(L, o)),
|
||||
-1);
|
||||
}
|
||||
|
||||
int lua_equal(lua_State *L, int index1, int index2) {
|
||||
return luaO_equalObj(Index(L, index1), Index(L, index2));
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
double lua_tonumber (lua_State *L, int index) {
|
||||
access(L, index, (tonumber(o) == 0), 0.0, nvalue(o));
|
||||
}
|
||||
|
||||
|
||||
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 lua_pop(L);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_gettable (lua_State *L) {
|
||||
luaA_checkCargs(L, 2);
|
||||
luaV_gettable(L, L->top--);
|
||||
return lua_pop(L);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_rawget (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");
|
||||
res = luaA_putluaObject(L, luaH_get(L, hvalue(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_rawset (lua_State *L) {
|
||||
luaA_checkCargs(L, 3);
|
||||
if (ttype(L->top-3) != TAG_TABLE)
|
||||
lua_error(L, "indexed expression not a table");
|
||||
*luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1);
|
||||
L->top -= 3;
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_createtable (lua_State *L) {
|
||||
TObject o;
|
||||
luaC_checkGC(L);
|
||||
hvalue(&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_new(L, name), L->top++);
|
||||
return lua_pop(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_setglobal (lua_State *L, const char *name) {
|
||||
luaA_checkCargs(L, 1);
|
||||
luaV_setglobal(L, luaS_new(L, name), 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) {
|
||||
const char *lua_tostring (lua_State *L, int index) {
|
||||
luaC_checkGC(L); /* `tostring' may create a new string */
|
||||
if (obj == LUA_NOOBJECT || tostring(L, obj))
|
||||
return NULL;
|
||||
else return (svalue(obj));
|
||||
access(L, index, (tostring(L, o) == 0), NULL, svalue(o));
|
||||
}
|
||||
|
||||
size_t lua_strlen (lua_State *L, lua_Object obj) {
|
||||
if (obj == LUA_NOOBJECT || tostring(L, obj))
|
||||
return 0L;
|
||||
else return (tsvalue(obj)->u.s.len);
|
||||
size_t lua_strlen (lua_State *L, int index) {
|
||||
access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->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_tocfunction (lua_State *L, int index) {
|
||||
access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c);
|
||||
}
|
||||
|
||||
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_touserdata (lua_State *L, int index) {
|
||||
access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** push functions (C -> stack)
|
||||
*/
|
||||
|
||||
|
||||
void lua_pushnil (lua_State *L) {
|
||||
ttype(L->top) = TAG_NIL;
|
||||
incr_top;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushnumber (lua_State *L, double n) {
|
||||
ttype(L->top) = TAG_NUMBER;
|
||||
nvalue(L->top) = n;
|
||||
incr_top;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
||||
luaC_checkGC(L);
|
||||
tsvalue(L->top) = luaS_newlstr(L, s, len);
|
||||
ttype(L->top) = TAG_STRING;
|
||||
incr_top;
|
||||
luaC_checkGC(L);
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushstring (lua_State *L, const char *s) {
|
||||
if (s == NULL)
|
||||
lua_pushnil(L);
|
||||
@@ -276,48 +177,154 @@ void lua_pushstring (lua_State *L, const char *s) {
|
||||
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);
|
||||
luaV_Cclosure(L, fn, n);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
|
||||
luaC_checkGC(L);
|
||||
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;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
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 tsvalue(o)->u.d.tag;
|
||||
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
|
||||
|
||||
void lua_getglobal (lua_State *L, const char *name) {
|
||||
luaV_getglobal(L, luaS_new(L, name), L->top++);
|
||||
}
|
||||
|
||||
|
||||
void lua_gettable (lua_State *L) {
|
||||
luaV_gettable(L, L->top--);
|
||||
}
|
||||
|
||||
|
||||
void lua_rawget (lua_State *L) {
|
||||
if (ttype(L->top - 2) != TAG_TABLE)
|
||||
lua_error(L, "indexed expression not a table");
|
||||
*(L->top - 2) = *luaH_get(L, hvalue(L->top - 2), L->top - 1);
|
||||
L->top--;
|
||||
}
|
||||
|
||||
|
||||
void lua_getglobals (lua_State *L) {
|
||||
hvalue(L->top) = L->gt;
|
||||
ttype(L->top) = TAG_TABLE;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_gettagmethod (lua_State *L, int tag, const char *event) {
|
||||
*L->top = *luaT_gettagmethod(L, tag, event);
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
int lua_getref (lua_State *L, int ref) {
|
||||
if (ref == LUA_REFNIL)
|
||||
ttype(L->top) = TAG_NIL;
|
||||
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 luaT_effectivetag(L, o);
|
||||
return 0;
|
||||
api_incr_top(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void lua_newtable (lua_State *L) {
|
||||
luaC_checkGC(L);
|
||||
hvalue(L->top) = luaH_new(L, 0);
|
||||
ttype(L->top) = TAG_TABLE;
|
||||
api_incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
|
||||
|
||||
void lua_setglobal (lua_State *L, const char *name) {
|
||||
luaV_setglobal(L, luaS_new(L, name), L->top--);
|
||||
}
|
||||
|
||||
|
||||
void lua_settable (lua_State *L) {
|
||||
StkId top = L->top;
|
||||
luaV_settable(L, top-3, top);
|
||||
L->top = top-3; /* pop table, index, and value */
|
||||
}
|
||||
|
||||
|
||||
void lua_rawset (lua_State *L) {
|
||||
if (ttype(L->top-3) != TAG_TABLE)
|
||||
lua_error(L, "indexed expression not a table");
|
||||
*luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1);
|
||||
L->top -= 3;
|
||||
}
|
||||
|
||||
|
||||
void lua_setglobals (lua_State *L) {
|
||||
TObject *newtable = --L->top;
|
||||
if (ttype(newtable) != TAG_TABLE)
|
||||
lua_error(L, "Lua API error - invalid value for global table");
|
||||
L->gt = hvalue(newtable);
|
||||
}
|
||||
|
||||
|
||||
void lua_settagmethod (lua_State *L, int tag, const char *event) {
|
||||
TObject *method = L->top - 1;
|
||||
if (ttype(method) != TAG_NIL &&
|
||||
ttype(method) != TAG_CCLOSURE &&
|
||||
ttype(method) != TAG_LCLOSURE)
|
||||
lua_error(L, "Lua API error - tag method must be a function or nil");
|
||||
luaT_settagmethod(L, tag, event, method);
|
||||
}
|
||||
|
||||
|
||||
int lua_ref (lua_State *L, int lock) {
|
||||
int ref;
|
||||
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,
|
||||
"reference table overflow", MAX_INT);
|
||||
ref = L->refSize++;
|
||||
}
|
||||
L->refArray[ref].o = *(L->top-1);
|
||||
L->refArray[ref].st = lock ? LOCK : HOLD;
|
||||
}
|
||||
L->top--;
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** miscelaneous functions
|
||||
*/
|
||||
|
||||
|
||||
void lua_settag (lua_State *L, int tag) {
|
||||
luaA_checkCargs(L, 1);
|
||||
luaT_realtag(L, tag);
|
||||
switch (ttype(L->top-1)) {
|
||||
case TAG_TABLE:
|
||||
@@ -334,6 +341,17 @@ void lua_settag (lua_State *L, int tag) {
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaA_next (lua_State *L, const Hash *t, int i) {
|
||||
int tsize = t->size;
|
||||
for (; i<tsize; i++) {
|
||||
@@ -348,38 +366,10 @@ int luaA_next (lua_State *L, const Hash *t, int i) {
|
||||
}
|
||||
|
||||
|
||||
int lua_next (lua_State *L, lua_Object t, int i) {
|
||||
int lua_next (lua_State *L, int index, int i) {
|
||||
const TObject *t = Index(L, index);
|
||||
if (ttype(t) != TAG_TABLE)
|
||||
lua_error(L, "Lua API error - object is not a table in `lua_next'");
|
||||
i = luaA_next(L, hvalue(t), i);
|
||||
top2LC(L, (i==0) ? 0 : 2);
|
||||
return i;
|
||||
return luaA_next(L, hvalue(t), i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if LUA_DEPRECATETFUNCS
|
||||
|
||||
/*
|
||||
** obsolete functions
|
||||
*/
|
||||
|
||||
lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
|
||||
lua_pushglobals(L);
|
||||
lua_pushstring(L, name);
|
||||
return lua_rawget(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_rawsetglobal (lua_State *L, const char *name) {
|
||||
lua_Object value;
|
||||
lua_beginblock(L);
|
||||
value = lua_pop(L);
|
||||
lua_pushglobals(L);
|
||||
lua_pushstring(L, name);
|
||||
lua_pushobject(L, value);
|
||||
lua_rawset(L);
|
||||
lua_endblock(L);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user