11/*
2- ** $Id: lauxlib.c,v 1.286 2016/01/08 15:33:09 roberto Exp $
2+ ** $Id: lauxlib.c,v 1.289 2016/12/20 18:37:00 roberto Exp $
33** Auxiliary functions for building Lua libraries
44** See Copyright Notice in lua.h
55*/
@@ -69,12 +69,11 @@ static int findfield (lua_State *L, int objidx, int level) {
6969
7070/*
7171** Search for a name for a function in all loaded modules
72- ** (registry._LOADED).
7372*/
7473static int pushglobalfuncname (lua_State * L , lua_Debug * ar ) {
7574 int top = lua_gettop (L );
7675 lua_getinfo (L , "f" , ar ); /* push function */
77- lua_getfield (L , LUA_REGISTRYINDEX , "_LOADED" );
76+ lua_getfield (L , LUA_REGISTRYINDEX , LUA_LOADED_TABLE );
7877 if (findfield (L , top + 1 , 2 )) {
7978 const char * name = lua_tostring (L , -1 );
8079 if (strncmp (name , "_G." , 3 ) == 0 ) { /* name start with '_G.'? */
@@ -809,13 +808,17 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
809808
810809
811810LUALIB_API const char * luaL_tolstring (lua_State * L , int idx , size_t * len ) {
812- if (!luaL_callmeta (L , idx , "__tostring" )) { /* no metafield? */
811+ if (luaL_callmeta (L , idx , "__tostring" )) { /* metafield? */
812+ if (!lua_isstring (L , -1 ))
813+ luaL_error (L , "'__tostring' must return a string" );
814+ }
815+ else {
813816 switch (lua_type (L , idx )) {
814817 case LUA_TNUMBER : {
815818 if (lua_isinteger (L , idx ))
816- lua_pushfstring (L , "%I" , lua_tointeger (L , idx ));
819+ lua_pushfstring (L , "%I" , ( LUAI_UACINT ) lua_tointeger (L , idx ));
817820 else
818- lua_pushfstring (L , "%f" , lua_tonumber (L , idx ));
821+ lua_pushfstring (L , "%f" , ( LUAI_UACNUMBER ) lua_tonumber (L , idx ));
819822 break ;
820823 }
821824 case LUA_TSTRING :
@@ -827,10 +830,15 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
827830 case LUA_TNIL :
828831 lua_pushliteral (L , "nil" );
829832 break ;
830- default :
831- lua_pushfstring (L , "%s: %p" , luaL_typename (L , idx ),
832- lua_topointer (L , idx ));
833+ default : {
834+ int tt = luaL_getmetafield (L , idx , "__name" ); /* try name */
835+ const char * kind = (tt == LUA_TSTRING ) ? lua_tostring (L , -1 ) :
836+ luaL_typename (L , idx );
837+ lua_pushfstring (L , "%s: %p" , kind , lua_topointer (L , idx ));
838+ if (tt != LUA_TNIL )
839+ lua_remove (L , -2 ); /* remove '__name' */
833840 break ;
841+ }
834842 }
835843 }
836844 return lua_tolstring (L , -1 , len );
@@ -882,23 +890,23 @@ static int libsize (const luaL_Reg *l) {
882890
883891/*
884892** Find or create a module table with a given name. The function
885- ** first looks at the _LOADED table and, if that fails, try a
893+ ** first looks at the LOADED table and, if that fails, try a
886894** global variable with that name. In any case, leaves on the stack
887895** the module table.
888896*/
889897LUALIB_API void luaL_pushmodule (lua_State * L , const char * modname ,
890898 int sizehint ) {
891- luaL_findtable (L , LUA_REGISTRYINDEX , "_LOADED" , 1 ); /* get _LOADED table */
892- if (lua_getfield (L , -1 , modname ) != LUA_TTABLE ) { /* no _LOADED [modname]? */
899+ luaL_findtable (L , LUA_REGISTRYINDEX , LUA_LOADED_TABLE , 1 );
900+ if (lua_getfield (L , -1 , modname ) != LUA_TTABLE ) { /* no LOADED [modname]? */
893901 lua_pop (L , 1 ); /* remove previous result */
894902 /* try global variable (and create one if it does not exist) */
895903 lua_pushglobaltable (L );
896904 if (luaL_findtable (L , 0 , modname , sizehint ) != NULL )
897905 luaL_error (L , "name conflict for module '%s'" , modname );
898906 lua_pushvalue (L , -1 );
899- lua_setfield (L , -3 , modname ); /* _LOADED [modname] = new table */
907+ lua_setfield (L , -3 , modname ); /* LOADED [modname] = new table */
900908 }
901- lua_remove (L , -2 ); /* remove _LOADED table */
909+ lua_remove (L , -2 ); /* remove LOADED table */
902910}
903911
904912
@@ -962,17 +970,17 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
962970*/
963971LUALIB_API void luaL_requiref (lua_State * L , const char * modname ,
964972 lua_CFunction openf , int glb ) {
965- luaL_getsubtable (L , LUA_REGISTRYINDEX , "_LOADED" );
966- lua_getfield (L , -1 , modname ); /* _LOADED [modname] */
973+ luaL_getsubtable (L , LUA_REGISTRYINDEX , LUA_LOADED_TABLE );
974+ lua_getfield (L , -1 , modname ); /* LOADED [modname] */
967975 if (!lua_toboolean (L , -1 )) { /* package not already loaded? */
968976 lua_pop (L , 1 ); /* remove field */
969977 lua_pushcfunction (L , openf );
970978 lua_pushstring (L , modname ); /* argument to open function */
971979 lua_call (L , 1 , 1 ); /* call 'openf' to open module */
972980 lua_pushvalue (L , -1 ); /* make copy of module (call result) */
973- lua_setfield (L , -3 , modname ); /* _LOADED [modname] = module */
981+ lua_setfield (L , -3 , modname ); /* LOADED [modname] = module */
974982 }
975- lua_remove (L , -2 ); /* remove _LOADED table */
983+ lua_remove (L , -2 ); /* remove LOADED table */
976984 if (glb ) {
977985 lua_pushvalue (L , -1 ); /* copy of module */
978986 lua_setglobal (L , modname ); /* _G[modname] = module */
@@ -1030,6 +1038,6 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
10301038 luaL_error (L , "multiple Lua VMs detected" );
10311039 else if (* v != ver )
10321040 luaL_error (L , "version mismatch: app. needs %f, Lua core provides %f" ,
1033- ver , * v );
1041+ ( LUAI_UACNUMBER ) ver , ( LUAI_UACNUMBER ) * v );
10341042}
10351043
0 commit comments