aboutsummaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-16 10:56:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-16 10:56:59 -0300
commit16b41105215e3cad719ffb121caca0d065e05b6e (patch)
treeca9ab1e9f3506cc6a8ccf4b7de41832360134db2 /lbaselib.c
parentf14662fca63652dc112732b899e3d829bfecb1ae (diff)
downloadlua-16b41105215e3cad719ffb121caca0d065e05b6e.tar.gz
lua-16b41105215e3cad719ffb121caca0d065e05b6e.tar.bz2
lua-16b41105215e3cad719ffb121caca0d065e05b6e.zip
function 'type' keeps type names as upvalues to avoid creating strings
everytime it is called
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/lbaselib.c b/lbaselib.c
index a6e01e9d..ea71660b 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.289 2014/06/10 17:41:38 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.290 2014/06/30 19:48:08 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*/
@@ -200,9 +200,12 @@ static int luaB_collectgarbage (lua_State *L) {
200} 200}
201 201
202 202
203/*
204** This function has all type names as upvalues, to maximize performance.
205*/
203static int luaB_type (lua_State *L) { 206static int luaB_type (lua_State *L) {
204 luaL_checkany(L, 1); 207 luaL_checkany(L, 1);
205 lua_pushstring(L, luaL_typename(L, 1)); 208 lua_pushvalue(L, lua_upvalueindex(lua_type(L, 1) + 1));
206 return 1; 209 return 1;
207} 210}
208 211
@@ -461,21 +464,31 @@ static const luaL_Reg base_funcs[] = {
461 {"setmetatable", luaB_setmetatable}, 464 {"setmetatable", luaB_setmetatable},
462 {"tonumber", luaB_tonumber}, 465 {"tonumber", luaB_tonumber},
463 {"tostring", luaB_tostring}, 466 {"tostring", luaB_tostring},
464 {"type", luaB_type},
465 {"xpcall", luaB_xpcall}, 467 {"xpcall", luaB_xpcall},
468 /* placeholders */
469 {"type", NULL},
470 {"_G", NULL},
471 {"_VERSION", NULL},
466 {NULL, NULL} 472 {NULL, NULL}
467}; 473};
468 474
469 475
470LUAMOD_API int luaopen_base (lua_State *L) { 476LUAMOD_API int luaopen_base (lua_State *L) {
471 /* set global _G */ 477 int i;
472 lua_pushglobaltable(L);
473 lua_pushglobaltable(L);
474 lua_setfield(L, -2, "_G");
475 /* open lib into global table */ 478 /* open lib into global table */
479 lua_pushglobaltable(L);
476 luaL_setfuncs(L, base_funcs, 0); 480 luaL_setfuncs(L, base_funcs, 0);
481 /* set global _G */
482 lua_pushvalue(L, -1);
483 lua_setfield(L, -2, "_G");
484 /* set global _VERSION */
477 lua_pushliteral(L, LUA_VERSION); 485 lua_pushliteral(L, LUA_VERSION);
478 lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ 486 lua_setfield(L, -2, "_VERSION");
487 /* set function 'type' with proper upvalues */
488 for (i = 0; i < LUA_NUMTAGS; i++) /* push all type names as upvalues */
489 lua_pushstring(L, lua_typename(L, i));
490 lua_pushcclosure(L, luaB_type, LUA_NUMTAGS);
491 lua_setfield(L, -2, "type");
479 return 1; 492 return 1;
480} 493}
481 494