diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-03-20 09:54:08 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-03-20 09:54:08 -0300 |
commit | 88c9bf99de50df5575cce13e8e6c278b9f1dc0d0 (patch) | |
tree | 3cfe2080f0a2595b62b74752c84ffa763e83f8e6 | |
parent | 63a614e1453b6b03b89b5d47efa476acd5f9d1d2 (diff) | |
download | lua-88c9bf99de50df5575cce13e8e6c278b9f1dc0d0.tar.gz lua-88c9bf99de50df5575cce13e8e6c278b9f1dc0d0.tar.bz2 lua-88c9bf99de50df5575cce13e8e6c278b9f1dc0d0.zip |
standard libraries in packages
-rw-r--r-- | lauxlib.c | 21 | ||||
-rw-r--r-- | lauxlib.h | 7 | ||||
-rw-r--r-- | lbaselib.c | 148 | ||||
-rw-r--r-- | ldblib.c | 94 | ||||
-rw-r--r-- | liolib.c | 160 | ||||
-rw-r--r-- | lmathlib.c | 60 | ||||
-rw-r--r-- | lstrlib.c | 30 | ||||
-rw-r--r-- | ltests.c | 14 |
8 files changed, 287 insertions, 247 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.60 2002/02/14 21:41:53 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.61 2002/03/07 18:15:10 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 | */ |
@@ -116,10 +116,21 @@ LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) { | |||
116 | } | 116 | } |
117 | 117 | ||
118 | 118 | ||
119 | LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n) { | 119 | LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l) { |
120 | int i; | 120 | for (; l->name; l++) { |
121 | for (i=0; i<n; i++) | 121 | lua_pushstring(L, l->name); |
122 | lua_register(L, l[i].name, l[i].func); | 122 | lua_pushcfunction(L, l->func); |
123 | lua_settable(L, -3); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | |||
128 | LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, | ||
129 | const luaL_reg *l) { | ||
130 | lua_pushstring(L, libname); | ||
131 | lua_newtable(L); | ||
132 | luaL_openlib(L, l); | ||
133 | lua_settable(L, LUA_GLOBALSINDEX); | ||
123 | } | 134 | } |
124 | 135 | ||
125 | 136 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ | 2 | ** $Id: lauxlib.h,v 1.42 2002/02/05 22:36:52 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 | */ |
@@ -27,7 +27,9 @@ typedef struct luaL_reg { | |||
27 | } luaL_reg; | 27 | } luaL_reg; |
28 | 28 | ||
29 | 29 | ||
30 | LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n); | 30 | LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l); |
31 | LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, | ||
32 | const luaL_reg *l); | ||
31 | LUALIB_API void luaL_typerror (lua_State *L, int narg, const char *tname); | 33 | LUALIB_API void luaL_typerror (lua_State *L, int narg, const char *tname); |
32 | LUALIB_API void luaL_argerror (lua_State *L, int numarg, | 34 | LUALIB_API void luaL_argerror (lua_State *L, int numarg, |
33 | const char *extramsg); | 35 | const char *extramsg); |
@@ -67,7 +69,6 @@ LUALIB_API const char *luaL_errstr (int errcode); | |||
67 | #define luaL_check_long(L,n) ((long)luaL_check_number(L, n)) | 69 | #define luaL_check_long(L,n) ((long)luaL_check_number(L, n)) |
68 | #define luaL_opt_int(L,n,d) ((int)luaL_opt_number(L, n,d)) | 70 | #define luaL_opt_int(L,n,d) ((int)luaL_opt_number(L, n,d)) |
69 | #define luaL_opt_long(L,n,d) ((long)luaL_opt_number(L, n,d)) | 71 | #define luaL_opt_long(L,n,d) ((long)luaL_opt_number(L, n,d)) |
70 | #define luaL_openl(L,a) luaL_openlib(L, a, (sizeof(a)/sizeof(a[0]))) | ||
71 | 72 | ||
72 | 73 | ||
73 | /* | 74 | /* |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ | 2 | ** $Id: lbaselib.c,v 1.59 2002/02/14 21:42:22 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 | */ |
@@ -19,12 +19,6 @@ | |||
19 | 19 | ||
20 | 20 | ||
21 | 21 | ||
22 | static void aux_setn (lua_State *L, int t, int n) { | ||
23 | lua_pushliteral(L, "n"); | ||
24 | lua_pushnumber(L, n); | ||
25 | lua_rawset(L, t); | ||
26 | } | ||
27 | |||
28 | 22 | ||
29 | /* | 23 | /* |
30 | ** If your system does not support `stderr', redefine this function, or | 24 | ** If your system does not support `stderr', redefine this function, or |
@@ -124,17 +118,6 @@ static int luaB_error (lua_State *L) { | |||
124 | return 0; /* to avoid warnings */ | 118 | return 0; /* to avoid warnings */ |
125 | } | 119 | } |
126 | 120 | ||
127 | static int luaB_setglobal (lua_State *L) { | ||
128 | luaL_check_any(L, 2); | ||
129 | lua_setglobal(L, luaL_check_string(L, 1)); | ||
130 | return 0; | ||
131 | } | ||
132 | |||
133 | static int luaB_getglobal (lua_State *L) { | ||
134 | lua_getglobal(L, luaL_check_string(L, 1)); | ||
135 | return 1; | ||
136 | } | ||
137 | |||
138 | 121 | ||
139 | static int luaB_metatable (lua_State *L) { | 122 | static int luaB_metatable (lua_State *L) { |
140 | luaL_check_type(L, 1, LUA_TTABLE); | 123 | luaL_check_type(L, 1, LUA_TTABLE); |
@@ -191,12 +174,7 @@ static int luaB_collectgarbage (lua_State *L) { | |||
191 | 174 | ||
192 | static int luaB_type (lua_State *L) { | 175 | static int luaB_type (lua_State *L) { |
193 | luaL_check_any(L, 1); | 176 | luaL_check_any(L, 1); |
194 | if (lua_isnone(L, 2)) | 177 | lua_pushstring(L, lua_typename(L, lua_type(L, 1))); |
195 | lua_pushstring(L, lua_typename(L, lua_type(L, 1))); | ||
196 | else { | ||
197 | lua_pushboolean(L, | ||
198 | (strcmp(lua_typename(L, lua_type(L, 1)), luaL_check_string(L, 2)) == 0)); | ||
199 | } | ||
200 | return 1; | 178 | return 1; |
201 | } | 179 | } |
202 | 180 | ||
@@ -248,6 +226,7 @@ static int luaB_loadstring (lua_State *L) { | |||
248 | return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop); | 226 | return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop); |
249 | } | 227 | } |
250 | 228 | ||
229 | |||
251 | static int luaB_dofile (lua_State *L) { | 230 | static int luaB_dofile (lua_State *L) { |
252 | int oldtop = lua_gettop(L); | 231 | int oldtop = lua_gettop(L); |
253 | const char *fname = luaL_opt_string(L, 1, NULL); | 232 | const char *fname = luaL_opt_string(L, 1, NULL); |
@@ -411,6 +390,50 @@ static int luaB_tostring (lua_State *L) { | |||
411 | } | 390 | } |
412 | 391 | ||
413 | 392 | ||
393 | static const luaL_reg base_funcs[] = { | ||
394 | {LUA_ALERT, luaB__ALERT}, | ||
395 | {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE}, | ||
396 | {"error", luaB_error}, | ||
397 | {"metatable", luaB_metatable}, | ||
398 | {"globals", luaB_globals}, | ||
399 | {"next", luaB_next}, | ||
400 | {"print", luaB_print}, | ||
401 | {"tonumber", luaB_tonumber}, | ||
402 | {"tostring", luaB_tostring}, | ||
403 | {"type", luaB_type}, | ||
404 | {"assert", luaB_assert}, | ||
405 | {"unpack", luaB_unpack}, | ||
406 | {"rawget", luaB_rawget}, | ||
407 | {"rawset", luaB_rawset}, | ||
408 | {"call", luaB_call}, | ||
409 | {"collectgarbage", luaB_collectgarbage}, | ||
410 | {"gcinfo", luaB_gcinfo}, | ||
411 | {"loadfile", luaB_loadfile}, | ||
412 | {"loadstring", luaB_loadstring}, | ||
413 | {"dofile", luaB_dofile}, | ||
414 | {"dostring", luaB_dostring}, | ||
415 | {NULL, NULL} | ||
416 | }; | ||
417 | |||
418 | |||
419 | static void base_open (lua_State *L) { | ||
420 | lua_pushliteral(L, "_G"); | ||
421 | lua_pushvalue(L, LUA_GLOBALSINDEX); | ||
422 | luaL_openlib(L, base_funcs); /* open lib into global table */ | ||
423 | lua_pushliteral(L, "_VERSION"); | ||
424 | lua_pushliteral(L, LUA_VERSION); | ||
425 | lua_settable(L, -3); /* set global _VERSION */ | ||
426 | lua_settable(L, -1); /* set global _G */ | ||
427 | } | ||
428 | |||
429 | |||
430 | /* | ||
431 | ** {====================================================== | ||
432 | ** Coroutine library | ||
433 | ** ======================================================= | ||
434 | */ | ||
435 | |||
436 | |||
414 | static int luaB_resume (lua_State *L) { | 437 | static int luaB_resume (lua_State *L) { |
415 | lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1)); | 438 | lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1)); |
416 | if (lua_resume(L, co) != 0) | 439 | if (lua_resume(L, co) != 0) |
@@ -457,6 +480,26 @@ static int luaB_yield (lua_State *L) { | |||
457 | return lua_yield(L, lua_gettop(L)); | 480 | return lua_yield(L, lua_gettop(L)); |
458 | } | 481 | } |
459 | 482 | ||
483 | static const luaL_reg co_funcs[] = { | ||
484 | {"create", luaB_coroutine}, | ||
485 | {"yield", luaB_yield}, | ||
486 | {NULL, NULL} | ||
487 | }; | ||
488 | |||
489 | |||
490 | static void co_open (lua_State *L) { | ||
491 | luaL_opennamedlib(L, "co", co_funcs); | ||
492 | /* create metatable for coroutines */ | ||
493 | lua_pushliteral(L, "Coroutine"); | ||
494 | lua_newtable(L); | ||
495 | lua_pushliteral(L, "__gc"); | ||
496 | lua_pushcfunction(L, gc_coroutine); | ||
497 | lua_rawset(L, -3); | ||
498 | lua_rawset(L, LUA_REGISTRYINDEX); | ||
499 | } | ||
500 | |||
501 | /* }====================================================== */ | ||
502 | |||
460 | 503 | ||
461 | /* | 504 | /* |
462 | ** {====================================================== | 505 | ** {====================================================== |
@@ -499,6 +542,13 @@ static int luaB_foreach (lua_State *L) { | |||
499 | } | 542 | } |
500 | 543 | ||
501 | 544 | ||
545 | static void aux_setn (lua_State *L, int t, int n) { | ||
546 | lua_pushliteral(L, "n"); | ||
547 | lua_pushnumber(L, n); | ||
548 | lua_rawset(L, t); | ||
549 | } | ||
550 | |||
551 | |||
502 | static int luaB_getn (lua_State *L) { | 552 | static int luaB_getn (lua_State *L) { |
503 | luaL_check_type(L, 1, LUA_TTABLE); | 553 | luaL_check_type(L, 1, LUA_TTABLE); |
504 | lua_pushnumber(L, lua_getn(L, 1)); | 554 | lua_pushnumber(L, lua_getn(L, 1)); |
@@ -653,61 +703,29 @@ static int luaB_sort (lua_State *L) { | |||
653 | 703 | ||
654 | /* }====================================================== */ | 704 | /* }====================================================== */ |
655 | 705 | ||
656 | /* }====================================================== */ | ||
657 | |||
658 | 706 | ||
659 | 707 | static const luaL_reg array_funcs[] = { | |
660 | static const luaL_reg base_funcs[] = { | ||
661 | {LUA_ALERT, luaB__ALERT}, | ||
662 | {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE}, | ||
663 | {"call", luaB_call}, | ||
664 | {"collectgarbage", luaB_collectgarbage}, | ||
665 | {"coroutine", luaB_coroutine}, | ||
666 | {"dofile", luaB_dofile}, | ||
667 | {"dostring", luaB_dostring}, | ||
668 | {"error", luaB_error}, | ||
669 | {"metatable", luaB_metatable}, | ||
670 | {"foreach", luaB_foreach}, | 708 | {"foreach", luaB_foreach}, |
671 | {"foreachi", luaB_foreachi}, | 709 | {"foreachi", luaB_foreachi}, |
672 | {"gcinfo", luaB_gcinfo}, | ||
673 | {"getglobal", luaB_getglobal}, /* compatibility with 4.0 */ | ||
674 | {"globals", luaB_globals}, | ||
675 | {"loadfile", luaB_loadfile}, | ||
676 | {"loadstring", luaB_loadstring}, | ||
677 | {"next", luaB_next}, | ||
678 | {"print", luaB_print}, | ||
679 | {"rawget", luaB_rawget}, | ||
680 | {"rawset", luaB_rawset}, | ||
681 | {"setglobal", luaB_setglobal}, /* compatibility with 4.0 */ | ||
682 | {"tonumber", luaB_tonumber}, | ||
683 | {"tostring", luaB_tostring}, | ||
684 | {"type", luaB_type}, | ||
685 | {"assert", luaB_assert}, | ||
686 | {"getn", luaB_getn}, | 710 | {"getn", luaB_getn}, |
687 | {"sort", luaB_sort}, | 711 | {"sort", luaB_sort}, |
688 | {"tinsert", luaB_tinsert}, | 712 | {"insert", luaB_tinsert}, |
689 | {"tremove", luaB_tremove}, | 713 | {"remove", luaB_tremove}, |
690 | {"unpack", luaB_unpack}, | 714 | {NULL, NULL} |
691 | {"yield", luaB_yield} | ||
692 | }; | 715 | }; |
693 | 716 | ||
717 | /* }====================================================== */ | ||
718 | |||
694 | 719 | ||
695 | 720 | ||
696 | LUALIB_API int lua_baselibopen (lua_State *L) { | 721 | LUALIB_API int lua_baselibopen (lua_State *L) { |
697 | luaL_openl(L, base_funcs); | 722 | base_open(L); |
698 | lua_pushliteral(L, LUA_VERSION); | 723 | co_open(L); |
699 | lua_setglobal(L, "_VERSION"); | 724 | luaL_opennamedlib(L, "A", array_funcs); |
700 | /* `require' needs an empty table as upvalue */ | 725 | /* `require' needs an empty table as upvalue */ |
701 | lua_newtable(L); | 726 | lua_newtable(L); |
702 | lua_pushcclosure(L, luaB_require, 1); | 727 | lua_pushcclosure(L, luaB_require, 1); |
703 | lua_setglobal(L, "require"); | 728 | lua_setglobal(L, "require"); |
704 | /* create metatable for coroutines */ | ||
705 | lua_pushliteral(L, "Coroutine"); | ||
706 | lua_newtable(L); | ||
707 | lua_pushliteral(L, "gc"); | ||
708 | lua_pushcfunction(L, gc_coroutine); | ||
709 | lua_rawset(L, -3); | ||
710 | lua_rawset(L, LUA_REGISTRYINDEX); | ||
711 | return 0; | 729 | return 0; |
712 | } | 730 | } |
713 | 731 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldblib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ | 2 | ** $Id: ldblib.c,v 1.43 2002/02/07 17:24:32 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 | */ |
@@ -167,17 +167,105 @@ static int setlinehook (lua_State *L) { | |||
167 | } | 167 | } |
168 | 168 | ||
169 | 169 | ||
170 | static int debug (lua_State *L) { | ||
171 | for (;;) { | ||
172 | char buffer[250]; | ||
173 | fprintf(stderr, "lua_debug> "); | ||
174 | if (fgets(buffer, sizeof(buffer), stdin) == 0 || | ||
175 | strcmp(buffer, "cont\n") == 0) | ||
176 | return 0; | ||
177 | lua_dostring(L, buffer); | ||
178 | lua_settop(L, 0); /* remove eventual returns */ | ||
179 | } | ||
180 | } | ||
181 | |||
182 | |||
183 | #define LEVELS1 12 /* size of the first part of the stack */ | ||
184 | #define LEVELS2 10 /* size of the second part of the stack */ | ||
185 | |||
186 | static int errorfb (lua_State *L) { | ||
187 | int level = 1; /* skip level 0 (it's this function) */ | ||
188 | int firstpart = 1; /* still before eventual `...' */ | ||
189 | lua_Debug ar; | ||
190 | luaL_Buffer b; | ||
191 | luaL_buffinit(L, &b); | ||
192 | luaL_addstring(&b, "error: "); | ||
193 | luaL_addstring(&b, luaL_check_string(L, 1)); | ||
194 | luaL_addstring(&b, "\n"); | ||
195 | while (lua_getstack(L, level++, &ar)) { | ||
196 | char buff[120]; /* enough to fit following `sprintf's */ | ||
197 | if (level == 2) | ||
198 | luaL_addstring(&b, "stack traceback:\n"); | ||
199 | else if (level > LEVELS1 && firstpart) { | ||
200 | /* no more than `LEVELS2' more levels? */ | ||
201 | if (!lua_getstack(L, level+LEVELS2, &ar)) | ||
202 | level--; /* keep going */ | ||
203 | else { | ||
204 | luaL_addstring(&b, " ...\n"); /* too many levels */ | ||
205 | while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ | ||
206 | level++; | ||
207 | } | ||
208 | firstpart = 0; | ||
209 | continue; | ||
210 | } | ||
211 | sprintf(buff, "%4d: ", level-1); | ||
212 | luaL_addstring(&b, buff); | ||
213 | lua_getinfo(L, "Snl", &ar); | ||
214 | switch (*ar.namewhat) { | ||
215 | case 'g': case 'l': /* global, local */ | ||
216 | sprintf(buff, "function `%.50s'", ar.name); | ||
217 | break; | ||
218 | case 'f': /* field */ | ||
219 | sprintf(buff, "method `%.50s'", ar.name); | ||
220 | break; | ||
221 | case 't': /* tag method */ | ||
222 | sprintf(buff, "`%.50s' tag method", ar.name); | ||
223 | break; | ||
224 | default: { | ||
225 | if (*ar.what == 'm') /* main? */ | ||
226 | sprintf(buff, "main of %.70s", ar.short_src); | ||
227 | else if (*ar.what == 'C') /* C function? */ | ||
228 | sprintf(buff, "%.70s", ar.short_src); | ||
229 | else | ||
230 | sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src); | ||
231 | ar.source = NULL; /* do not print source again */ | ||
232 | } | ||
233 | } | ||
234 | luaL_addstring(&b, buff); | ||
235 | if (ar.currentline > 0) { | ||
236 | sprintf(buff, " at line %d", ar.currentline); | ||
237 | luaL_addstring(&b, buff); | ||
238 | } | ||
239 | if (ar.source) { | ||
240 | sprintf(buff, " [%.70s]", ar.short_src); | ||
241 | luaL_addstring(&b, buff); | ||
242 | } | ||
243 | luaL_addstring(&b, "\n"); | ||
244 | } | ||
245 | luaL_pushresult(&b); | ||
246 | lua_getglobal(L, LUA_ALERT); | ||
247 | if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */ | ||
248 | lua_pushvalue(L, -2); /* error message */ | ||
249 | lua_rawcall(L, 1, 0); | ||
250 | } | ||
251 | return 0; | ||
252 | } | ||
253 | |||
254 | |||
170 | static const luaL_reg dblib[] = { | 255 | static const luaL_reg dblib[] = { |
171 | {"getlocal", getlocal}, | 256 | {"getlocal", getlocal}, |
172 | {"getinfo", getinfo}, | 257 | {"getinfo", getinfo}, |
173 | {"setcallhook", setcallhook}, | 258 | {"setcallhook", setcallhook}, |
174 | {"setlinehook", setlinehook}, | 259 | {"setlinehook", setlinehook}, |
175 | {"setlocal", setlocal} | 260 | {"setlocal", setlocal}, |
261 | {"debug", debug}, | ||
262 | {NULL, NULL} | ||
176 | }; | 263 | }; |
177 | 264 | ||
178 | 265 | ||
179 | LUALIB_API int lua_dblibopen (lua_State *L) { | 266 | LUALIB_API int lua_dblibopen (lua_State *L) { |
180 | luaL_openl(L, dblib); | 267 | luaL_opennamedlib(L, "dbg", dblib); |
268 | lua_register(L, LUA_ERRORMESSAGE, errorfb); | ||
181 | return 0; | 269 | return 0; |
182 | } | 270 | } |
183 | 271 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ | 2 | ** $Id: liolib.c,v 1.131 2002/02/08 22:39:56 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 | */ |
@@ -20,6 +20,12 @@ | |||
20 | 20 | ||
21 | 21 | ||
22 | 22 | ||
23 | /* | ||
24 | ** {====================================================== | ||
25 | ** FILE Operations | ||
26 | ** ======================================================= | ||
27 | */ | ||
28 | |||
23 | 29 | ||
24 | #ifdef POPEN | 30 | #ifdef POPEN |
25 | /* FILE *popen(); | 31 | /* FILE *popen(); |
@@ -41,7 +47,7 @@ int pclose(); */ | |||
41 | 47 | ||
42 | 48 | ||
43 | static const char *const filenames[] = {"_INPUT", "_OUTPUT"}; | 49 | static const char *const filenames[] = {"_INPUT", "_OUTPUT"}; |
44 | static const char *const basicfiles[] = {"_STDIN", "_STDOUT"}; | 50 | static const char *const basicfiles[] = {"stdin", "stdout"}; |
45 | 51 | ||
46 | 52 | ||
47 | static int pushresult (lua_State *L, int i) { | 53 | static int pushresult (lua_State *L, int i) { |
@@ -58,15 +64,6 @@ static int pushresult (lua_State *L, int i) { | |||
58 | } | 64 | } |
59 | 65 | ||
60 | 66 | ||
61 | |||
62 | /* | ||
63 | ** {====================================================== | ||
64 | ** FILE Operations | ||
65 | ** ======================================================= | ||
66 | */ | ||
67 | |||
68 | |||
69 | |||
70 | static int checkfile (lua_State *L, int findex, const char *tname) { | 67 | static int checkfile (lua_State *L, int findex, const char *tname) { |
71 | int res; | 68 | int res; |
72 | lua_getmetatable(L, findex); | 69 | lua_getmetatable(L, findex); |
@@ -116,8 +113,9 @@ static void newfile (lua_State *L, FILE *f) { | |||
116 | 113 | ||
117 | 114 | ||
118 | static void newfilewithname (lua_State *L, FILE *f, const char *name) { | 115 | static void newfilewithname (lua_State *L, FILE *f, const char *name) { |
116 | lua_pushstring(L, name); | ||
119 | newfile(L, f); | 117 | newfile(L, f); |
120 | lua_setglobal(L, name); | 118 | lua_settable(L, -3); |
121 | } | 119 | } |
122 | 120 | ||
123 | 121 | ||
@@ -136,8 +134,11 @@ static int setnewfile (lua_State *L, FILE *f, int inout) { | |||
136 | 134 | ||
137 | 135 | ||
138 | static void resetfile (lua_State *L, int inout) { | 136 | static void resetfile (lua_State *L, int inout) { |
139 | lua_getglobal(L, basicfiles[inout]); | 137 | lua_getglobal(L, "io"); |
138 | lua_pushstring(L, basicfiles[inout]); | ||
139 | lua_gettable(L, -2); | ||
140 | lua_setglobal(L, filenames[inout]); | 140 | lua_setglobal(L, filenames[inout]); |
141 | lua_pop(L, 1); | ||
141 | } | 142 | } |
142 | 143 | ||
143 | 144 | ||
@@ -410,6 +411,20 @@ static int io_flush (lua_State *L) { | |||
410 | return pushresult(L, fflush(f) == 0); | 411 | return pushresult(L, fflush(f) == 0); |
411 | } | 412 | } |
412 | 413 | ||
414 | static const luaL_reg iolib[] = { | ||
415 | {"appendto", io_appendto}, | ||
416 | {"close", io_close}, | ||
417 | {"flush", io_flush}, | ||
418 | {"open", io_open}, | ||
419 | {"read", io_read}, | ||
420 | {"readfrom", io_readfrom}, | ||
421 | {"seek", io_seek}, | ||
422 | {"tmpfile", io_tmpfile}, | ||
423 | {"write", io_write}, | ||
424 | {"writeto", io_writeto}, | ||
425 | {NULL, NULL} | ||
426 | }; | ||
427 | |||
413 | /* }====================================================== */ | 428 | /* }====================================================== */ |
414 | 429 | ||
415 | 430 | ||
@@ -445,7 +460,6 @@ static int io_tmpname (lua_State *L) { | |||
445 | } | 460 | } |
446 | 461 | ||
447 | 462 | ||
448 | |||
449 | static int io_getenv (lua_State *L) { | 463 | static int io_getenv (lua_State *L) { |
450 | lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */ | 464 | lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */ |
451 | return 1; | 465 | return 1; |
@@ -580,128 +594,30 @@ static int io_exit (lua_State *L) { | |||
580 | return 0; /* to avoid warnings */ | 594 | return 0; /* to avoid warnings */ |
581 | } | 595 | } |
582 | 596 | ||
583 | /* }====================================================== */ | 597 | static const luaL_reg syslib[] = { |
584 | |||
585 | |||
586 | |||
587 | static int io_debug (lua_State *L) { | ||
588 | for (;;) { | ||
589 | char buffer[250]; | ||
590 | fprintf(stderr, "lua_debug> "); | ||
591 | if (fgets(buffer, sizeof(buffer), stdin) == 0 || | ||
592 | strcmp(buffer, "cont\n") == 0) | ||
593 | return 0; | ||
594 | lua_dostring(L, buffer); | ||
595 | lua_settop(L, 0); /* remove eventual returns */ | ||
596 | } | ||
597 | } | ||
598 | |||
599 | |||
600 | #define LEVELS1 12 /* size of the first part of the stack */ | ||
601 | #define LEVELS2 10 /* size of the second part of the stack */ | ||
602 | |||
603 | static int errorfb (lua_State *L) { | ||
604 | int level = 1; /* skip level 0 (it's this function) */ | ||
605 | int firstpart = 1; /* still before eventual `...' */ | ||
606 | lua_Debug ar; | ||
607 | luaL_Buffer b; | ||
608 | luaL_buffinit(L, &b); | ||
609 | luaL_addstring(&b, "error: "); | ||
610 | luaL_addstring(&b, luaL_check_string(L, 1)); | ||
611 | luaL_addstring(&b, "\n"); | ||
612 | while (lua_getstack(L, level++, &ar)) { | ||
613 | char buff[120]; /* enough to fit following `sprintf's */ | ||
614 | if (level == 2) | ||
615 | luaL_addstring(&b, "stack traceback:\n"); | ||
616 | else if (level > LEVELS1 && firstpart) { | ||
617 | /* no more than `LEVELS2' more levels? */ | ||
618 | if (!lua_getstack(L, level+LEVELS2, &ar)) | ||
619 | level--; /* keep going */ | ||
620 | else { | ||
621 | luaL_addstring(&b, " ...\n"); /* too many levels */ | ||
622 | while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ | ||
623 | level++; | ||
624 | } | ||
625 | firstpart = 0; | ||
626 | continue; | ||
627 | } | ||
628 | sprintf(buff, "%4d: ", level-1); | ||
629 | luaL_addstring(&b, buff); | ||
630 | lua_getinfo(L, "Snl", &ar); | ||
631 | switch (*ar.namewhat) { | ||
632 | case 'g': case 'l': /* global, local */ | ||
633 | sprintf(buff, "function `%.50s'", ar.name); | ||
634 | break; | ||
635 | case 'f': /* field */ | ||
636 | sprintf(buff, "method `%.50s'", ar.name); | ||
637 | break; | ||
638 | case 't': /* tag method */ | ||
639 | sprintf(buff, "`%.50s' tag method", ar.name); | ||
640 | break; | ||
641 | default: { | ||
642 | if (*ar.what == 'm') /* main? */ | ||
643 | sprintf(buff, "main of %.70s", ar.short_src); | ||
644 | else if (*ar.what == 'C') /* C function? */ | ||
645 | sprintf(buff, "%.70s", ar.short_src); | ||
646 | else | ||
647 | sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src); | ||
648 | ar.source = NULL; /* do not print source again */ | ||
649 | } | ||
650 | } | ||
651 | luaL_addstring(&b, buff); | ||
652 | if (ar.currentline > 0) { | ||
653 | sprintf(buff, " at line %d", ar.currentline); | ||
654 | luaL_addstring(&b, buff); | ||
655 | } | ||
656 | if (ar.source) { | ||
657 | sprintf(buff, " [%.70s]", ar.short_src); | ||
658 | luaL_addstring(&b, buff); | ||
659 | } | ||
660 | luaL_addstring(&b, "\n"); | ||
661 | } | ||
662 | luaL_pushresult(&b); | ||
663 | lua_getglobal(L, LUA_ALERT); | ||
664 | if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */ | ||
665 | lua_pushvalue(L, -2); /* error message */ | ||
666 | lua_rawcall(L, 1, 0); | ||
667 | } | ||
668 | return 0; | ||
669 | } | ||
670 | |||
671 | |||
672 | |||
673 | static const luaL_reg iolib[] = { | ||
674 | {"appendto", io_appendto}, | ||
675 | {"clock", io_clock}, | 598 | {"clock", io_clock}, |
676 | {"closefile", io_close}, | ||
677 | {"date", io_date}, | 599 | {"date", io_date}, |
678 | {"debug", io_debug}, | ||
679 | {"difftime", io_difftime}, | 600 | {"difftime", io_difftime}, |
680 | {"execute", io_execute}, | 601 | {"execute", io_execute}, |
681 | {"exit", io_exit}, | 602 | {"exit", io_exit}, |
682 | {"flush", io_flush}, | ||
683 | {"getenv", io_getenv}, | 603 | {"getenv", io_getenv}, |
684 | {"openfile", io_open}, | ||
685 | {"read", io_read}, | ||
686 | {"readfrom", io_readfrom}, | ||
687 | {"remove", io_remove}, | 604 | {"remove", io_remove}, |
688 | {"rename", io_rename}, | 605 | {"rename", io_rename}, |
689 | {"seek", io_seek}, | ||
690 | {"setlocale", io_setloc}, | 606 | {"setlocale", io_setloc}, |
691 | {"time", io_time}, | 607 | {"time", io_time}, |
692 | {"tmpfile", io_tmpfile}, | ||
693 | {"tmpname", io_tmpname}, | 608 | {"tmpname", io_tmpname}, |
694 | {"write", io_write}, | 609 | {NULL, NULL} |
695 | {"writeto", io_writeto}, | ||
696 | {LUA_ERRORMESSAGE, errorfb} | ||
697 | }; | 610 | }; |
698 | 611 | ||
612 | /* }====================================================== */ | ||
613 | |||
614 | |||
699 | 615 | ||
700 | LUALIB_API int lua_iolibopen (lua_State *L) { | 616 | LUALIB_API int lua_iolibopen (lua_State *L) { |
701 | lua_pushliteral(L, FILEHANDLE); | 617 | lua_pushliteral(L, FILEHANDLE); |
702 | lua_newtable(L); /* meta table for FILEHANDLE */ | 618 | lua_newtable(L); /* meta table for FILEHANDLE */ |
703 | /* close files when collected */ | 619 | /* close files when collected */ |
704 | lua_pushliteral(L, "gc"); | 620 | lua_pushliteral(L, "__gc"); |
705 | lua_pushcfunction(L, file_collect); | 621 | lua_pushcfunction(L, file_collect); |
706 | lua_rawset(L, -3); | 622 | lua_rawset(L, -3); |
707 | /* put new metatable into registry */ | 623 | /* put new metatable into registry */ |
@@ -710,11 +626,15 @@ LUALIB_API int lua_iolibopen (lua_State *L) { | |||
710 | lua_pushliteral(L, CLOSEDFILEHANDLE); | 626 | lua_pushliteral(L, CLOSEDFILEHANDLE); |
711 | lua_newtable(L); | 627 | lua_newtable(L); |
712 | lua_rawset(L, LUA_REGISTRYINDEX); | 628 | lua_rawset(L, LUA_REGISTRYINDEX); |
713 | luaL_openl(L, iolib); | 629 | luaL_opennamedlib(L, "os", syslib); |
630 | lua_pushliteral(L, "io"); | ||
631 | lua_newtable(L); | ||
632 | luaL_openlib(L, iolib); | ||
714 | /* predefined file handles */ | 633 | /* predefined file handles */ |
715 | newfilewithname(L, stdin, basicfiles[INFILE]); | 634 | newfilewithname(L, stdin, basicfiles[INFILE]); |
716 | newfilewithname(L, stdout, basicfiles[OUTFILE]); | 635 | newfilewithname(L, stdout, basicfiles[OUTFILE]); |
717 | newfilewithname(L, stderr, "_STDERR"); | 636 | newfilewithname(L, stderr, "stderr"); |
637 | lua_settable(L, LUA_GLOBALSINDEX); | ||
718 | resetfile(L, INFILE); | 638 | resetfile(L, INFILE); |
719 | resetfile(L, OUTFILE); | 639 | resetfile(L, OUTFILE); |
720 | return 0; | 640 | return 0; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmathlib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ | 2 | ** $Id: lmathlib.c,v 1.40 2001/12/05 20:15:18 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 | */ |
@@ -200,39 +200,45 @@ static int math_randomseed (lua_State *L) { | |||
200 | 200 | ||
201 | 201 | ||
202 | static const luaL_reg mathlib[] = { | 202 | static const luaL_reg mathlib[] = { |
203 | {"abs", math_abs}, | 203 | {"abs", math_abs}, |
204 | {"sin", math_sin}, | 204 | {"sin", math_sin}, |
205 | {"cos", math_cos}, | 205 | {"cos", math_cos}, |
206 | {"tan", math_tan}, | 206 | {"tan", math_tan}, |
207 | {"asin", math_asin}, | 207 | {"asin", math_asin}, |
208 | {"acos", math_acos}, | 208 | {"acos", math_acos}, |
209 | {"atan", math_atan}, | 209 | {"atan", math_atan}, |
210 | {"atan2", math_atan2}, | 210 | {"atan2", math_atan2}, |
211 | {"ceil", math_ceil}, | 211 | {"ceil", math_ceil}, |
212 | {"floor", math_floor}, | 212 | {"floor", math_floor}, |
213 | {"mod", math_mod}, | 213 | {"mod", math_mod}, |
214 | {"frexp", math_frexp}, | 214 | {"frexp", math_frexp}, |
215 | {"ldexp", math_ldexp}, | 215 | {"ldexp", math_ldexp}, |
216 | {"sqrt", math_sqrt}, | 216 | {"sqrt", math_sqrt}, |
217 | {"min", math_min}, | 217 | {"min", math_min}, |
218 | {"max", math_max}, | 218 | {"max", math_max}, |
219 | {"log", math_log}, | 219 | {"log", math_log}, |
220 | {"log10", math_log10}, | 220 | {"log10", math_log10}, |
221 | {"exp", math_exp}, | 221 | {"exp", math_exp}, |
222 | {"deg", math_deg}, | 222 | {"deg", math_deg}, |
223 | {"pow", math_pow}, | 223 | {"pow", math_pow}, |
224 | {"rad", math_rad}, | 224 | {"rad", math_rad}, |
225 | {"random", math_random}, | 225 | {"random", math_random}, |
226 | {"randomseed", math_randomseed} | 226 | {"randomseed", math_randomseed}, |
227 | {NULL, NULL} | ||
227 | }; | 228 | }; |
228 | 229 | ||
230 | |||
229 | /* | 231 | /* |
230 | ** Open math library | 232 | ** Open math library |
231 | */ | 233 | */ |
232 | LUALIB_API int lua_mathlibopen (lua_State *L) { | 234 | LUALIB_API int lua_mathlibopen (lua_State *L) { |
233 | luaL_openl(L, mathlib); | 235 | lua_pushliteral(L, "math"); |
236 | lua_newtable(L); | ||
237 | luaL_openlib(L, mathlib); | ||
238 | lua_pushliteral(L, "pi"); | ||
234 | lua_pushnumber(L, PI); | 239 | lua_pushnumber(L, PI); |
235 | lua_setglobal(L, "PI"); | 240 | lua_settable(L, -3); |
241 | lua_settable(L, LUA_GLOBALSINDEX); | ||
236 | return 0; | 242 | return 0; |
237 | } | 243 | } |
238 | 244 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.77 2002/02/08 22:39:36 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.78 2002/03/11 13:29:40 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 | */ |
@@ -707,18 +707,19 @@ static int str_format (lua_State *L) { | |||
707 | 707 | ||
708 | 708 | ||
709 | static const luaL_reg strlib[] = { | 709 | static const luaL_reg strlib[] = { |
710 | {"strlen", str_len}, | 710 | {"len", str_len}, |
711 | {"strsub", str_sub}, | 711 | {"sub", str_sub}, |
712 | {"strlower", str_lower}, | 712 | {"lower", str_lower}, |
713 | {"strupper", str_upper}, | 713 | {"upper", str_upper}, |
714 | {"strchar", str_char}, | 714 | {"char", str_char}, |
715 | {"strrep", str_rep}, | 715 | {"rep", str_rep}, |
716 | {"strbyte", str_byte}, | 716 | {"byte", str_byte}, |
717 | {"concat", str_concat}, | 717 | {"concat", str_concat}, |
718 | {"format", str_format}, | 718 | {"format", str_format}, |
719 | {"strfind", str_find}, | 719 | {"find", str_find}, |
720 | {"gfind", gfind}, | 720 | {"gfind", gfind}, |
721 | {"gsub", str_gsub} | 721 | {"gsub", str_gsub}, |
722 | {NULL, NULL} | ||
722 | }; | 723 | }; |
723 | 724 | ||
724 | 725 | ||
@@ -726,6 +727,7 @@ static const luaL_reg strlib[] = { | |||
726 | ** Open string library | 727 | ** Open string library |
727 | */ | 728 | */ |
728 | LUALIB_API int lua_strlibopen (lua_State *L) { | 729 | LUALIB_API int lua_strlibopen (lua_State *L) { |
729 | luaL_openl(L, strlib); | 730 | luaL_opennamedlib(L, "str", strlib); |
730 | return 0; | 731 | return 0; |
731 | } | 732 | } |
733 | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltests.c,v 1.111 2002/03/04 21:29:41 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 1.112 2002/03/14 18:01:52 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 | */ |
@@ -666,7 +666,8 @@ static const struct luaL_reg tests_funcs[] = { | |||
666 | {"closestate", closestate}, | 666 | {"closestate", closestate}, |
667 | {"doremote", doremote}, | 667 | {"doremote", doremote}, |
668 | {"log2", log2_aux}, | 668 | {"log2", log2_aux}, |
669 | {"totalmem", mem_query} | 669 | {"totalmem", mem_query}, |
670 | {NULL, NULL} | ||
670 | }; | 671 | }; |
671 | 672 | ||
672 | 673 | ||
@@ -681,14 +682,7 @@ static void fim (void) { | |||
681 | void luaB_opentests (lua_State *L) { | 682 | void luaB_opentests (lua_State *L) { |
682 | *cast(int **, L) = &islocked; /* init lock */ | 683 | *cast(int **, L) = &islocked; /* init lock */ |
683 | lua_state = L; /* keep first state to be opened */ | 684 | lua_state = L; /* keep first state to be opened */ |
684 | /* open lib in a new table */ | 685 | luaL_opennamedlib(L, "T", tests_funcs); |
685 | lua_newtable(L); | ||
686 | lua_getglobals(L); | ||
687 | lua_pushvalue(L, -2); | ||
688 | lua_setglobals(L); | ||
689 | luaL_openl(L, tests_funcs); /* open functions inside new table */ | ||
690 | lua_setglobals(L); /* restore old table of globals */ | ||
691 | lua_setglobal(L, "T"); /* set new table as global T */ | ||
692 | atexit(fim); | 686 | atexit(fim); |
693 | } | 687 | } |
694 | 688 | ||