aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-07-02 08:38:13 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-07-02 08:38:13 -0300
commit7192afafeeb1a96b3de60af90a72cd8762b09d94 (patch)
treed1e7e061822f755c33cc497d98ea451c7e7e32e8
parenta139e2e003e0b62b7d34eeda20dd2354e74885f9 (diff)
downloadlua-7192afafeeb1a96b3de60af90a72cd8762b09d94.tar.gz
lua-7192afafeeb1a96b3de60af90a72cd8762b09d94.tar.bz2
lua-7192afafeeb1a96b3de60af90a72cd8762b09d94.zip
new module policy: C modules do not create globals and do not register
themselves with 'require' (let 'require' do its work); new auxiliary functions luaL_newlib/luaL_newlibtable/luaL_setfuncs/luaL_requiref. Old luaL_register will be deprecated.
-rw-r--r--lauxlib.c45
-rw-r--r--lauxlib.h22
-rw-r--r--lbaselib.c4
-rw-r--r--lbitlib.c4
-rw-r--r--lcorolib.c4
-rw-r--r--ldblib.c4
-rw-r--r--linit.c21
-rw-r--r--liolib.c7
-rw-r--r--lmathlib.c4
-rw-r--r--loadlib.c4
-rw-r--r--loslib.c4
-rw-r--r--lstrlib.c4
-rw-r--r--ltablib.c4
-rw-r--r--ltests.c28
14 files changed, 104 insertions, 55 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 38e40404..2f0168f1 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.215 2010/06/09 17:53:59 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.216 2010/06/30 17:40:27 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -729,20 +729,33 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
729 luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */ 729 luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
730 lua_insert(L, -(nup + 1)); /* move library table to below upvalues */ 730 lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
731 } 731 }
732 luaL_setfuncs(L, l, nup);
733}
734
735/* }====================================================== */
736
737/*
738** set functions from list 'l' into table at top - 'nup'; each
739** function gets the 'nup' elements at the top as upvalues.
740** Returns with only the table at the stack.
741*/
742LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
732 luaL_checkstack(L, nup, "too many upvalues"); 743 luaL_checkstack(L, nup, "too many upvalues");
733 for (; l && l->name; l++) { /* fill the table with given functions */ 744 for (; l && l->name; l++) { /* fill the table with given functions */
734 int i; 745 int i;
735 for (i = 0; i < nup; i++) /* copy upvalues to the top */ 746 for (i = 0; i < nup; i++) /* copy upvalues to the top */
736 lua_pushvalue(L, -nup); 747 lua_pushvalue(L, -nup);
737 lua_pushcclosure(L, l->func, nup); 748 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
738 lua_setfield(L, -(nup + 2), l->name); 749 lua_setfield(L, -(nup + 2), l->name);
739 } 750 }
740 lua_pop(L, nup); /* remove upvalues */ 751 lua_pop(L, nup); /* remove upvalues */
741} 752}
742 753
743/* }====================================================== */
744
745 754
755/*
756** ensure that stack[idx][fname] has a table and push that table
757** into the stack
758*/
746LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) { 759LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) {
747 lua_getfield(L, idx, fname); 760 lua_getfield(L, idx, fname);
748 if (lua_istable(L, -1)) return; /* table already there */ 761 if (lua_istable(L, -1)) return; /* table already there */
@@ -756,6 +769,30 @@ LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) {
756} 769}
757 770
758 771
772/*
773** stripped-down 'require'. Calls 'openf' to open a module,
774** registers the result in 'package.loaded' table and, if 'glb'
775** is true, also registers the result in the global table.
776** Leaves resulting module on the top.
777*/
778LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
779 lua_CFunction openf, int glb) {
780 lua_pushcfunction(L, openf);
781 lua_pushstring(L, modname); /* argument to open function */
782 lua_call(L, 1, 1); /* open module */
783 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
784 lua_pushvalue(L, -2); /* make copy of module (call result) */
785 lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
786 lua_pop(L, 1); /* remove _LOADED table */
787 if (glb) {
788 lua_pushglobaltable(L);
789 lua_pushvalue(L, -2); /* copy of 'mod' */
790 lua_setfield(L, -2, modname); /* _G[modname] = module */
791 lua_pop(L, 1); /* remove _G table */
792 }
793}
794
795
759LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, 796LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
760 const char *r) { 797 const char *r) {
761 const char *wild; 798 const char *wild;
diff --git a/lauxlib.h b/lauxlib.h
index d5728b58..4d762886 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.106 2010/05/31 16:34:19 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.107 2010/06/30 17:40:27 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -29,10 +29,6 @@ typedef struct luaL_Reg {
29LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); 29LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver);
30#define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) 30#define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM)
31 31
32LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname,
33 int sizehint);
34LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
35 const luaL_Reg *l, int nup);
36LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 32LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
37LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 33LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
38LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 34LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len);
@@ -78,11 +74,15 @@ LUALIB_API int (luaL_len) (lua_State *L, int idx);
78LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 74LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
79 const char *r); 75 const char *r);
80 76
77LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
78
81LUALIB_API void (luaL_findtable) (lua_State *L, int idx, const char *fname); 79LUALIB_API void (luaL_findtable) (lua_State *L, int idx, const char *fname);
82 80
83LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 81LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
84 const char *msg, int level); 82 const char *msg, int level);
85 83
84LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
85 lua_CFunction openf, int glb);
86 86
87/* 87/*
88** =============================================================== 88** ===============================================================
@@ -90,6 +90,12 @@ LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
90** =============================================================== 90** ===============================================================
91*/ 91*/
92 92
93
94#define luaL_newlibtable(L,l) \
95 lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
96
97#define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
98
93#define luaL_argcheck(L, cond,numarg,extramsg) \ 99#define luaL_argcheck(L, cond,numarg,extramsg) \
94 ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 100 ((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
95#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 101#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
@@ -149,6 +155,12 @@ LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz);
149/* }====================================================== */ 155/* }====================================================== */
150 156
151 157
158LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname,
159 int sizehint);
160LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
161 const luaL_Reg *l, int nup);
162
163
152/* compatibility with ref system */ 164/* compatibility with ref system */
153 165
154/* pre-defined references */ 166/* pre-defined references */
diff --git a/lbaselib.c b/lbaselib.c
index a84e682d..9680bb38 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.244 2010/06/10 21:29:47 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.245 2010/06/13 19:41:34 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*/
@@ -496,7 +496,7 @@ LUAMOD_API int luaopen_base (lua_State *L) {
496 lua_pushglobaltable(L); 496 lua_pushglobaltable(L);
497 lua_setfield(L, -2, "_G"); 497 lua_setfield(L, -2, "_G");
498 /* open lib into global table */ 498 /* open lib into global table */
499 luaL_register(L, "_G", base_funcs); 499 luaL_setfuncs(L, base_funcs, 0);
500 lua_pushliteral(L, LUA_VERSION); 500 lua_pushliteral(L, LUA_VERSION);
501 lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ 501 lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
502 /* `newproxy' needs a weaktable as upvalue */ 502 /* `newproxy' needs a weaktable as upvalue */
diff --git a/lbitlib.c b/lbitlib.c
index 3b5f5b83..cae1b1f5 100644
--- a/lbitlib.c
+++ b/lbitlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbitlib.c,v 1.3 2010/01/12 19:40:02 roberto Exp roberto $ 2** $Id: lbitlib.c,v 1.4 2010/02/11 15:55:29 roberto Exp roberto $
3** Standard library for bitwise operations 3** Standard library for bitwise operations
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -141,6 +141,6 @@ static const luaL_Reg bitlib[] = {
141 141
142 142
143LUAMOD_API int luaopen_bit (lua_State *L) { 143LUAMOD_API int luaopen_bit (lua_State *L) {
144 luaL_register(L, LUA_BITLIBNAME, bitlib); 144 luaL_newlib(L, bitlib);
145 return 1; 145 return 1;
146} 146}
diff --git a/lcorolib.c b/lcorolib.c
index fdcbc6de..1eef89c9 100644
--- a/lcorolib.c
+++ b/lcorolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcorolib.c,v $ 2** $Id: lcorolib.c,v 1.1 2010/06/10 21:30:26 roberto Exp roberto $
3** Coroutine Library 3** Coroutine Library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -148,7 +148,7 @@ static const luaL_Reg co_funcs[] = {
148 148
149 149
150LUAMOD_API int luaopen_coroutine (lua_State *L) { 150LUAMOD_API int luaopen_coroutine (lua_State *L) {
151 luaL_register(L, LUA_COLIBNAME, co_funcs); 151 luaL_newlib(L, co_funcs);
152 return 1; 152 return 1;
153} 153}
154 154
diff --git a/ldblib.c b/ldblib.c
index d6d58233..6afc1fc1 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.121 2010/03/26 20:58:11 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.122 2010/06/21 16:30:12 roberto Exp roberto $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -395,7 +395,7 @@ static const luaL_Reg dblib[] = {
395 395
396 396
397LUAMOD_API int luaopen_debug (lua_State *L) { 397LUAMOD_API int luaopen_debug (lua_State *L) {
398 luaL_register(L, LUA_DBLIBNAME, dblib); 398 luaL_newlib(L, dblib);
399 return 1; 399 return 1;
400} 400}
401 401
diff --git a/linit.c b/linit.c
index 6c803270..2779e9b1 100644
--- a/linit.c
+++ b/linit.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: linit.c,v 1.26 2010/06/10 21:29:47 roberto Exp roberto $ 2** $Id: linit.c,v 1.27 2010/06/30 17:40:27 roberto Exp roberto $
3** Initialization of libraries for lua.c and other clients 3** Initialization of libraries for lua.c and other clients
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -36,6 +36,9 @@ static const luaL_Reg loadedlibs[] = {
36 {LUA_STRLIBNAME, luaopen_string}, 36 {LUA_STRLIBNAME, luaopen_string},
37 {LUA_BITLIBNAME, luaopen_bit}, 37 {LUA_BITLIBNAME, luaopen_bit},
38 {LUA_MATHLIBNAME, luaopen_math}, 38 {LUA_MATHLIBNAME, luaopen_math},
39#if defined(LUA_COMPAT_DEBUGLIB)
40 {LUA_DBLIBNAME, luaopen_debug},
41#endif
39 {NULL, NULL} 42 {NULL, NULL}
40}; 43};
41 44
@@ -51,25 +54,17 @@ static const luaL_Reg preloadedlibs[] = {
51 54
52LUALIB_API void luaL_openlibs (lua_State *L) { 55LUALIB_API void luaL_openlibs (lua_State *L) {
53 const luaL_Reg *lib; 56 const luaL_Reg *lib;
54 /* call open functions from 'loadedlibs' */ 57 /* call open functions from 'loadedlibs' and set results to global table */
55 for (lib = loadedlibs; lib->func; lib++) { 58 for (lib = loadedlibs; lib->func; lib++) {
56 lua_pushcfunction(L, lib->func); 59 luaL_requiref(L, lib->name, lib->func, 1);
57 lua_pushstring(L, lib->name); 60 lua_pop(L, 1); /* remove lib */
58 lua_call(L, 1, 0);
59 } 61 }
60 /* add open functions from 'preloadedlibs' into 'package.preload' table */ 62 /* add open functions from 'preloadedlibs' into 'package.preload' table */
61 lua_pushglobaltable(L);
62 luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 63 luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
63 for (lib = preloadedlibs; lib->func; lib++) { 64 for (lib = preloadedlibs; lib->func; lib++) {
64 lua_pushcfunction(L, lib->func); 65 lua_pushcfunction(L, lib->func);
65 lua_setfield(L, -2, lib->name); 66 lua_setfield(L, -2, lib->name);
66 } 67 }
67 lua_pop(L, 1); /* remove package.preload table */ 68 lua_pop(L, 1); /* remove _PRELOAD table */
68#if defined(LUA_COMPAT_DEBUGLIB)
69 lua_getglobal(L, "require");
70 lua_pushliteral(L, LUA_DBLIBNAME);
71 lua_call(L, 1, 0); /* call 'require"debug"' */
72 lua_pop(L, 1); /* remove global table */
73#endif
74} 69}
75 70
diff --git a/liolib.c b/liolib.c
index 6a290098..a26aff5a 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.87 2010/03/17 21:37:37 roberto Exp roberto $ 2** $Id: liolib.c,v 2.88 2010/03/26 20:58:11 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -615,8 +615,9 @@ LUAMOD_API int luaopen_io (lua_State *L) {
615 createmeta(L); 615 createmeta(L);
616 /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ 616 /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
617 newenv(L, io_fclose); /* upvalue for all io functions at index 1 */ 617 newenv(L, io_fclose); /* upvalue for all io functions at index 1 */
618 lua_pushvalue(L, -1); /* copy to be consumed by 'openlib' */ 618 luaL_newlibtable(L, iolib); /* new module at index 2 */
619 luaL_openlib(L, LUA_IOLIBNAME, iolib, 1); /* new module at index 2 */ 619 lua_pushvalue(L, 1); /* copy of env to be consumed by 'setfuncs' */
620 luaL_setfuncs(L, iolib, 1);
620 /* create (and set) default files */ 621 /* create (and set) default files */
621 newenv(L, io_noclose); /* environment for default files at index 3 */ 622 newenv(L, io_noclose); /* environment for default files at index 3 */
622 createstdfile(L, stdin, IO_INPUT, "stdin"); 623 createstdfile(L, stdin, IO_INPUT, "stdin");
diff --git a/lmathlib.c b/lmathlib.c
index 391a31e7..83eaf22e 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.73 2009/03/17 17:55:39 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.74 2009/11/24 12:05:44 roberto Exp roberto $
3** Standard mathematical library 3** Standard mathematical library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -263,7 +263,7 @@ static const luaL_Reg mathlib[] = {
263** Open math library 263** Open math library
264*/ 264*/
265LUAMOD_API int luaopen_math (lua_State *L) { 265LUAMOD_API int luaopen_math (lua_State *L) {
266 luaL_register(L, LUA_MATHLIBNAME, mathlib); 266 luaL_newlib(L, mathlib);
267 lua_pushnumber(L, PI); 267 lua_pushnumber(L, PI);
268 lua_setfield(L, -2, "pi"); 268 lua_setfield(L, -2, "pi");
269 lua_pushnumber(L, HUGE_VAL); 269 lua_pushnumber(L, HUGE_VAL);
diff --git a/loadlib.c b/loadlib.c
index bb517801..712fcfef 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.85 2010/06/18 17:23:02 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.86 2010/06/30 17:40:27 roberto Exp roberto $
3** Dynamic library loader for Lua 3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5** 5**
@@ -616,7 +616,7 @@ LUAMOD_API int luaopen_package (lua_State *L) {
616 lua_pushcfunction(L, gctm); 616 lua_pushcfunction(L, gctm);
617 lua_setfield(L, -2, "__gc"); 617 lua_setfield(L, -2, "__gc");
618 /* create `package' table */ 618 /* create `package' table */
619 luaL_register(L, LUA_LOADLIBNAME, pk_funcs); 619 luaL_newlib(L, pk_funcs);
620 /* create `loaders' table */ 620 /* create `loaders' table */
621 lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); 621 lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
622 /* fill it with pre-defined loaders */ 622 /* fill it with pre-defined loaders */
diff --git a/loslib.c b/loslib.c
index c51ce5eb..2c378e9b 100644
--- a/loslib.c
+++ b/loslib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loslib.c,v 1.28 2009/12/17 12:26:09 roberto Exp roberto $ 2** $Id: loslib.c,v 1.29 2009/12/17 13:08:51 roberto Exp roberto $
3** Standard Operating System library 3** Standard Operating System library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -304,7 +304,7 @@ static const luaL_Reg syslib[] = {
304 304
305 305
306LUAMOD_API int luaopen_os (lua_State *L) { 306LUAMOD_API int luaopen_os (lua_State *L) {
307 luaL_register(L, LUA_OSLIBNAME, syslib); 307 luaL_newlib(L, syslib);
308 return 1; 308 return 1;
309} 309}
310 310
diff --git a/lstrlib.c b/lstrlib.c
index 565d913e..79b3ab37 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.152 2010/05/04 17:20:33 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.153 2010/05/24 19:34:57 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -913,7 +913,7 @@ static void createmetatable (lua_State *L) {
913** Open string library 913** Open string library
914*/ 914*/
915LUAMOD_API int luaopen_string (lua_State *L) { 915LUAMOD_API int luaopen_string (lua_State *L) {
916 luaL_register(L, LUA_STRLIBNAME, strlib); 916 luaL_newlib(L, strlib);
917 createmetatable(L); 917 createmetatable(L);
918 return 1; 918 return 1;
919} 919}
diff --git a/ltablib.c b/ltablib.c
index 533f9ac9..93cc3442 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltablib.c,v 1.54 2010/01/13 19:59:10 roberto Exp roberto $ 2** $Id: ltablib.c,v 1.55 2010/03/13 03:57:46 roberto Exp roberto $
3** Library for Table Manipulation 3** Library for Table Manipulation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -324,7 +324,7 @@ static const luaL_Reg tab_funcs[] = {
324 324
325 325
326LUAMOD_API int luaopen_table (lua_State *L) { 326LUAMOD_API int luaopen_table (lua_State *L) {
327 luaL_register(L, LUA_TABLIBNAME, tab_funcs); 327 luaL_newlib(L, tab_funcs);
328#if defined(LUA_COMPAT_UNPACK) 328#if defined(LUA_COMPAT_UNPACK)
329 /* _G.unpack = table.unpack */ 329 /* _G.unpack = table.unpack */
330 lua_getfield(L, -1, "unpack"); 330 lua_getfield(L, -1, "unpack");
diff --git a/ltests.c b/ltests.c
index 7bafe829..ec70a954 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.109 2010/06/10 21:29:47 roberto Exp roberto $ 2** $Id: ltests.c,v 2.110 2010/06/25 12:18:10 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -841,19 +841,23 @@ static lua_State *getstate (lua_State *L) {
841 841
842static int loadlib (lua_State *L) { 842static int loadlib (lua_State *L) {
843 static const luaL_Reg libs[] = { 843 static const luaL_Reg libs[] = {
844 {"baselibopen", luaopen_base}, 844 {"_G", luaopen_base},
845 {"corolibopen", luaopen_coroutine}, 845 {"coroutine", luaopen_coroutine},
846 {"dblibopen", luaopen_debug}, 846 {"debug", luaopen_debug},
847 {"iolibopen", luaopen_io}, 847 {"io", luaopen_io},
848 {"mathlibopen", luaopen_math}, 848 {"math", luaopen_math},
849 {"strlibopen", luaopen_string}, 849 {"string", luaopen_string},
850 {"tablibopen", luaopen_table}, 850 {"table", luaopen_table},
851 {"packageopen", luaopen_package},
852 {NULL, NULL} 851 {NULL, NULL}
853 }; 852 };
854 lua_State *L1 = getstate(L); 853 lua_State *L1 = getstate(L);
855 lua_pushglobaltable(L1); 854 int i;
856 luaL_register(L1, NULL, libs); 855 luaL_requiref(L1, "package", luaopen_package, 1);
856 luaL_findtable(L1, LUA_REGISTRYINDEX, "_PRELOAD");
857 for (i = 0; libs[i].name; i++) {
858 lua_pushcfunction(L1, libs[i].func);
859 lua_setfield(L1, -2, libs[i].name);
860 }
857 return 0; 861 return 0;
858} 862}
859 863
@@ -874,8 +878,8 @@ static int doremote (lua_State *L) {
874 status = lua_pcall(L1, 0, LUA_MULTRET, 0); 878 status = lua_pcall(L1, 0, LUA_MULTRET, 0);
875 if (status != LUA_OK) { 879 if (status != LUA_OK) {
876 lua_pushnil(L); 880 lua_pushnil(L);
877 lua_pushinteger(L, status);
878 lua_pushstring(L, lua_tostring(L1, -1)); 881 lua_pushstring(L, lua_tostring(L1, -1));
882 lua_pushinteger(L, status);
879 return 3; 883 return 3;
880 } 884 }
881 else { 885 else {