new constant 'LUA_GNAME' for the name of the global table "_G"

This commit is contained in:
Roberto Ierusalimschy 2017-06-27 15:32:49 -03:00
parent 124bfd2081
commit 5a1c8d8ef3
5 changed files with 14 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.289 2016/12/20 18:37:00 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.290 2017/04/24 18:06:12 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -76,7 +76,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
if (findfield(L, top + 1, 2)) { if (findfield(L, top + 1, 2)) {
const char *name = lua_tostring(L, -1); const char *name = lua_tostring(L, -1);
if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */ if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */
lua_pushstring(L, name + 3); /* push name without prefix */ lua_pushstring(L, name + 3); /* push name without prefix */
lua_remove(L, -2); /* remove original name */ lua_remove(L, -2); /* remove original name */
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.h,v 1.131 2016/12/06 14:54:31 roberto Exp roberto $ ** $Id: lauxlib.h,v 1.132 2017/04/24 18:06:12 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -15,6 +15,10 @@
#include "lua.h" #include "lua.h"
/* global table */
#define LUA_GNAME "_G"
/* extra error code for 'luaL_loadfilex' */ /* extra error code for 'luaL_loadfilex' */
#define LUA_ERRFILE (LUA_ERRERR+1) #define LUA_ERRFILE (LUA_ERRERR+1)

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.315 2017/02/23 21:07:34 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.316 2017/05/26 19:14:29 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -504,7 +504,7 @@ static const luaL_Reg base_funcs[] = {
{"type", luaB_type}, {"type", luaB_type},
{"xpcall", luaB_xpcall}, {"xpcall", luaB_xpcall},
/* placeholders */ /* placeholders */
{"_G", NULL}, {LUA_GNAME, NULL},
{"_VERSION", NULL}, {"_VERSION", NULL},
{NULL, NULL} {NULL, NULL}
}; };
@ -516,7 +516,7 @@ LUAMOD_API int luaopen_base (lua_State *L) {
luaL_setfuncs(L, base_funcs, 0); luaL_setfuncs(L, base_funcs, 0);
/* set global _G */ /* set global _G */
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -2, "_G"); lua_setfield(L, -2, LUA_GNAME);
/* set global _VERSION */ /* set global _VERSION */
lua_pushliteral(L, LUA_VERSION); lua_pushliteral(L, LUA_VERSION);
lua_setfield(L, -2, "_VERSION"); lua_setfield(L, -2, "_VERSION");

View File

@ -1,5 +1,5 @@
/* /*
** $Id: linit.c,v 1.38 2015/01/05 13:48:33 roberto Exp roberto $ ** $Id: linit.c,v 1.39 2016/12/04 20:17:24 roberto Exp roberto $
** Initialization of libraries for lua.c and other clients ** Initialization of libraries for lua.c and other clients
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -40,7 +40,7 @@
** program ** program
*/ */
static const luaL_Reg loadedlibs[] = { static const luaL_Reg loadedlibs[] = {
{"_G", luaopen_base}, {LUA_GNAME, luaopen_base},
{LUA_LOADLIBNAME, luaopen_package}, {LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine}, {LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table}, {LUA_TABLIBNAME, luaopen_table},

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 2.220 2017/06/12 14:21:44 roberto Exp roberto $ ** $Id: ltests.c,v 2.221 2017/06/27 11:35:31 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -960,7 +960,7 @@ static lua_State *getstate (lua_State *L) {
static int loadlib (lua_State *L) { static int loadlib (lua_State *L) {
static const luaL_Reg libs[] = { static const luaL_Reg libs[] = {
{"_G", luaopen_base}, {LUA_GNAME, luaopen_base},
{"coroutine", luaopen_coroutine}, {"coroutine", luaopen_coroutine},
{"debug", luaopen_debug}, {"debug", luaopen_debug},
{"io", luaopen_io}, {"io", luaopen_io},