2 ** $Id: lapi.c,v 1.110a 2000/10/30 12:50:09 roberto Exp $
4 ** See Copyright Notice in lua.h
25 const char lua_ident
[] = "$Lua: " LUA_VERSION
" " LUA_COPYRIGHT
" $\n"
26 "$Authors: " LUA_AUTHORS
" $";
30 #define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
32 #define api_incr_top(L) incr_top
37 TObject
*luaA_index (lua_State
*L
, int index
) {
38 return Index(L
, index
);
42 static TObject
*luaA_indexAcceptable (lua_State
*L
, int index
) {
44 TObject
*o
= L
->Cbase
+(index
-1);
45 if (o
>= L
->top
) return NULL
;
48 else return L
->top
+index
;
52 void luaA_pushobject (lua_State
*L
, const TObject
*o
) {
57 LUA_API
int lua_stackspace (lua_State
*L
) {
58 return (L
->stack_last
- L
->top
);
64 ** basic stack manipulation
68 LUA_API
int lua_gettop (lua_State
*L
) {
69 return (L
->top
- L
->Cbase
);
73 LUA_API
void lua_settop (lua_State
*L
, int index
) {
75 luaD_adjusttop(L
, L
->Cbase
, index
);
77 L
->top
= L
->top
+index
+1; /* index is negative */
81 LUA_API
void lua_remove (lua_State
*L
, int index
) {
82 StkId p
= luaA_index(L
, index
);
83 while (++p
< L
->top
) *(p
-1) = *p
;
88 LUA_API
void lua_insert (lua_State
*L
, int index
) {
89 StkId p
= luaA_index(L
, index
);
91 for (q
= L
->top
; q
>p
; q
--)
97 LUA_API
void lua_pushvalue (lua_State
*L
, int index
) {
98 *L
->top
= *luaA_index(L
, index
);
105 ** access functions (stack -> C)
109 LUA_API
int lua_type (lua_State
*L
, int index
) {
110 StkId o
= luaA_indexAcceptable(L
, index
);
111 return (o
== NULL
) ? LUA_TNONE
: ttype(o
);
114 LUA_API
const char *lua_typename (lua_State
*L
, int t
) {
116 return (t
== LUA_TNONE
) ? "no value" : luaO_typenames
[t
];
120 LUA_API
int lua_iscfunction (lua_State
*L
, int index
) {
121 StkId o
= luaA_indexAcceptable(L
, index
);
122 return (o
== NULL
) ? 0 : iscfunction(o
);
125 LUA_API
int lua_isnumber (lua_State
*L
, int index
) {
126 TObject
*o
= luaA_indexAcceptable(L
, index
);
127 return (o
== NULL
) ? 0 : (tonumber(o
) == 0);
130 LUA_API
int lua_isstring (lua_State
*L
, int index
) {
131 int t
= lua_type(L
, index
);
132 return (t
== LUA_TSTRING
|| t
== LUA_TNUMBER
);
136 LUA_API
int lua_tag (lua_State
*L
, int index
) {
137 StkId o
= luaA_indexAcceptable(L
, index
);
138 return (o
== NULL
) ? LUA_NOTAG
: luaT_tag(o
);
141 LUA_API
int lua_equal (lua_State
*L
, int index1
, int index2
) {
142 StkId o1
= luaA_indexAcceptable(L
, index1
);
143 StkId o2
= luaA_indexAcceptable(L
, index2
);
144 if (o1
== NULL
|| o2
== NULL
) return 0; /* index out-of-range */
145 else return luaO_equalObj(o1
, o2
);
148 LUA_API
int lua_lessthan (lua_State
*L
, int index1
, int index2
) {
149 StkId o1
= luaA_indexAcceptable(L
, index1
);
150 StkId o2
= luaA_indexAcceptable(L
, index2
);
151 if (o1
== NULL
|| o2
== NULL
) return 0; /* index out-of-range */
152 else return luaV_lessthan(L
, o1
, o2
, L
->top
);
157 LUA_API
double lua_tonumber (lua_State
*L
, int index
) {
158 StkId o
= luaA_indexAcceptable(L
, index
);
159 return (o
== NULL
|| tonumber(o
)) ? 0 : nvalue(o
);
162 LUA_API
const char *lua_tostring (lua_State
*L
, int index
) {
163 StkId o
= luaA_indexAcceptable(L
, index
);
164 return (o
== NULL
|| tostring(L
, o
)) ? NULL
: svalue(o
);
167 LUA_API
size_t lua_strlen (lua_State
*L
, int index
) {
168 StkId o
= luaA_indexAcceptable(L
, index
);
169 return (o
== NULL
|| tostring(L
, o
)) ? 0 : tsvalue(o
)->len
;
172 LUA_API lua_CFunction
lua_tocfunction (lua_State
*L
, int index
) {
173 StkId o
= luaA_indexAcceptable(L
, index
);
174 return (o
== NULL
|| !iscfunction(o
)) ? NULL
: clvalue(o
)->f
.c
;
177 LUA_API
void *lua_touserdata (lua_State
*L
, int index
) {
178 StkId o
= luaA_indexAcceptable(L
, index
);
179 return (o
== NULL
|| ttype(o
) != LUA_TUSERDATA
) ? NULL
:
180 tsvalue(o
)->u
.d
.value
;
183 LUA_API
const void *lua_topointer (lua_State
*L
, int index
) {
184 StkId o
= luaA_indexAcceptable(L
, index
);
185 if (o
== NULL
) return NULL
;
191 default: return NULL
;
198 ** push functions (C -> stack)
202 LUA_API
void lua_pushnil (lua_State
*L
) {
203 ttype(L
->top
) = LUA_TNIL
;
208 LUA_API
void lua_pushnumber (lua_State
*L
, double n
) {
210 ttype(L
->top
) = LUA_TNUMBER
;
215 LUA_API
void lua_pushlstring (lua_State
*L
, const char *s
, size_t len
) {
216 tsvalue(L
->top
) = luaS_newlstr(L
, s
, len
);
217 ttype(L
->top
) = LUA_TSTRING
;
222 LUA_API
void lua_pushstring (lua_State
*L
, const char *s
) {
226 lua_pushlstring(L
, s
, strlen(s
));
230 LUA_API
void lua_pushcclosure (lua_State
*L
, lua_CFunction fn
, int n
) {
231 luaV_Cclosure(L
, fn
, n
);
235 LUA_API
void lua_pushusertag (lua_State
*L
, void *u
, int tag
) {
237 if (!(tag
== LUA_ANYTAG
|| tag
== LUA_TUSERDATA
|| validtag(tag
)))
238 luaO_verror(L
, "invalid tag for a userdata (%d)", tag
);
239 tsvalue(L
->top
) = luaS_createudata(L
, u
, tag
);
240 ttype(L
->top
) = LUA_TUSERDATA
;
247 ** get functions (Lua -> stack)
251 LUA_API
void lua_getglobal (lua_State
*L
, const char *name
) {
253 *top
= *luaV_getglobal(L
, luaS_new(L
, name
));
259 LUA_API
void lua_gettable (lua_State
*L
, int index
) {
260 StkId t
= Index(L
, index
);
262 *(top
-1) = *luaV_gettable(L
, t
);
263 L
->top
= top
; /* tag method may change top */
267 LUA_API
void lua_rawget (lua_State
*L
, int index
) {
268 StkId t
= Index(L
, index
);
269 LUA_ASSERT(ttype(t
) == LUA_TTABLE
, "table expected");
270 *(L
->top
- 1) = *luaH_get(L
, hvalue(t
), L
->top
- 1);
274 LUA_API
void lua_rawgeti (lua_State
*L
, int index
, int n
) {
275 StkId o
= Index(L
, index
);
276 LUA_ASSERT(ttype(o
) == LUA_TTABLE
, "table expected");
277 *L
->top
= *luaH_getnum(hvalue(o
), n
);
282 LUA_API
void lua_getglobals (lua_State
*L
) {
283 hvalue(L
->top
) = L
->gt
;
284 ttype(L
->top
) = LUA_TTABLE
;
289 LUA_API
int lua_getref (lua_State
*L
, int ref
) {
290 if (ref
== LUA_REFNIL
)
291 ttype(L
->top
) = LUA_TNIL
;
292 else if (0 <= ref
&& ref
< L
->refSize
&&
293 (L
->refArray
[ref
].st
== LOCK
|| L
->refArray
[ref
].st
== HOLD
))
294 *L
->top
= L
->refArray
[ref
].o
;
302 LUA_API
void lua_newtable (lua_State
*L
) {
303 hvalue(L
->top
) = luaH_new(L
, 0);
304 ttype(L
->top
) = LUA_TTABLE
;
311 ** set functions (stack -> Lua)
315 LUA_API
void lua_setglobal (lua_State
*L
, const char *name
) {
317 luaV_setglobal(L
, luaS_new(L
, name
));
318 L
->top
= top
-1; /* remove element from the top */
322 LUA_API
void lua_settable (lua_State
*L
, int index
) {
323 StkId t
= Index(L
, index
);
325 luaV_settable(L
, t
, top
-2);
326 L
->top
= top
-2; /* pop index and value */
330 LUA_API
void lua_rawset (lua_State
*L
, int index
) {
331 StkId t
= Index(L
, index
);
332 LUA_ASSERT(ttype(t
) == LUA_TTABLE
, "table expected");
333 *luaH_set(L
, hvalue(t
), L
->top
-2) = *(L
->top
-1);
338 LUA_API
void lua_rawseti (lua_State
*L
, int index
, int n
) {
339 StkId o
= Index(L
, index
);
340 LUA_ASSERT(ttype(o
) == LUA_TTABLE
, "table expected");
341 *luaH_setint(L
, hvalue(o
), n
) = *(L
->top
-1);
346 LUA_API
void lua_setglobals (lua_State
*L
) {
347 StkId newtable
= --L
->top
;
348 LUA_ASSERT(ttype(newtable
) == LUA_TTABLE
, "table expected");
349 L
->gt
= hvalue(newtable
);
353 LUA_API
int lua_ref (lua_State
*L
, int lock
) {
355 if (ttype(L
->top
-1) == LUA_TNIL
)
358 if (L
->refFree
!= NONEXT
) { /* is there a free place? */
360 L
->refFree
= L
->refArray
[ref
].st
;
362 else { /* no more free places */
363 luaM_growvector(L
, L
->refArray
, L
->refSize
, 1, struct Ref
,
364 "reference table overflow", MAX_INT
);
365 L
->nblocks
+= sizeof(struct Ref
);
368 L
->refArray
[ref
].o
= *(L
->top
-1);
369 L
->refArray
[ref
].st
= lock
? LOCK
: HOLD
;
377 ** "do" functions (run Lua code)
378 ** (most of them are in ldo.c)
381 LUA_API
void lua_rawcall (lua_State
*L
, int nargs
, int nresults
) {
382 luaD_call(L
, L
->top
-(nargs
+1), nresults
);
387 ** Garbage-collection functions
390 /* GC values are expressed in Kbytes: #bytes/2^10 */
391 #define GCscale(x) ((int)((x)>>10))
392 #define GCunscale(x) ((unsigned long)(x)<<10)
394 LUA_API
int lua_getgcthreshold (lua_State
*L
) {
395 return GCscale(L
->GCthreshold
);
398 LUA_API
int lua_getgccount (lua_State
*L
) {
399 return GCscale(L
->nblocks
);
402 LUA_API
void lua_setgcthreshold (lua_State
*L
, int newthreshold
) {
403 if (newthreshold
> GCscale(ULONG_MAX
))
404 L
->GCthreshold
= ULONG_MAX
;
406 L
->GCthreshold
= GCunscale(newthreshold
);
412 ** miscellaneous functions
415 LUA_API
void lua_settag (lua_State
*L
, int tag
) {
416 luaT_realtag(L
, tag
);
417 switch (ttype(L
->top
-1)) {
419 hvalue(L
->top
-1)->htag
= tag
;
422 tsvalue(L
->top
-1)->u
.d
.tag
= tag
;
425 luaO_verror(L
, "cannot change the tag of a %.20s",
426 luaO_typename(L
->top
-1));
431 LUA_API
void lua_unref (lua_State
*L
, int ref
) {
433 LUA_ASSERT(ref
< L
->refSize
&& L
->refArray
[ref
].st
< 0, "invalid ref");
434 L
->refArray
[ref
].st
= L
->refFree
;
440 LUA_API
int lua_next (lua_State
*L
, int index
) {
441 StkId t
= luaA_index(L
, index
);
443 LUA_ASSERT(ttype(t
) == LUA_TTABLE
, "table expected");
444 n
= luaH_next(L
, hvalue(t
), luaA_index(L
, -1));
446 *(L
->top
-1) = *key(n
);
451 else { /* no more elements */
452 L
->top
-= 1; /* remove key */
458 LUA_API
int lua_getn (lua_State
*L
, int index
) {
459 Hash
*h
= hvalue(luaA_index(L
, index
));
460 const TObject
*value
= luaH_getstr(h
, luaS_new(L
, "n")); /* value = h.n */
461 if (ttype(value
) == LUA_TNUMBER
)
462 return (int)nvalue(value
);
468 if (ttype(key(n
)) == LUA_TNUMBER
&&
469 ttype(val(n
)) != LUA_TNIL
&&
470 nvalue(key(n
)) > max
)
471 max
= nvalue(key(n
));
479 LUA_API
void lua_concat (lua_State
*L
, int n
) {
481 luaV_strconc(L
, n
, top
);
487 LUA_API
void *lua_newuserdata (lua_State
*L
, size_t size
) {
488 TString
*ts
= luaS_newudata(L
, (size
==0) ? 1 : size
, NULL
);
489 tsvalue(L
->top
) = ts
;
490 ttype(L
->top
) = LUA_TUSERDATA
;
492 return ts
->u
.d
.value
;