diff options
-rw-r--r-- | lapi.c | 64 | ||||
-rw-r--r-- | lapi.h | 3 | ||||
-rw-r--r-- | lbuiltin.c | 172 | ||||
-rw-r--r-- | ltable.c | 28 | ||||
-rw-r--r-- | ltable.h | 4 | ||||
-rw-r--r-- | lua.h | 4 |
6 files changed, 143 insertions, 132 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.36 1999/02/12 19:23:02 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.37 1999/02/22 19:13:12 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 | */ |
@@ -25,7 +25,7 @@ | |||
25 | 25 | ||
26 | 26 | ||
27 | char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" | 27 | char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" |
28 | "$Autores: " LUA_AUTHORS " $"; | 28 | "$Authors: " LUA_AUTHORS " $"; |
29 | 29 | ||
30 | 30 | ||
31 | 31 | ||
@@ -70,12 +70,8 @@ void luaA_packresults (void) | |||
70 | } | 70 | } |
71 | 71 | ||
72 | 72 | ||
73 | int luaA_passresults (void) | 73 | int luaA_passresults (void) { |
74 | { | 74 | L->Cstack.base = L->Cstack.lua2C; /* position of first result */ |
75 | luaD_checkstack(L->Cstack.num); | ||
76 | memcpy(L->stack.top, L->Cstack.lua2C+L->stack.stack, | ||
77 | L->Cstack.num*sizeof(TObject)); | ||
78 | L->stack.top += L->Cstack.num; | ||
79 | return L->Cstack.num; | 75 | return L->Cstack.num; |
80 | } | 76 | } |
81 | 77 | ||
@@ -87,24 +83,29 @@ static void checkCparams (int nParams) | |||
87 | } | 83 | } |
88 | 84 | ||
89 | 85 | ||
90 | static lua_Object put_luaObject (TObject *o) | 86 | static lua_Object put_luaObject (TObject *o) { |
91 | { | ||
92 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); | 87 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); |
93 | L->stack.stack[L->Cstack.base++] = *o; | 88 | L->stack.stack[L->Cstack.base++] = *o; |
94 | return L->Cstack.base; /* this is +1 real position (see Ref) */ | 89 | return L->Cstack.base; /* this is +1 real position (see Ref) */ |
95 | } | 90 | } |
96 | 91 | ||
97 | 92 | ||
98 | static lua_Object put_luaObjectonTop (void) | 93 | static lua_Object put_luaObjectonTop (void) { |
99 | { | ||
100 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); | 94 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); |
101 | L->stack.stack[L->Cstack.base++] = *(--L->stack.top); | 95 | L->stack.stack[L->Cstack.base++] = *(--L->stack.top); |
102 | return L->Cstack.base; /* this is +1 real position (see Ref) */ | 96 | return L->Cstack.base; /* this is +1 real position (see Ref) */ |
103 | } | 97 | } |
104 | 98 | ||
105 | 99 | ||
106 | lua_Object lua_pop (void) | 100 | static void top2LC (int n) { |
107 | { | 101 | /* Put the 'n' elements on the top as the Lua2C contents */ |
102 | L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */ | ||
103 | L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */ | ||
104 | L->Cstack.num = n; /* number of results */ | ||
105 | } | ||
106 | |||
107 | |||
108 | lua_Object lua_pop (void) { | ||
108 | checkCparams(1); | 109 | checkCparams(1); |
109 | return put_luaObjectonTop(); | 110 | return put_luaObjectonTop(); |
110 | } | 111 | } |
@@ -436,6 +437,11 @@ TaggedString *luaA_nextvar (TaggedString *g) { | |||
436 | } | 437 | } |
437 | while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ | 438 | while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ |
438 | g = (TaggedString *)g->head.next; | 439 | g = (TaggedString *)g->head.next; |
440 | if (g) { | ||
441 | ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g; | ||
442 | incr_top; | ||
443 | luaA_pushobject(&g->u.s.globalval); | ||
444 | } | ||
439 | return g; | 445 | return g; |
440 | } | 446 | } |
441 | 447 | ||
@@ -444,11 +450,37 @@ char *lua_nextvar (char *varname) { | |||
444 | TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname); | 450 | TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname); |
445 | g = luaA_nextvar(g); | 451 | g = luaA_nextvar(g); |
446 | if (g) { | 452 | if (g) { |
447 | luaA_pushobject(&g->u.s.globalval); | 453 | top2LC(2); |
448 | return g->str; | 454 | return g->str; |
449 | } | 455 | } |
450 | else | 456 | else { |
457 | top2LC(0); | ||
451 | return NULL; | 458 | return NULL; |
459 | } | ||
460 | } | ||
461 | |||
462 | |||
463 | int luaA_next (Hash *t, int i) { | ||
464 | Node *n; | ||
465 | int tsize = nhash(t); | ||
466 | while (i < tsize && ttype(val(n=node(t, i))) == LUA_T_NIL) i++; | ||
467 | if (i >= tsize) | ||
468 | return 0; | ||
469 | else { | ||
470 | luaA_pushobject(ref(n)); | ||
471 | luaA_pushobject(val(n)); | ||
472 | return i+1; | ||
473 | } | ||
474 | } | ||
475 | |||
476 | |||
477 | int lua_next (lua_Object o, int i) { | ||
478 | TObject *t = Address(o); | ||
479 | if (ttype(t) != LUA_T_ARRAY) | ||
480 | lua_error("API error: object is not a table in `lua_next'"); | ||
481 | i = luaA_next(avalue(t), i); | ||
482 | top2LC((i==0) ? 0 : 2); | ||
483 | return i; | ||
452 | } | 484 | } |
453 | 485 | ||
454 | 486 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.h,v 1.2 1998/06/19 16:14:09 roberto Exp roberto $ | 2 | ** $Id: lapi.h,v 1.3 1999/02/22 19:13:12 roberto Exp roberto $ |
3 | ** Auxiliary functions from Lua API | 3 | ** Auxiliary functions from Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -17,5 +17,6 @@ void luaA_pushobject (TObject *o); | |||
17 | void luaA_packresults (void); | 17 | void luaA_packresults (void); |
18 | int luaA_passresults (void); | 18 | int luaA_passresults (void); |
19 | TaggedString *luaA_nextvar (TaggedString *g); | 19 | TaggedString *luaA_nextvar (TaggedString *g); |
20 | int luaA_next (Hash *t, int i); | ||
20 | 21 | ||
21 | #endif | 22 | #endif |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.52 1999/02/22 14:17:24 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.53 1999/02/22 19:13:12 roberto Exp roberto $ |
3 | ** Built-in functions | 3 | ** Built-in functions |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -300,9 +300,76 @@ static void luaB_call (void) { | |||
300 | } | 300 | } |
301 | } | 301 | } |
302 | 302 | ||
303 | |||
304 | static void luaB_nextvar (void) { | ||
305 | TObject *o = luaA_Address(luaL_nonnullarg(1)); | ||
306 | TaggedString *g; | ||
307 | if (ttype(o) == LUA_T_NIL) | ||
308 | g = NULL; | ||
309 | else { | ||
310 | luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); | ||
311 | g = tsvalue(o); | ||
312 | } | ||
313 | if (!luaA_nextvar(g)) | ||
314 | lua_pushnil(); | ||
315 | } | ||
316 | |||
317 | |||
318 | static void luaB_next (void) { | ||
319 | Hash *a = gethash(1); | ||
320 | TObject *k = luaA_Address(luaL_nonnullarg(2)); | ||
321 | int i = (ttype(k) == LUA_T_NIL) ? 0 : luaH_pos(a, k)+1; | ||
322 | if (luaA_next(a, i) == 0) | ||
323 | lua_pushnil(); | ||
324 | } | ||
325 | |||
326 | |||
327 | static void luaB_tostring (void) { | ||
328 | lua_Object obj = lua_getparam(1); | ||
329 | TObject *o = luaA_Address(obj); | ||
330 | char buff[64]; | ||
331 | switch (ttype(o)) { | ||
332 | case LUA_T_NUMBER: | ||
333 | lua_pushstring(lua_getstring(obj)); | ||
334 | return; | ||
335 | case LUA_T_STRING: | ||
336 | lua_pushobject(obj); | ||
337 | return; | ||
338 | case LUA_T_ARRAY: | ||
339 | sprintf(buff, "table: %p", (void *)o->value.a); | ||
340 | break; | ||
341 | case LUA_T_CLOSURE: | ||
342 | sprintf(buff, "function: %p", (void *)o->value.cl); | ||
343 | break; | ||
344 | case LUA_T_PROTO: | ||
345 | sprintf(buff, "function: %p", (void *)o->value.tf); | ||
346 | break; | ||
347 | case LUA_T_CPROTO: | ||
348 | sprintf(buff, "function: %p", (void *)o->value.f); | ||
349 | break; | ||
350 | case LUA_T_USERDATA: | ||
351 | sprintf(buff, "userdata: %p", o->value.ts->u.d.v); | ||
352 | break; | ||
353 | case LUA_T_NIL: | ||
354 | lua_pushstring("nil"); | ||
355 | return; | ||
356 | default: | ||
357 | LUA_INTERNALERROR("invalid type"); | ||
358 | } | ||
359 | lua_pushstring(buff); | ||
360 | } | ||
361 | |||
362 | |||
363 | static void luaB_type (void) { | ||
364 | lua_Object o = luaL_nonnullarg(1); | ||
365 | lua_pushstring(luaO_typename(luaA_Address(o))); | ||
366 | lua_pushnumber(lua_tag(o)); | ||
367 | } | ||
368 | |||
303 | /* }====================================================== */ | 369 | /* }====================================================== */ |
304 | 370 | ||
305 | 371 | ||
372 | |||
306 | /* | 373 | /* |
307 | ** {====================================================== | 374 | ** {====================================================== |
308 | ** "Extra" functions | 375 | ** "Extra" functions |
@@ -352,7 +419,7 @@ static void luaB_foreach (void) { | |||
352 | luaD_calln(2, 1); | 419 | luaD_calln(2, 1); |
353 | if (ttype(L->stack.top-1) != LUA_T_NIL) | 420 | if (ttype(L->stack.top-1) != LUA_T_NIL) |
354 | return; | 421 | return; |
355 | L->stack.top--; | 422 | L->stack.top--; /* remove result */ |
356 | } | 423 | } |
357 | } | 424 | } |
358 | } | 425 | } |
@@ -361,22 +428,21 @@ static void luaB_foreach (void) { | |||
361 | static void luaB_foreachvar (void) { | 428 | static void luaB_foreachvar (void) { |
362 | TObject *f = luaA_Address(luaL_functionarg(1)); | 429 | TObject *f = luaA_Address(luaL_functionarg(1)); |
363 | GCnode *g; | 430 | GCnode *g; |
364 | StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */ | 431 | luaD_checkstack(4); /* for extra var name, f, var name, and globalval */ |
365 | luaD_checkstack(4); /* for var name, f, s, and globalvar */ | ||
366 | ttype(L->stack.stack+name) = LUA_T_NIL; | ||
367 | L->stack.top++; /* top == base */ | ||
368 | for (g = L->rootglobal.next; g; g = g->next) { | 432 | for (g = L->rootglobal.next; g; g = g->next) { |
369 | TaggedString *s = (TaggedString *)g; | 433 | TaggedString *s = (TaggedString *)g; |
370 | if (s->u.s.globalval.ttype != LUA_T_NIL) { | 434 | if (s->u.s.globalval.ttype != LUA_T_NIL) { |
371 | ttype(L->stack.stack+name) = LUA_T_STRING; | 435 | pushtagstring(s); /* keep (extra) s on stack to avoid GC */ |
372 | tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */ | ||
373 | *(L->stack.top++) = *f; | 436 | *(L->stack.top++) = *f; |
374 | pushtagstring(s); | 437 | pushtagstring(s); |
375 | *(L->stack.top++) = s->u.s.globalval; | 438 | *(L->stack.top++) = s->u.s.globalval; |
376 | luaD_calln(2, 1); | 439 | luaD_calln(2, 1); |
377 | if (ttype(L->stack.top-1) != LUA_T_NIL) | 440 | if (ttype(L->stack.top-1) != LUA_T_NIL) { |
441 | L->stack.top--; | ||
442 | *(L->stack.top-1) = *L->stack.top; /* remove extra s */ | ||
378 | return; | 443 | return; |
379 | L->stack.top--; | 444 | } |
445 | L->stack.top-=2; /* remove result and extra s */ | ||
380 | } | 446 | } |
381 | } | 447 | } |
382 | } | 448 | } |
@@ -505,85 +571,7 @@ static void luaB_sort (void) { | |||
505 | 571 | ||
506 | 572 | ||
507 | /* | 573 | /* |
508 | ** {====================================================== | 574 | ** ====================================================== */ |
509 | ** Internal Functions. | ||
510 | ** These functions need access to internal structures | ||
511 | ** to be implemented. | ||
512 | ** ======================================================= | ||
513 | */ | ||
514 | |||
515 | |||
516 | static void luaB_nextvar (void) { | ||
517 | TObject *o = luaA_Address(luaL_nonnullarg(1)); | ||
518 | TaggedString *g; | ||
519 | if (ttype(o) == LUA_T_NIL) | ||
520 | g = NULL; | ||
521 | else { | ||
522 | luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); | ||
523 | g = tsvalue(o); | ||
524 | } | ||
525 | g = luaA_nextvar(g); | ||
526 | if (g) { | ||
527 | pushtagstring(g); | ||
528 | luaA_pushobject(&g->u.s.globalval); | ||
529 | } | ||
530 | else lua_pushnil(); /* no more globals */ | ||
531 | } | ||
532 | |||
533 | |||
534 | static void luaB_next (void) { | ||
535 | Node *n = luaH_next(gethash(1), luaA_Address(luaL_nonnullarg(2))); | ||
536 | if (n) { | ||
537 | luaA_pushobject(&n->ref); | ||
538 | luaA_pushobject(&n->val); | ||
539 | } | ||
540 | else lua_pushnil(); | ||
541 | } | ||
542 | |||
543 | |||
544 | static void luaB_tostring (void) { | ||
545 | lua_Object obj = lua_getparam(1); | ||
546 | TObject *o = luaA_Address(obj); | ||
547 | char buff[64]; | ||
548 | switch (ttype(o)) { | ||
549 | case LUA_T_NUMBER: | ||
550 | lua_pushstring(lua_getstring(obj)); | ||
551 | return; | ||
552 | case LUA_T_STRING: | ||
553 | lua_pushobject(obj); | ||
554 | return; | ||
555 | case LUA_T_ARRAY: | ||
556 | sprintf(buff, "table: %p", (void *)o->value.a); | ||
557 | break; | ||
558 | case LUA_T_CLOSURE: | ||
559 | sprintf(buff, "function: %p", (void *)o->value.cl); | ||
560 | break; | ||
561 | case LUA_T_PROTO: | ||
562 | sprintf(buff, "function: %p", (void *)o->value.tf); | ||
563 | break; | ||
564 | case LUA_T_CPROTO: | ||
565 | sprintf(buff, "function: %p", (void *)o->value.f); | ||
566 | break; | ||
567 | case LUA_T_USERDATA: | ||
568 | sprintf(buff, "userdata: %p", o->value.ts->u.d.v); | ||
569 | break; | ||
570 | case LUA_T_NIL: | ||
571 | lua_pushstring("nil"); | ||
572 | return; | ||
573 | default: | ||
574 | LUA_INTERNALERROR("invalid type"); | ||
575 | } | ||
576 | lua_pushstring(buff); | ||
577 | } | ||
578 | |||
579 | |||
580 | static void luaB_type (void) { | ||
581 | lua_Object o = luaL_nonnullarg(1); | ||
582 | lua_pushstring(luaO_typename(luaA_Address(o))); | ||
583 | lua_pushnumber(lua_tag(o)); | ||
584 | } | ||
585 | |||
586 | /* }====================================================== */ | ||
587 | 575 | ||
588 | 576 | ||
589 | 577 | ||
@@ -658,6 +646,12 @@ static void testC (void) { | |||
658 | case 'I': reg[getnum(s)] = lua_rawgettable(); break; | 646 | case 'I': reg[getnum(s)] = lua_rawgettable(); break; |
659 | case 't': lua_settable(); break; | 647 | case 't': lua_settable(); break; |
660 | case 'T': lua_rawsettable(); break; | 648 | case 'T': lua_rawsettable(); break; |
649 | case 'N' : lua_pushstring(lua_nextvar(lua_getstring(reg[getnum(s)]))); | ||
650 | break; | ||
651 | case 'n' : { int n=getnum(s); | ||
652 | n=lua_next(reg[n], lua_getnumber(reg[getnum(s)])); | ||
653 | lua_pushnumber(n); break; | ||
654 | } | ||
661 | default: luaL_verror("unknown command in `testC': %c", *(s-1)); | 655 | default: luaL_verror("unknown command in `testC': %c", *(s-1)); |
662 | } | 656 | } |
663 | if (*s == 0) return; | 657 | if (*s == 0) return; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 1.19 1999/01/25 12:30:11 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.20 1999/01/25 17:41:19 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -152,28 +152,10 @@ void luaH_set (Hash *t, TObject *ref, TObject *val) { | |||
152 | } | 152 | } |
153 | 153 | ||
154 | 154 | ||
155 | static Node *hashnext (Hash *t, int i) { | 155 | int luaH_pos (Hash *t, TObject *r) { |
156 | Node *n; | 156 | Node *n = luaH_present(t, r); |
157 | int tsize = nhash(t); | 157 | luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found"); |
158 | if (i >= tsize) | 158 | return n-(t->node); |
159 | return NULL; | ||
160 | n = node(t, i); | ||
161 | while (ttype(val(n)) == LUA_T_NIL) { | ||
162 | if (++i >= tsize) | ||
163 | return NULL; | ||
164 | n = node(t, i); | ||
165 | } | ||
166 | return n; | ||
167 | } | ||
168 | |||
169 | Node *luaH_next (Hash *t, TObject *r) { | ||
170 | if (ttype(r) == LUA_T_NIL) | ||
171 | return hashnext(t, 0); | ||
172 | else { | ||
173 | Node *n = luaH_present(t, r); | ||
174 | luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found"); | ||
175 | return hashnext(t, (n-(t->node))+1); | ||
176 | } | ||
177 | } | 159 | } |
178 | 160 | ||
179 | 161 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.h,v 1.9 1999/01/25 12:30:11 roberto Exp roberto $ | 2 | ** $Id: ltable.h,v 1.10 1999/01/25 17:40:10 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -22,7 +22,7 @@ Hash *luaH_new (int nhash); | |||
22 | void luaH_free (Hash *frees); | 22 | void luaH_free (Hash *frees); |
23 | Node *luaH_present (Hash *t, TObject *key); | 23 | Node *luaH_present (Hash *t, TObject *key); |
24 | void luaH_set (Hash *t, TObject *ref, TObject *val); | 24 | void luaH_set (Hash *t, TObject *ref, TObject *val); |
25 | Node *luaH_next (Hash *t, TObject *r); | 25 | int luaH_pos (Hash *t, TObject *r); |
26 | void luaH_setint (Hash *t, int ref, TObject *val); | 26 | void luaH_setint (Hash *t, int ref, TObject *val); |
27 | TObject *luaH_getint (Hash *t, int ref); | 27 | TObject *luaH_getint (Hash *t, int ref); |
28 | 28 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.h,v 1.27 1999/02/09 15:59:22 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.28 1999/02/22 19:13:12 roberto Exp roberto $ |
3 | ** Lua - An Extensible Extension Language | 3 | ** Lua - An Extensible Extension Language |
4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil | 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil |
5 | ** e-mail: lua@tecgraf.puc-rio.br | 5 | ** e-mail: lua@tecgraf.puc-rio.br |
@@ -90,6 +90,8 @@ lua_Object lua_rawgettable (void); /* In: table, index */ | |||
90 | int lua_tag (lua_Object object); | 90 | int lua_tag (lua_Object object); |
91 | 91 | ||
92 | char *lua_nextvar (char *varname); /* Out: value */ | 92 | char *lua_nextvar (char *varname); /* Out: value */ |
93 | int lua_next (lua_Object o, int i); | ||
94 | /* Out: ref, value */ | ||
93 | 95 | ||
94 | int lua_ref (int lock); /* In: value */ | 96 | int lua_ref (int lock); /* In: value */ |
95 | lua_Object lua_getref (int ref); | 97 | lua_Object lua_getref (int ref); |