1
1
/*
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 $
3
3
** Auxiliary functions for building Lua libraries
4
4
** See Copyright Notice in lua.h
5
5
*/
@@ -69,12 +69,11 @@ static int findfield (lua_State *L, int objidx, int level) {
69
69
70
70
/*
71
71
** Search for a name for a function in all loaded modules
72
- ** (registry._LOADED).
73
72
*/
74
73
static int pushglobalfuncname (lua_State * L , lua_Debug * ar ) {
75
74
int top = lua_gettop (L );
76
75
lua_getinfo (L , "f" , ar ); /* push function */
77
- lua_getfield (L , LUA_REGISTRYINDEX , "_LOADED" );
76
+ lua_getfield (L , LUA_REGISTRYINDEX , LUA_LOADED_TABLE );
78
77
if (findfield (L , top + 1 , 2 )) {
79
78
const char * name = lua_tostring (L , -1 );
80
79
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) {
809
808
810
809
811
810
LUALIB_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 {
813
816
switch (lua_type (L , idx )) {
814
817
case LUA_TNUMBER : {
815
818
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 ));
817
820
else
818
- lua_pushfstring (L , "%f" , lua_tonumber (L , idx ));
821
+ lua_pushfstring (L , "%f" , ( LUAI_UACNUMBER ) lua_tonumber (L , idx ));
819
822
break ;
820
823
}
821
824
case LUA_TSTRING :
@@ -827,10 +830,15 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
827
830
case LUA_TNIL :
828
831
lua_pushliteral (L , "nil" );
829
832
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' */
833
840
break ;
841
+ }
834
842
}
835
843
}
836
844
return lua_tolstring (L , -1 , len );
@@ -882,23 +890,23 @@ static int libsize (const luaL_Reg *l) {
882
890
883
891
/*
884
892
** 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
886
894
** global variable with that name. In any case, leaves on the stack
887
895
** the module table.
888
896
*/
889
897
LUALIB_API void luaL_pushmodule (lua_State * L , const char * modname ,
890
898
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]? */
893
901
lua_pop (L , 1 ); /* remove previous result */
894
902
/* try global variable (and create one if it does not exist) */
895
903
lua_pushglobaltable (L );
896
904
if (luaL_findtable (L , 0 , modname , sizehint ) != NULL )
897
905
luaL_error (L , "name conflict for module '%s'" , modname );
898
906
lua_pushvalue (L , -1 );
899
- lua_setfield (L , -3 , modname ); /* _LOADED [modname] = new table */
907
+ lua_setfield (L , -3 , modname ); /* LOADED [modname] = new table */
900
908
}
901
- lua_remove (L , -2 ); /* remove _LOADED table */
909
+ lua_remove (L , -2 ); /* remove LOADED table */
902
910
}
903
911
904
912
@@ -962,17 +970,17 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
962
970
*/
963
971
LUALIB_API void luaL_requiref (lua_State * L , const char * modname ,
964
972
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] */
967
975
if (!lua_toboolean (L , -1 )) { /* package not already loaded? */
968
976
lua_pop (L , 1 ); /* remove field */
969
977
lua_pushcfunction (L , openf );
970
978
lua_pushstring (L , modname ); /* argument to open function */
971
979
lua_call (L , 1 , 1 ); /* call 'openf' to open module */
972
980
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 */
974
982
}
975
- lua_remove (L , -2 ); /* remove _LOADED table */
983
+ lua_remove (L , -2 ); /* remove LOADED table */
976
984
if (glb ) {
977
985
lua_pushvalue (L , -1 ); /* copy of module */
978
986
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) {
1030
1038
luaL_error (L , "multiple Lua VMs detected" );
1031
1039
else if (* v != ver )
1032
1040
luaL_error (L , "version mismatch: app. needs %f, Lua core provides %f" ,
1033
- ver , * v );
1041
+ ( LUAI_UACNUMBER ) ver , ( LUAI_UACNUMBER ) * v );
1034
1042
}
1035
1043
0 commit comments