diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-12-01 17:50:08 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-12-01 17:50:08 -0200 |
commit | fe237ad8085f34e89fcd3610a9771215af63f03f (patch) | |
tree | f7ee5d8f7d1ffb74e94f049aa6f31eb03606cdf6 /lapi.c | |
parent | 3181dfefee40b9a424b80aa779c671f5f458904c (diff) | |
download | lua-fe237ad8085f34e89fcd3610a9771215af63f03f.tar.gz lua-fe237ad8085f34e89fcd3610a9771215af63f03f.tar.bz2 lua-fe237ad8085f34e89fcd3610a9771215af63f03f.zip |
fixed stack; first version.
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 103 |
1 files changed, 50 insertions, 53 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.59 1999/11/29 19:11:36 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.60 1999/11/29 19:31:29 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 | */ |
@@ -63,28 +63,28 @@ static const TObject *luaA_protovalue (const TObject *o) { | |||
63 | 63 | ||
64 | 64 | ||
65 | static void checkCparams (lua_State *L, int nParams) { | 65 | static void checkCparams (lua_State *L, int nParams) { |
66 | if (L->stack.top-L->stack.stack < L->Cstack.base+nParams) | 66 | if (nParams > L->top-L->Cstack.base) |
67 | lua_error(L, "API error - wrong number of arguments in C2lua stack"); | 67 | lua_error(L, "API error - wrong number of arguments in C2lua stack"); |
68 | } | 68 | } |
69 | 69 | ||
70 | 70 | ||
71 | static lua_Object put_luaObject (lua_State *L, const TObject *o) { | 71 | static lua_Object put_luaObject (lua_State *L, const TObject *o) { |
72 | luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base); | 72 | luaD_openstack(L, L->Cstack.base); |
73 | L->stack.stack[L->Cstack.base++] = *o; | 73 | *L->Cstack.base++ = *o; |
74 | return L->Cstack.base; /* this is +1 real position (see Ref) */ | 74 | return Ref(L, L->Cstack.base-1); |
75 | } | 75 | } |
76 | 76 | ||
77 | 77 | ||
78 | lua_Object luaA_putObjectOnTop (lua_State *L) { | 78 | lua_Object luaA_putObjectOnTop (lua_State *L) { |
79 | luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base); | 79 | luaD_openstack(L, L->Cstack.base); |
80 | L->stack.stack[L->Cstack.base++] = *(--L->stack.top); | 80 | *L->Cstack.base++ = *(--L->top); |
81 | return L->Cstack.base; /* this is +1 real position (see Ref) */ | 81 | return Ref(L, L->Cstack.base-1); |
82 | } | 82 | } |
83 | 83 | ||
84 | 84 | ||
85 | static void top2LC (lua_State *L, int n) { | 85 | static void top2LC (lua_State *L, int n) { |
86 | /* Put the 'n' elements on the top as the Lua2C contents */ | 86 | /* Put the 'n' elements on the top as the Lua2C contents */ |
87 | L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */ | 87 | L->Cstack.base = L->top; /* new base */ |
88 | L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */ | 88 | L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */ |
89 | L->Cstack.num = n; /* number of results */ | 89 | L->Cstack.num = n; /* number of results */ |
90 | } | 90 | } |
@@ -98,13 +98,11 @@ lua_Object lua_pop (lua_State *L) { | |||
98 | 98 | ||
99 | /* | 99 | /* |
100 | ** Get a parameter, returning the object handle or LUA_NOOBJECT on error. | 100 | ** Get a parameter, returning the object handle or LUA_NOOBJECT on error. |
101 | ** 'number' must be 1 to get the first parameter. | 101 | ** `number' must be 1 to get the first parameter. |
102 | */ | 102 | */ |
103 | lua_Object lua_lua2C (lua_State *L, int number) { | 103 | lua_Object lua_lua2C (lua_State *L, int number) { |
104 | if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT; | 104 | if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT; |
105 | /* Ref(L, L->stack.stack+(L->Cstack.lua2C+number-1)) == | 105 | return Ref(L, L->Cstack.lua2C+number-1); |
106 | L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */ | ||
107 | return L->Cstack.lua2C+number; | ||
108 | } | 106 | } |
109 | 107 | ||
110 | 108 | ||
@@ -112,8 +110,8 @@ int lua_callfunction (lua_State *L, lua_Object function) { | |||
112 | if (function == LUA_NOOBJECT) | 110 | if (function == LUA_NOOBJECT) |
113 | return 1; | 111 | return 1; |
114 | else { | 112 | else { |
115 | luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base); | 113 | luaD_openstack(L, L->Cstack.base); |
116 | set_normalized(L->stack.stack+L->Cstack.base, Address(L, function)); | 114 | set_normalized(L->Cstack.base, Address(L, function)); |
117 | return luaD_protectedrun(L); | 115 | return luaD_protectedrun(L); |
118 | } | 116 | } |
119 | } | 117 | } |
@@ -126,7 +124,7 @@ lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) { | |||
126 | 124 | ||
127 | lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) { | 125 | lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) { |
128 | checkCparams(L, 1); | 126 | checkCparams(L, 1); |
129 | luaT_settagmethod(L, tag, event, L->stack.top-1); | 127 | luaT_settagmethod(L, tag, event, L->top-1); |
130 | return luaA_putObjectOnTop(L); | 128 | return luaA_putObjectOnTop(L); |
131 | } | 129 | } |
132 | 130 | ||
@@ -149,24 +147,24 @@ lua_Object lua_gettable (lua_State *L) { | |||
149 | 147 | ||
150 | lua_Object lua_rawgettable (lua_State *L) { | 148 | lua_Object lua_rawgettable (lua_State *L) { |
151 | checkCparams(L, 2); | 149 | checkCparams(L, 2); |
152 | if (ttype(L->stack.top-2) != LUA_T_ARRAY) | 150 | if (ttype(L->top-2) != LUA_T_ARRAY) |
153 | lua_error(L, "indexed expression not a table in rawgettable"); | 151 | lua_error(L, "indexed expression not a table in rawgettable"); |
154 | *(L->stack.top-2) = *luaH_get(L, avalue(L->stack.top-2), L->stack.top-1); | 152 | *(L->top-2) = *luaH_get(L, avalue(L->top-2), L->top-1); |
155 | --L->stack.top; | 153 | --L->top; |
156 | return luaA_putObjectOnTop(L); | 154 | return luaA_putObjectOnTop(L); |
157 | } | 155 | } |
158 | 156 | ||
159 | 157 | ||
160 | void lua_settable (lua_State *L) { | 158 | void lua_settable (lua_State *L) { |
161 | checkCparams(L, 3); | 159 | checkCparams(L, 3); |
162 | luaV_settable(L, L->stack.top-3); | 160 | luaV_settable(L, L->top-3); |
163 | L->stack.top -= 2; /* pop table and index */ | 161 | L->top -= 2; /* pop table and index */ |
164 | } | 162 | } |
165 | 163 | ||
166 | 164 | ||
167 | void lua_rawsettable (lua_State *L) { | 165 | void lua_rawsettable (lua_State *L) { |
168 | checkCparams(L, 3); | 166 | checkCparams(L, 3); |
169 | luaV_rawsettable(L, L->stack.top-3); | 167 | luaV_rawsettable(L, L->top-3); |
170 | } | 168 | } |
171 | 169 | ||
172 | 170 | ||
@@ -202,7 +200,7 @@ void lua_setglobal (lua_State *L, const char *name) { | |||
202 | void lua_rawsetglobal (lua_State *L, const char *name) { | 200 | void lua_rawsetglobal (lua_State *L, const char *name) { |
203 | GlobalVar *gv = luaS_assertglobalbyname(L, name); | 201 | GlobalVar *gv = luaS_assertglobalbyname(L, name); |
204 | checkCparams(L, 1); | 202 | checkCparams(L, 1); |
205 | gv->value = *(--L->stack.top); | 203 | gv->value = *(--L->top); |
206 | } | 204 | } |
207 | 205 | ||
208 | 206 | ||
@@ -280,19 +278,19 @@ lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) { | |||
280 | 278 | ||
281 | 279 | ||
282 | void lua_pushnil (lua_State *L) { | 280 | void lua_pushnil (lua_State *L) { |
283 | ttype(L->stack.top) = LUA_T_NIL; | 281 | ttype(L->top) = LUA_T_NIL; |
284 | incr_top; | 282 | incr_top; |
285 | } | 283 | } |
286 | 284 | ||
287 | void lua_pushnumber (lua_State *L, double n) { | 285 | void lua_pushnumber (lua_State *L, double n) { |
288 | ttype(L->stack.top) = LUA_T_NUMBER; | 286 | ttype(L->top) = LUA_T_NUMBER; |
289 | nvalue(L->stack.top) = n; | 287 | nvalue(L->top) = n; |
290 | incr_top; | 288 | incr_top; |
291 | } | 289 | } |
292 | 290 | ||
293 | void lua_pushlstring (lua_State *L, const char *s, long len) { | 291 | void lua_pushlstring (lua_State *L, const char *s, long len) { |
294 | tsvalue(L->stack.top) = luaS_newlstr(L, s, len); | 292 | tsvalue(L->top) = luaS_newlstr(L, s, len); |
295 | ttype(L->stack.top) = LUA_T_STRING; | 293 | ttype(L->top) = LUA_T_STRING; |
296 | incr_top; | 294 | incr_top; |
297 | luaC_checkGC(L); | 295 | luaC_checkGC(L); |
298 | } | 296 | } |
@@ -308,8 +306,8 @@ void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { | |||
308 | if (fn == NULL) | 306 | if (fn == NULL) |
309 | lua_error(L, "API error - attempt to push a NULL Cfunction"); | 307 | lua_error(L, "API error - attempt to push a NULL Cfunction"); |
310 | checkCparams(L, n); | 308 | checkCparams(L, n); |
311 | ttype(L->stack.top) = LUA_T_CPROTO; | 309 | ttype(L->top) = LUA_T_CPROTO; |
312 | fvalue(L->stack.top) = fn; | 310 | fvalue(L->top) = fn; |
313 | incr_top; | 311 | incr_top; |
314 | luaV_closure(L, n); | 312 | luaV_closure(L, n); |
315 | luaC_checkGC(L); | 313 | luaC_checkGC(L); |
@@ -318,21 +316,21 @@ void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { | |||
318 | void lua_pushusertag (lua_State *L, void *u, int tag) { | 316 | void lua_pushusertag (lua_State *L, void *u, int tag) { |
319 | if (tag < 0 && tag != LUA_ANYTAG) | 317 | if (tag < 0 && tag != LUA_ANYTAG) |
320 | luaT_realtag(L, tag); /* error if tag is not valid */ | 318 | luaT_realtag(L, tag); /* error if tag is not valid */ |
321 | tsvalue(L->stack.top) = luaS_createudata(L, u, tag); | 319 | tsvalue(L->top) = luaS_createudata(L, u, tag); |
322 | ttype(L->stack.top) = LUA_T_USERDATA; | 320 | ttype(L->top) = LUA_T_USERDATA; |
323 | incr_top; | 321 | incr_top; |
324 | luaC_checkGC(L); | 322 | luaC_checkGC(L); |
325 | } | 323 | } |
326 | 324 | ||
327 | void luaA_pushobject (lua_State *L, const TObject *o) { | 325 | void luaA_pushobject (lua_State *L, const TObject *o) { |
328 | *L->stack.top = *o; | 326 | *L->top = *o; |
329 | incr_top; | 327 | incr_top; |
330 | } | 328 | } |
331 | 329 | ||
332 | void lua_pushobject (lua_State *L, lua_Object o) { | 330 | void lua_pushobject (lua_State *L, lua_Object o) { |
333 | if (o == LUA_NOOBJECT) | 331 | if (o == LUA_NOOBJECT) |
334 | lua_error(L, "API error - attempt to push a NOOBJECT"); | 332 | lua_error(L, "API error - attempt to push a NOOBJECT"); |
335 | set_normalized(L->stack.top, Address(L, o)); | 333 | set_normalized(L->top, Address(L, o)); |
336 | incr_top; | 334 | incr_top; |
337 | } | 335 | } |
338 | 336 | ||
@@ -368,18 +366,18 @@ int lua_tag (lua_State *L, lua_Object lo) { | |||
368 | void lua_settag (lua_State *L, int tag) { | 366 | void lua_settag (lua_State *L, int tag) { |
369 | checkCparams(L, 1); | 367 | checkCparams(L, 1); |
370 | luaT_realtag(L, tag); | 368 | luaT_realtag(L, tag); |
371 | switch (ttype(L->stack.top-1)) { | 369 | switch (ttype(L->top-1)) { |
372 | case LUA_T_ARRAY: | 370 | case LUA_T_ARRAY: |
373 | (L->stack.top-1)->value.a->htag = tag; | 371 | (L->top-1)->value.a->htag = tag; |
374 | break; | 372 | break; |
375 | case LUA_T_USERDATA: | 373 | case LUA_T_USERDATA: |
376 | (L->stack.top-1)->value.ts->u.d.tag = tag; | 374 | (L->top-1)->value.ts->u.d.tag = tag; |
377 | break; | 375 | break; |
378 | default: | 376 | default: |
379 | luaL_verror(L, "cannot change the tag of a %.20s", | 377 | luaL_verror(L, "cannot change the tag of a %.20s", |
380 | luaO_typename(L, L->stack.top-1)); | 378 | luaO_typename(L, L->top-1)); |
381 | } | 379 | } |
382 | L->stack.top--; | 380 | L->top--; |
383 | } | 381 | } |
384 | 382 | ||
385 | 383 | ||
@@ -395,7 +393,7 @@ GlobalVar *luaA_nextvar (lua_State *L, TaggedString *ts) { | |||
395 | while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */ | 393 | while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */ |
396 | gv = gv->next; | 394 | gv = gv->next; |
397 | if (gv) { | 395 | if (gv) { |
398 | ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = gv->name; | 396 | ttype(L->top) = LUA_T_STRING; tsvalue(L->top) = gv->name; |
399 | incr_top; | 397 | incr_top; |
400 | luaA_pushobject(L, &gv->value); | 398 | luaA_pushobject(L, &gv->value); |
401 | } | 399 | } |
@@ -479,12 +477,12 @@ int lua_setdebug (lua_State *L, int debug) { | |||
479 | 477 | ||
480 | 478 | ||
481 | lua_Function lua_stackedfunction (lua_State *L, int level) { | 479 | lua_Function lua_stackedfunction (lua_State *L, int level) { |
482 | StkId i; | 480 | int i; |
483 | for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) { | 481 | for (i = (L->top-1)-L->stack; i>=0; i--) { |
484 | int t = L->stack.stack[i].ttype; | 482 | int t = L->stack[i].ttype; |
485 | if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK) | 483 | if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK) |
486 | if (level-- == 0) | 484 | if (level-- == 0) |
487 | return Ref(L, L->stack.stack+i); | 485 | return Ref(L, L->stack+i); |
488 | } | 486 | } |
489 | return LUA_NOOBJECT; | 487 | return LUA_NOOBJECT; |
490 | } | 488 | } |
@@ -498,8 +496,7 @@ int lua_nups (lua_State *L, lua_Function func) { | |||
498 | 496 | ||
499 | int lua_currentline (lua_State *L, lua_Function func) { | 497 | int lua_currentline (lua_State *L, lua_Function func) { |
500 | const TObject *f = Address(L, func); | 498 | const TObject *f = Address(L, func); |
501 | return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ? | 499 | return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1; |
502 | (f+1)->value.i : -1; | ||
503 | } | 500 | } |
504 | 501 | ||
505 | 502 | ||
@@ -533,11 +530,11 @@ int lua_setlocal (lua_State *L, lua_Function func, int local_number) { | |||
533 | const char *name = luaF_getlocalname(fp, local_number, | 530 | const char *name = luaF_getlocalname(fp, local_number, |
534 | lua_currentline(L, func)); | 531 | lua_currentline(L, func)); |
535 | checkCparams(L, 1); | 532 | checkCparams(L, 1); |
536 | --L->stack.top; | 533 | --L->top; |
537 | if (name) { | 534 | if (name) { |
538 | /* if "name", there must be a LUA_T_LINE */ | 535 | /* if "name", there must be a LUA_T_LINE */ |
539 | /* therefore, f+2 points to function base */ | 536 | /* therefore, f+2 points to function base */ |
540 | *((f+2)+(local_number-1)) = *L->stack.top; | 537 | *((f+2)+(local_number-1)) = *L->top; |
541 | return 1; | 538 | return 1; |
542 | } | 539 | } |
543 | else | 540 | else |
@@ -565,14 +562,14 @@ void lua_funcinfo (lua_State *L, lua_Object func, | |||
565 | 562 | ||
566 | 563 | ||
567 | static int checkfunc (lua_State *L, TObject *o) { | 564 | static int checkfunc (lua_State *L, TObject *o) { |
568 | return luaO_equalObj(o, L->stack.top); | 565 | return luaO_equalObj(o, L->top); |
569 | } | 566 | } |
570 | 567 | ||
571 | 568 | ||
572 | const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) { | 569 | const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) { |
573 | /* try to find a name for given function */ | 570 | /* try to find a name for given function */ |
574 | GlobalVar *g; | 571 | GlobalVar *g; |
575 | set_normalized(L->stack.top, Address(L, o)); /* to be used by `checkfunc' */ | 572 | set_normalized(L->top, Address(L, o)); /* to be used by `checkfunc' */ |
576 | for (g=L->rootglobal; g; g=g->next) { | 573 | for (g=L->rootglobal; g; g=g->next) { |
577 | if (checkfunc(L, &g->value)) { | 574 | if (checkfunc(L, &g->value)) { |
578 | *name = g->name->str; | 575 | *name = g->name->str; |
@@ -610,7 +607,7 @@ void lua_beginblock (lua_State *L) { | |||
610 | void lua_endblock (lua_State *L) { | 607 | void lua_endblock (lua_State *L) { |
611 | --L->numCblocks; | 608 | --L->numCblocks; |
612 | L->Cstack = L->Cblocks[L->numCblocks]; | 609 | L->Cstack = L->Cblocks[L->numCblocks]; |
613 | luaD_adjusttop(L, L->Cstack.base); | 610 | L->top = L->Cstack.base; |
614 | } | 611 | } |
615 | 612 | ||
616 | 613 | ||
@@ -618,8 +615,8 @@ void lua_endblock (lua_State *L) { | |||
618 | int lua_ref (lua_State *L, int lock) { | 615 | int lua_ref (lua_State *L, int lock) { |
619 | int ref; | 616 | int ref; |
620 | checkCparams(L, 1); | 617 | checkCparams(L, 1); |
621 | ref = luaR_ref(L, L->stack.top-1, lock); | 618 | ref = luaR_ref(L, L->top-1, lock); |
622 | L->stack.top--; | 619 | L->top--; |
623 | return ref; | 620 | return ref; |
624 | } | 621 | } |
625 | 622 | ||