diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-11-04 15:23:12 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-11-04 15:23:12 -0200 |
commit | cde179b36979c58d9380d3c4dd29b61412d13b51 (patch) | |
tree | 804c457691024d797a7479eddf711e103966b513 /lbuiltin.c | |
parent | 80b39d83c3512e1dd627842e64c69841638d9088 (diff) | |
download | lua-cde179b36979c58d9380d3c4dd29b61412d13b51.tar.gz lua-cde179b36979c58d9380d3c4dd29b61412d13b51.tar.bz2 lua-cde179b36979c58d9380d3c4dd29b61412d13b51.zip |
new implementation for global variable values (separated from strings)
Diffstat (limited to 'lbuiltin.c')
-rw-r--r-- | lbuiltin.c | 61 |
1 files changed, 35 insertions, 26 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.68 1999/10/19 13:33:22 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.69 1999/10/26 10:53:40 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 | */ |
@@ -35,10 +35,9 @@ | |||
35 | 35 | ||
36 | 36 | ||
37 | static void pushtagstring (TaggedString *s) { | 37 | static void pushtagstring (TaggedString *s) { |
38 | TObject o; | 38 | ttype(L->stack.top) = LUA_T_STRING; |
39 | o.ttype = LUA_T_STRING; | 39 | tsvalue(L->stack.top) = s; |
40 | o.value.ts = s; | 40 | incr_top; |
41 | luaA_pushobject(&o); | ||
42 | } | 41 | } |
43 | 42 | ||
44 | 43 | ||
@@ -107,7 +106,7 @@ static void error_message (void) { | |||
107 | 106 | ||
108 | 107 | ||
109 | /* | 108 | /* |
110 | ** If your system does not support "stdout", just remove this function. | 109 | ** If your system does not support "stdout", you can just remove this function. |
111 | ** If you need, you can define your own "print" function, following this | 110 | ** If you need, you can define your own "print" function, following this |
112 | ** model but changing "fputs" to put the strings at a proper place | 111 | ** model but changing "fputs" to put the strings at a proper place |
113 | ** (a console window or a log file, for instance). | 112 | ** (a console window or a log file, for instance). |
@@ -264,22 +263,29 @@ static void luaB_type (void) { | |||
264 | ** ======================================================= | 263 | ** ======================================================= |
265 | */ | 264 | */ |
266 | 265 | ||
266 | |||
267 | static void passresults (void) { | ||
268 | L->Cstack.base = L->Cstack.lua2C; /* position of first result */ | ||
269 | if (L->Cstack.num == 0) | ||
270 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | ||
271 | } | ||
272 | |||
267 | static void luaB_dostring (void) { | 273 | static void luaB_dostring (void) { |
268 | long l; | 274 | long l; |
269 | const char *s = luaL_check_lstr(1, &l); | 275 | const char *s = luaL_check_lstr(1, &l); |
270 | if (*s == ID_CHUNK) | 276 | if (*s == ID_CHUNK) |
271 | lua_error("`dostring' cannot run pre-compiled code"); | 277 | lua_error("`dostring' cannot run pre-compiled code"); |
272 | if (lua_dobuffer(s, l, luaL_opt_string(2, s)) == 0) | 278 | if (lua_dobuffer(s, l, luaL_opt_string(2, s)) == 0) |
273 | if (luaA_passresults() == 0) | 279 | passresults(); |
274 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | 280 | /* else return no value */ |
275 | } | 281 | } |
276 | 282 | ||
277 | 283 | ||
278 | static void luaB_dofile (void) { | 284 | static void luaB_dofile (void) { |
279 | const char *fname = luaL_opt_string(1, NULL); | 285 | const char *fname = luaL_opt_string(1, NULL); |
280 | if (lua_dofile(fname) == 0) | 286 | if (lua_dofile(fname) == 0) |
281 | if (luaA_passresults() == 0) | 287 | passresults(); |
282 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | 288 | /* else return no value */ |
283 | } | 289 | } |
284 | 290 | ||
285 | 291 | ||
@@ -312,10 +318,12 @@ static void luaB_call (void) { | |||
312 | lua_error(NULL); | 318 | lua_error(NULL); |
313 | } | 319 | } |
314 | else { /* no errors */ | 320 | else { /* no errors */ |
315 | if (strchr(options, 'p')) | 321 | if (strchr(options, 'p')) { /* pack results? */ |
316 | luaA_packresults(); | 322 | luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top); |
323 | incr_top; | ||
324 | } | ||
317 | else | 325 | else |
318 | luaA_passresults(); | 326 | L->Cstack.base = L->Cstack.lua2C; /* position of first result */ |
319 | } | 327 | } |
320 | } | 328 | } |
321 | 329 | ||
@@ -373,7 +381,7 @@ static void luaB_tostring (void) { | |||
373 | sprintf(buff, "function: %p", (void *)o->value.f); | 381 | sprintf(buff, "function: %p", (void *)o->value.f); |
374 | break; | 382 | break; |
375 | case LUA_T_USERDATA: | 383 | case LUA_T_USERDATA: |
376 | sprintf(buff, "userdata: %p", o->value.ts->u.d.v); | 384 | sprintf(buff, "userdata: %p", o->value.ts->u.d.value); |
377 | break; | 385 | break; |
378 | case LUA_T_NIL: | 386 | case LUA_T_NIL: |
379 | lua_pushstring("nil"); | 387 | lua_pushstring("nil"); |
@@ -449,23 +457,23 @@ static void luaB_foreach (void) { | |||
449 | 457 | ||
450 | 458 | ||
451 | static void luaB_foreachvar (void) { | 459 | static void luaB_foreachvar (void) { |
452 | TaggedString *s; | 460 | GlobalVar *gv; |
453 | TObject f; /* see comment in 'foreachi' */ | 461 | TObject f; /* see comment in 'foreachi' */ |
454 | f = *luaA_Address(luaL_functionarg(1)); | 462 | f = *luaA_Address(luaL_functionarg(1)); |
455 | luaD_checkstack(4); /* for extra var name, f, var name, and globalval */ | 463 | luaD_checkstack(4); /* for extra var name, f, var name, and globalval */ |
456 | for (s = L->rootglobal; s; s = s->nextglobal) { | 464 | for (gv = L->rootglobal; gv; gv = gv->next) { |
457 | if (s->u.s.globalval.ttype != LUA_T_NIL) { | 465 | if (gv->value.ttype != LUA_T_NIL) { |
458 | pushtagstring(s); /* keep (extra) s on stack to avoid GC */ | 466 | pushtagstring(gv->name); /* keep (extra) name on stack to avoid GC */ |
459 | *(L->stack.top++) = f; | 467 | *(L->stack.top++) = f; |
460 | pushtagstring(s); | 468 | pushtagstring(gv->name); |
461 | *(L->stack.top++) = s->u.s.globalval; | 469 | *(L->stack.top++) = gv->value; |
462 | luaD_calln(2, 1); | 470 | luaD_calln(2, 1); |
463 | if (ttype(L->stack.top-1) != LUA_T_NIL) { | 471 | if (ttype(L->stack.top-1) != LUA_T_NIL) { |
464 | L->stack.top--; | 472 | L->stack.top--; |
465 | *(L->stack.top-1) = *L->stack.top; /* remove extra `s' */ | 473 | *(L->stack.top-1) = *L->stack.top; /* remove extra name */ |
466 | return; | 474 | return; |
467 | } | 475 | } |
468 | L->stack.top-=2; /* remove result and extra `s' */ | 476 | L->stack.top-=2; /* remove result and extra name */ |
469 | } | 477 | } |
470 | } | 478 | } |
471 | } | 479 | } |
@@ -507,7 +515,8 @@ static void luaB_tremove (void) { | |||
507 | } | 515 | } |
508 | 516 | ||
509 | 517 | ||
510 | /* { | 518 | /* |
519 | ** {====================================================== | ||
511 | ** Quicksort | 520 | ** Quicksort |
512 | */ | 521 | */ |
513 | 522 | ||
@@ -593,11 +602,10 @@ static void luaB_sort (void) { | |||
593 | lua_pushobject(t); | 602 | lua_pushobject(t); |
594 | } | 603 | } |
595 | 604 | ||
596 | /* }}===================================================== */ | 605 | /* }====================================================== */ |
597 | 606 | ||
598 | 607 | ||
599 | /* | 608 | /* }====================================================== */ |
600 | ** ====================================================== */ | ||
601 | 609 | ||
602 | 610 | ||
603 | 611 | ||
@@ -605,6 +613,7 @@ static void luaB_sort (void) { | |||
605 | /* | 613 | /* |
606 | ** {====================================================== | 614 | ** {====================================================== |
607 | ** some DEBUG functions | 615 | ** some DEBUG functions |
616 | ** (for internal debugging of the Lua implementation) | ||
608 | ** ======================================================= | 617 | ** ======================================================= |
609 | */ | 618 | */ |
610 | 619 | ||