aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c99
1 files changed, 50 insertions, 49 deletions
diff --git a/lapi.c b/lapi.c
index 5d56ac2e..c2a22774 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.105 2000/10/05 12:14:08 roberto Exp roberto $ 2** $Id: lapi.c,v 1.106 2000/10/06 19:29:26 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -54,7 +54,7 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
54 incr_top; 54 incr_top;
55} 55}
56 56
57int lua_stackspace (lua_State *L) { 57LUA_API int lua_stackspace (lua_State *L) {
58 return (L->stack_last - L->top); 58 return (L->stack_last - L->top);
59} 59}
60 60
@@ -65,12 +65,12 @@ int lua_stackspace (lua_State *L) {
65*/ 65*/
66 66
67 67
68int lua_gettop (lua_State *L) { 68LUA_API int lua_gettop (lua_State *L) {
69 return (L->top - L->Cbase); 69 return (L->top - L->Cbase);
70} 70}
71 71
72 72
73void lua_settop (lua_State *L, int index) { 73LUA_API void lua_settop (lua_State *L, int index) {
74 if (index >= 0) 74 if (index >= 0)
75 luaD_adjusttop(L, L->Cbase, index); 75 luaD_adjusttop(L, L->Cbase, index);
76 else 76 else
@@ -78,14 +78,14 @@ void lua_settop (lua_State *L, int index) {
78} 78}
79 79
80 80
81void lua_remove (lua_State *L, int index) { 81LUA_API void lua_remove (lua_State *L, int index) {
82 StkId p = luaA_index(L, index); 82 StkId p = luaA_index(L, index);
83 while (++p < L->top) *(p-1) = *p; 83 while (++p < L->top) *(p-1) = *p;
84 L->top--; 84 L->top--;
85} 85}
86 86
87 87
88void lua_insert (lua_State *L, int index) { 88LUA_API void lua_insert (lua_State *L, int index) {
89 StkId p = luaA_index(L, index); 89 StkId p = luaA_index(L, index);
90 StkId q; 90 StkId q;
91 for (q = L->top; q>p; q--) 91 for (q = L->top; q>p; q--)
@@ -94,7 +94,7 @@ void lua_insert (lua_State *L, int index) {
94} 94}
95 95
96 96
97void lua_pushvalue (lua_State *L, int index) { 97LUA_API void lua_pushvalue (lua_State *L, int index) {
98 *L->top = *luaA_index(L, index); 98 *L->top = *luaA_index(L, index);
99 api_incr_top(L); 99 api_incr_top(L);
100} 100}
@@ -106,50 +106,50 @@ void lua_pushvalue (lua_State *L, int index) {
106*/ 106*/
107 107
108 108
109int lua_type (lua_State *L, int index) { 109LUA_API int lua_type (lua_State *L, int index) {
110 StkId o = luaA_indexAcceptable(L, index); 110 StkId o = luaA_indexAcceptable(L, index);
111 if (o == NULL) return LUA_TNONE; 111 if (o == NULL) return LUA_TNONE;
112 else return ttype(o); 112 else return ttype(o);
113} 113}
114 114
115const char *lua_typename (lua_State *L, int t) { 115LUA_API const char *lua_typename (lua_State *L, int t) {
116 UNUSED(L); 116 UNUSED(L);
117 return luaO_typenames[t]; 117 return luaO_typenames[t];
118} 118}
119 119
120 120
121int lua_iscfunction (lua_State *L, int index) { 121LUA_API int lua_iscfunction (lua_State *L, int index) {
122 StkId o = luaA_indexAcceptable(L, index); 122 StkId o = luaA_indexAcceptable(L, index);
123 if (o == NULL) return 0; 123 if (o == NULL) return 0;
124 else return iscfunction(o); 124 else return iscfunction(o);
125} 125}
126 126
127int lua_isnumber (lua_State *L, int index) { 127LUA_API int lua_isnumber (lua_State *L, int index) {
128 TObject *o = luaA_indexAcceptable(L, index); 128 TObject *o = luaA_indexAcceptable(L, index);
129 if (o == NULL) return 0; 129 if (o == NULL) return 0;
130 else return (tonumber(o) == 0); 130 else return (tonumber(o) == 0);
131} 131}
132 132
133int lua_isstring (lua_State *L, int index) { 133LUA_API int lua_isstring (lua_State *L, int index) {
134 int t = lua_type(L, index); 134 int t = lua_type(L, index);
135 return (t == LUA_TSTRING || t == LUA_TNUMBER); 135 return (t == LUA_TSTRING || t == LUA_TNUMBER);
136} 136}
137 137
138 138
139int lua_tag (lua_State *L, int index) { 139LUA_API int lua_tag (lua_State *L, int index) {
140 StkId o = luaA_indexAcceptable(L, index); 140 StkId o = luaA_indexAcceptable(L, index);
141 if (o == NULL) return LUA_NOTAG; 141 if (o == NULL) return LUA_NOTAG;
142 else return luaT_tag(o); 142 else return luaT_tag(o);
143} 143}
144 144
145int lua_equal (lua_State *L, int index1, int index2) { 145LUA_API int lua_equal (lua_State *L, int index1, int index2) {
146 StkId o1 = luaA_indexAcceptable(L, index1); 146 StkId o1 = luaA_indexAcceptable(L, index1);
147 StkId o2 = luaA_indexAcceptable(L, index2); 147 StkId o2 = luaA_indexAcceptable(L, index2);
148 if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */ 148 if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */
149 else return luaO_equalObj(o1, o2); 149 else return luaO_equalObj(o1, o2);
150} 150}
151 151
152int lua_lessthan (lua_State *L, int index1, int index2) { 152LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
153 StkId o1 = luaA_indexAcceptable(L, index1); 153 StkId o1 = luaA_indexAcceptable(L, index1);
154 StkId o2 = luaA_indexAcceptable(L, index2); 154 StkId o2 = luaA_indexAcceptable(L, index2);
155 if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */ 155 if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */
@@ -158,37 +158,37 @@ int lua_lessthan (lua_State *L, int index1, int index2) {
158 158
159 159
160 160
161double lua_tonumber (lua_State *L, int index) { 161LUA_API double lua_tonumber (lua_State *L, int index) {
162 StkId o = luaA_indexAcceptable(L, index); 162 StkId o = luaA_indexAcceptable(L, index);
163 if (o == NULL || tonumber(o)) return 0; 163 if (o == NULL || tonumber(o)) return 0;
164 else return nvalue(o); 164 else return nvalue(o);
165} 165}
166 166
167const char *lua_tostring (lua_State *L, int index) { 167LUA_API const char *lua_tostring (lua_State *L, int index) {
168 StkId o = luaA_indexAcceptable(L, index); 168 StkId o = luaA_indexAcceptable(L, index);
169 if (o == NULL || tostring(L, o)) return NULL; 169 if (o == NULL || tostring(L, o)) return NULL;
170 else return svalue(o); 170 else return svalue(o);
171} 171}
172 172
173size_t lua_strlen (lua_State *L, int index) { 173LUA_API size_t lua_strlen (lua_State *L, int index) {
174 StkId o = luaA_indexAcceptable(L, index); 174 StkId o = luaA_indexAcceptable(L, index);
175 if (o == NULL || tostring(L, o)) return 0; 175 if (o == NULL || tostring(L, o)) return 0;
176 else return tsvalue(o)->u.s.len; 176 else return tsvalue(o)->u.s.len;
177} 177}
178 178
179lua_CFunction lua_tocfunction (lua_State *L, int index) { 179LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
180 StkId o = luaA_indexAcceptable(L, index); 180 StkId o = luaA_indexAcceptable(L, index);
181 if (o == NULL || !iscfunction(o)) return NULL; 181 if (o == NULL || !iscfunction(o)) return NULL;
182 else return clvalue(o)->f.c; 182 else return clvalue(o)->f.c;
183} 183}
184 184
185void *lua_touserdata (lua_State *L, int index) { 185LUA_API void *lua_touserdata (lua_State *L, int index) {
186 StkId o = luaA_indexAcceptable(L, index); 186 StkId o = luaA_indexAcceptable(L, index);
187 if (o == NULL || ttype(o) != LUA_TUSERDATA) return NULL; 187 if (o == NULL || ttype(o) != LUA_TUSERDATA) return NULL;
188 else return tsvalue(o)->u.d.value; 188 else return tsvalue(o)->u.d.value;
189} 189}
190 190
191const void *lua_topointer (lua_State *L, int index) { 191LUA_API const void *lua_topointer (lua_State *L, int index) {
192 StkId o = luaA_indexAcceptable(L, index); 192 StkId o = luaA_indexAcceptable(L, index);
193 if (o == NULL) return NULL; 193 if (o == NULL) return NULL;
194 switch (ttype(o)) { 194 switch (ttype(o)) {
@@ -207,27 +207,27 @@ const void *lua_topointer (lua_State *L, int index) {
207*/ 207*/
208 208
209 209
210void lua_pushnil (lua_State *L) { 210LUA_API void lua_pushnil (lua_State *L) {
211 ttype(L->top) = LUA_TNIL; 211 ttype(L->top) = LUA_TNIL;
212 api_incr_top(L); 212 api_incr_top(L);
213} 213}
214 214
215 215
216void lua_pushnumber (lua_State *L, double n) { 216LUA_API void lua_pushnumber (lua_State *L, double n) {
217 nvalue(L->top) = n; 217 nvalue(L->top) = n;
218 ttype(L->top) = LUA_TNUMBER; 218 ttype(L->top) = LUA_TNUMBER;
219 api_incr_top(L); 219 api_incr_top(L);
220} 220}
221 221
222 222
223void lua_pushlstring (lua_State *L, const char *s, size_t len) { 223LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
224 tsvalue(L->top) = luaS_newlstr(L, s, len); 224 tsvalue(L->top) = luaS_newlstr(L, s, len);
225 ttype(L->top) = LUA_TSTRING; 225 ttype(L->top) = LUA_TSTRING;
226 api_incr_top(L); 226 api_incr_top(L);
227} 227}
228 228
229 229
230void lua_pushstring (lua_State *L, const char *s) { 230LUA_API void lua_pushstring (lua_State *L, const char *s) {
231 if (s == NULL) 231 if (s == NULL)
232 lua_pushnil(L); 232 lua_pushnil(L);
233 else 233 else
@@ -235,12 +235,13 @@ void lua_pushstring (lua_State *L, const char *s) {
235} 235}
236 236
237 237
238void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { 238LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
239 luaV_Cclosure(L, fn, n); 239 luaV_Cclosure(L, fn, n);
240} 240}
241 241
242 242
243void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */ 243LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
244 /* ORDER LUA_T */
244 if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag))) 245 if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag)))
245 luaO_verror(L, "invalid tag for a userdata (%d)", tag); 246 luaO_verror(L, "invalid tag for a userdata (%d)", tag);
246 tsvalue(L->top) = luaS_createudata(L, u, tag); 247 tsvalue(L->top) = luaS_createudata(L, u, tag);
@@ -255,7 +256,7 @@ void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
255*/ 256*/
256 257
257 258
258void lua_getglobal (lua_State *L, const char *name) { 259LUA_API void lua_getglobal (lua_State *L, const char *name) {
259 StkId top = L->top; 260 StkId top = L->top;
260 *top = *luaV_getglobal(L, luaS_new(L, name)); 261 *top = *luaV_getglobal(L, luaS_new(L, name));
261 L->top = top; 262 L->top = top;
@@ -263,7 +264,7 @@ void lua_getglobal (lua_State *L, const char *name) {
263} 264}
264 265
265 266
266void lua_gettable (lua_State *L, int index) { 267LUA_API void lua_gettable (lua_State *L, int index) {
267 StkId t = Index(L, index); 268 StkId t = Index(L, index);
268 StkId top = L->top; 269 StkId top = L->top;
269 *(top-1) = *luaV_gettable(L, t); 270 *(top-1) = *luaV_gettable(L, t);
@@ -271,14 +272,14 @@ void lua_gettable (lua_State *L, int index) {
271} 272}
272 273
273 274
274void lua_rawget (lua_State *L, int index) { 275LUA_API void lua_rawget (lua_State *L, int index) {
275 StkId t = Index(L, index); 276 StkId t = Index(L, index);
276 LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); 277 LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
277 *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1); 278 *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
278} 279}
279 280
280 281
281void lua_rawgeti (lua_State *L, int index, int n) { 282LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
282 StkId o = Index(L, index); 283 StkId o = Index(L, index);
283 LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected"); 284 LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
284 *L->top = *luaH_getnum(hvalue(o), n); 285 *L->top = *luaH_getnum(hvalue(o), n);
@@ -286,14 +287,14 @@ void lua_rawgeti (lua_State *L, int index, int n) {
286} 287}
287 288
288 289
289void lua_getglobals (lua_State *L) { 290LUA_API void lua_getglobals (lua_State *L) {
290 hvalue(L->top) = L->gt; 291 hvalue(L->top) = L->gt;
291 ttype(L->top) = LUA_TTABLE; 292 ttype(L->top) = LUA_TTABLE;
292 api_incr_top(L); 293 api_incr_top(L);
293} 294}
294 295
295 296
296int lua_getref (lua_State *L, int ref) { 297LUA_API int lua_getref (lua_State *L, int ref) {
297 if (ref == LUA_REFNIL) 298 if (ref == LUA_REFNIL)
298 ttype(L->top) = LUA_TNIL; 299 ttype(L->top) = LUA_TNIL;
299 else if (0 <= ref && ref < L->refSize && 300 else if (0 <= ref && ref < L->refSize &&
@@ -306,7 +307,7 @@ int lua_getref (lua_State *L, int ref) {
306} 307}
307 308
308 309
309void lua_newtable (lua_State *L) { 310LUA_API void lua_newtable (lua_State *L) {
310 hvalue(L->top) = luaH_new(L, 0); 311 hvalue(L->top) = luaH_new(L, 0);
311 ttype(L->top) = LUA_TTABLE; 312 ttype(L->top) = LUA_TTABLE;
312 api_incr_top(L); 313 api_incr_top(L);
@@ -319,14 +320,14 @@ void lua_newtable (lua_State *L) {
319*/ 320*/
320 321
321 322
322void lua_setglobal (lua_State *L, const char *name) { 323LUA_API void lua_setglobal (lua_State *L, const char *name) {
323 StkId top = L->top; 324 StkId top = L->top;
324 luaV_setglobal(L, luaS_new(L, name)); 325 luaV_setglobal(L, luaS_new(L, name));
325 L->top = top-1; /* remove element from the top */ 326 L->top = top-1; /* remove element from the top */
326} 327}
327 328
328 329
329void lua_settable (lua_State *L, int index) { 330LUA_API void lua_settable (lua_State *L, int index) {
330 StkId t = Index(L, index); 331 StkId t = Index(L, index);
331 StkId top = L->top; 332 StkId top = L->top;
332 luaV_settable(L, t, top-2); 333 luaV_settable(L, t, top-2);
@@ -334,7 +335,7 @@ void lua_settable (lua_State *L, int index) {
334} 335}
335 336
336 337
337void lua_rawset (lua_State *L, int index) { 338LUA_API void lua_rawset (lua_State *L, int index) {
338 StkId t = Index(L, index); 339 StkId t = Index(L, index);
339 LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); 340 LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
340 *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1); 341 *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
@@ -342,7 +343,7 @@ void lua_rawset (lua_State *L, int index) {
342} 343}
343 344
344 345
345void lua_rawseti (lua_State *L, int index, int n) { 346LUA_API void lua_rawseti (lua_State *L, int index, int n) {
346 StkId o = Index(L, index); 347 StkId o = Index(L, index);
347 LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected"); 348 LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
348 *luaH_setint(L, hvalue(o), n) = *(L->top-1); 349 *luaH_setint(L, hvalue(o), n) = *(L->top-1);
@@ -350,14 +351,14 @@ void lua_rawseti (lua_State *L, int index, int n) {
350} 351}
351 352
352 353
353void lua_setglobals (lua_State *L) { 354LUA_API void lua_setglobals (lua_State *L) {
354 StkId newtable = --L->top; 355 StkId newtable = --L->top;
355 LUA_ASSERT(ttype(newtable) == LUA_TTABLE, "table expected"); 356 LUA_ASSERT(ttype(newtable) == LUA_TTABLE, "table expected");
356 L->gt = hvalue(newtable); 357 L->gt = hvalue(newtable);
357} 358}
358 359
359 360
360int lua_ref (lua_State *L, int lock) { 361LUA_API int lua_ref (lua_State *L, int lock) {
361 int ref; 362 int ref;
362 if (ttype(L->top-1) == LUA_TNIL) 363 if (ttype(L->top-1) == LUA_TNIL)
363 ref = LUA_REFNIL; 364 ref = LUA_REFNIL;
@@ -385,7 +386,7 @@ int lua_ref (lua_State *L, int lock) {
385** (most of them are in ldo.c) 386** (most of them are in ldo.c)
386*/ 387*/
387 388
388void lua_rawcall (lua_State *L, int nargs, int nresults) { 389LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
389 luaD_call(L, L->top-(nargs+1), nresults); 390 luaD_call(L, L->top-(nargs+1), nresults);
390} 391}
391 392
@@ -398,15 +399,15 @@ void lua_rawcall (lua_State *L, int nargs, int nresults) {
398#define GCscale(x) ((int)((x)>>10)) 399#define GCscale(x) ((int)((x)>>10))
399#define GCunscale(x) ((unsigned long)(x)<<10) 400#define GCunscale(x) ((unsigned long)(x)<<10)
400 401
401int lua_getgcthreshold (lua_State *L) { 402LUA_API int lua_getgcthreshold (lua_State *L) {
402 return GCscale(L->GCthreshold); 403 return GCscale(L->GCthreshold);
403} 404}
404 405
405int lua_getgccount (lua_State *L) { 406LUA_API int lua_getgccount (lua_State *L) {
406 return GCscale(L->nblocks); 407 return GCscale(L->nblocks);
407} 408}
408 409
409void lua_setgcthreshold (lua_State *L, int newthreshold) { 410LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
410 if (newthreshold > GCscale(ULONG_MAX)) 411 if (newthreshold > GCscale(ULONG_MAX))
411 L->GCthreshold = ULONG_MAX; 412 L->GCthreshold = ULONG_MAX;
412 else 413 else
@@ -419,7 +420,7 @@ void lua_setgcthreshold (lua_State *L, int newthreshold) {
419** miscellaneous functions 420** miscellaneous functions
420*/ 421*/
421 422
422void lua_settag (lua_State *L, int tag) { 423LUA_API void lua_settag (lua_State *L, int tag) {
423 luaT_realtag(L, tag); 424 luaT_realtag(L, tag);
424 switch (ttype(L->top-1)) { 425 switch (ttype(L->top-1)) {
425 case LUA_TTABLE: 426 case LUA_TTABLE:
@@ -436,7 +437,7 @@ void lua_settag (lua_State *L, int tag) {
436} 437}
437 438
438 439
439void lua_unref (lua_State *L, int ref) { 440LUA_API void lua_unref (lua_State *L, int ref) {
440 if (ref >= 0) { 441 if (ref >= 0) {
441 LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref"); 442 LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
442 L->refArray[ref].st = L->refFree; 443 L->refArray[ref].st = L->refFree;
@@ -445,7 +446,7 @@ void lua_unref (lua_State *L, int ref) {
445} 446}
446 447
447 448
448int lua_next (lua_State *L, int index) { 449LUA_API int lua_next (lua_State *L, int index) {
449 StkId t = luaA_index(L, index); 450 StkId t = luaA_index(L, index);
450 Node *n; 451 Node *n;
451 LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); 452 LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
@@ -463,7 +464,7 @@ int lua_next (lua_State *L, int index) {
463} 464}
464 465
465 466
466int lua_getn (lua_State *L, int index) { 467LUA_API int lua_getn (lua_State *L, int index) {
467 Hash *h = hvalue(luaA_index(L, index)); 468 Hash *h = hvalue(luaA_index(L, index));
468 const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */ 469 const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
469 if (ttype(value) == LUA_TNUMBER) 470 if (ttype(value) == LUA_TNUMBER)
@@ -484,7 +485,7 @@ int lua_getn (lua_State *L, int index) {
484} 485}
485 486
486 487
487void lua_concat (lua_State *L, int n) { 488LUA_API void lua_concat (lua_State *L, int n) {
488 StkId top = L->top; 489 StkId top = L->top;
489 luaV_strconc(L, n, top); 490 luaV_strconc(L, n, top);
490 L->top = top-(n-1); 491 L->top = top-(n-1);