summaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-04-02 17:42:20 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-04-02 17:42:20 -0300
commit2cbbf7e95a229cce3033f68807cf3e82021879f8 (patch)
treeb7d17cc8d7abbebf47967cb4dc6ac63b9e3670ae /lbaselib.c
parente5919be1a7a76bc782a66ac19ff1367ede11f4b9 (diff)
downloadlua-2cbbf7e95a229cce3033f68807cf3e82021879f8.tar.gz
lua-2cbbf7e95a229cce3033f68807cf3e82021879f8.tar.bz2
lua-2cbbf7e95a229cce3033f68807cf3e82021879f8.zip
`tostring' uses `__tostring' when available
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c29
1 files changed, 13 insertions, 16 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 9873d88d..dae741b3 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.61 2002/03/27 12:49:53 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.62 2002/03/27 15:30:41 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -122,7 +122,9 @@ static int luaB_error (lua_State *L) {
122static int luaB_metatable (lua_State *L) { 122static int luaB_metatable (lua_State *L) {
123 luaL_check_any(L, 1); 123 luaL_check_any(L, 1);
124 if (lua_isnone(L, 2)) { 124 if (lua_isnone(L, 2)) {
125 if (lua_getmetatable(L, 1)) { 125 if (!lua_getmetatable(L, 1))
126 return 0; /* no metatable */
127 else {
126 lua_pushliteral(L, "__metatable"); 128 lua_pushliteral(L, "__metatable");
127 lua_rawget(L, -2); 129 lua_rawget(L, -2);
128 if (lua_isnil(L, -1)) 130 if (lua_isnil(L, -1))
@@ -361,6 +363,9 @@ static int luaB_call (lua_State *L) {
361 363
362static int luaB_tostring (lua_State *L) { 364static int luaB_tostring (lua_State *L) {
363 char buff[64]; 365 char buff[64];
366 luaL_checkany(L, 1);
367 if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
368 return 1; /* use its value */
364 switch (lua_type(L, 1)) { 369 switch (lua_type(L, 1)) {
365 case LUA_TNUMBER: 370 case LUA_TNUMBER:
366 lua_pushstring(L, lua_tostring(L, 1)); 371 lua_pushstring(L, lua_tostring(L, 1));
@@ -372,25 +377,17 @@ static int luaB_tostring (lua_State *L) {
372 lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); 377 lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
373 return 1; 378 return 1;
374 case LUA_TTABLE: 379 case LUA_TTABLE:
375 sprintf(buff, "%.40s: %p", lua_typename(L, lua_type(L, 1)), 380 sprintf(buff, "table: %p", lua_topointer(L, 1));
376 lua_topointer(L, 1));
377 break; 381 break;
378 case LUA_TFUNCTION: 382 case LUA_TFUNCTION:
379 sprintf(buff, "function: %p", lua_topointer(L, 1)); 383 sprintf(buff, "function: %p", lua_topointer(L, 1));
380 break; 384 break;
381 case LUA_TUSERDATA: { 385 case LUA_TUSERDATA:
382 const char *t = lua_typename(L, lua_type(L, 1)); 386 sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
383 if (strcmp(t, "userdata") == 0)
384 sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
385 else
386 sprintf(buff, "%.40s: %p", t, lua_touserdata(L, 1));
387 break; 387 break;
388 }
389 case LUA_TNIL: 388 case LUA_TNIL:
390 lua_pushliteral(L, "nil"); 389 lua_pushliteral(L, "nil");
391 return 1; 390 return 1;
392 default:
393 luaL_argerror(L, 1, "value expected");
394 } 391 }
395 lua_pushstring(L, buff); 392 lua_pushstring(L, buff);
396 return 1; 393 return 1;
@@ -426,7 +423,7 @@ static const luaL_reg base_funcs[] = {
426static void base_open (lua_State *L) { 423static void base_open (lua_State *L) {
427 lua_pushliteral(L, "_G"); 424 lua_pushliteral(L, "_G");
428 lua_pushvalue(L, LUA_GLOBALSINDEX); 425 lua_pushvalue(L, LUA_GLOBALSINDEX);
429 luaL_openlib(L, base_funcs); /* open lib into global table */ 426 luaL_openlib(L, base_funcs, 0); /* open lib into global table */
430 lua_pushliteral(L, "_VERSION"); 427 lua_pushliteral(L, "_VERSION");
431 lua_pushliteral(L, LUA_VERSION); 428 lua_pushliteral(L, LUA_VERSION);
432 lua_settable(L, -3); /* set global _VERSION */ 429 lua_settable(L, -3); /* set global _VERSION */
@@ -495,7 +492,7 @@ static const luaL_reg co_funcs[] = {
495 492
496 493
497static void co_open (lua_State *L) { 494static void co_open (lua_State *L) {
498 luaL_opennamedlib(L, "co", co_funcs); 495 luaL_opennamedlib(L, "co", co_funcs, 0);
499 /* create metatable for coroutines */ 496 /* create metatable for coroutines */
500 lua_pushliteral(L, "Coroutine"); 497 lua_pushliteral(L, "Coroutine");
501 lua_newtable(L); 498 lua_newtable(L);
@@ -728,7 +725,7 @@ static const luaL_reg array_funcs[] = {
728LUALIB_API int lua_baselibopen (lua_State *L) { 725LUALIB_API int lua_baselibopen (lua_State *L) {
729 base_open(L); 726 base_open(L);
730 co_open(L); 727 co_open(L);
731 luaL_opennamedlib(L, "A", array_funcs); 728 luaL_opennamedlib(L, "tab", array_funcs, 0);
732 /* `require' needs an empty table as upvalue */ 729 /* `require' needs an empty table as upvalue */
733 lua_newtable(L); 730 lua_newtable(L);
734 lua_pushcclosure(L, luaB_require, 1); 731 lua_pushcclosure(L, luaB_require, 1);