aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lbaselib.c122
-rw-r--r--loadlib.c187
-rw-r--r--lua.c8
-rw-r--r--luaconf.h18
4 files changed, 206 insertions, 129 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 771d8525..58d43ece 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.158 2004/09/15 20:39:42 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.159 2004/09/29 21:03:14 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*/
@@ -440,105 +440,6 @@ static int luaB_newproxy (lua_State *L) {
440} 440}
441 441
442 442
443/*
444** {======================================================
445** `require' function
446** =======================================================
447*/
448
449
450static const char *getpath (lua_State *L) {
451 /* try first `LUA_PATH' for compatibility */
452 lua_getfield(L, LUA_GLOBALSINDEX, "LUA_PATH");
453 if (!lua_isstring(L, -1)) {
454 lua_pop(L, 1);
455 lua_getfield(L, LUA_GLOBALSINDEX, "_PATH");
456 }
457 if (!lua_isstring(L, -1))
458 luaL_error(L, "global _PATH must be a string");
459 return lua_tostring(L, -1);
460}
461
462
463static int luaB_require (lua_State *L) {
464 const char *name = luaL_checkstring(L, 1);
465 const char *fname;
466 lua_settop(L, 1);
467 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
468 lua_getfield(L, 2, name);
469 if (lua_toboolean(L, -1)) /* is it there? */
470 return 1; /* package is already loaded; return its result */
471 /* else must load it; first mark it as loaded */
472 lua_pushboolean(L, 1);
473 lua_setfield(L, 2, name); /* _LOADED[name] = true */
474 fname = luaL_gsub(L, name, ".", LUA_DIRSEP);
475 fname = luaL_searchpath(L, fname, getpath(L));
476 if (fname == NULL || luaL_loadfile(L, fname) != 0)
477 return luaL_error(L, "error loading package `%s' (%s)", name,
478 lua_tostring(L, -1));
479 lua_pushvalue(L, 1); /* pass name as argument to module */
480 lua_call(L, 1, 1); /* run loaded module */
481 if (!lua_isnil(L, -1)) /* nil return? */
482 lua_setfield(L, 2, name);
483 lua_getfield(L, 2, name); /* return _LOADED[name] */
484 return 1;
485}
486
487
488static void setfenv (lua_State *L) {
489 lua_Debug ar;
490 lua_getstack(L, 1, &ar);
491 lua_getinfo(L, "f", &ar);
492 lua_pushvalue(L, -2);
493 lua_setfenv(L, -2);
494}
495
496
497static int luaB_module (lua_State *L) {
498 const char *modname = luaL_checkstring(L, 1);
499 const char *dot;
500 lua_settop(L, 1);
501 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
502 /* try to find given table */
503 luaL_getfield(L, LUA_GLOBALSINDEX, modname);
504 if (lua_isnil(L, -1)) { /* not found? */
505 lua_pop(L, 1); /* remove previous result */
506 lua_newtable(L); /* create it */
507 /* register it with given name */
508 lua_pushvalue(L, -1);
509 luaL_setfield(L, LUA_GLOBALSINDEX, modname);
510 }
511 else if (!lua_istable(L, -1))
512 return luaL_error(L, "name conflict for module `%s'", modname);
513 /* check whether table already has a _NAME field */
514 lua_getfield(L, -1, "_NAME");
515 if (!lua_isnil(L, -1)) /* is table an initialized module? */
516 lua_pop(L, 1);
517 else { /* no; initialize it */
518 lua_pop(L, 1);
519 lua_newtable(L); /* create new metatable */
520 lua_pushvalue(L, LUA_GLOBALSINDEX);
521 lua_setfield(L, -2, "__index"); /* mt.__index = _G */
522 lua_setmetatable(L, -2);
523 lua_pushstring(L, modname);
524 lua_setfield(L, -2, "_NAME");
525 dot = strrchr(modname, '.'); /* look for last dot in module name */
526 if (dot == NULL) dot = modname;
527 else dot++;
528 /* set _PACK as package name (full module name minus last part) */
529 lua_pushlstring(L, modname, dot - modname);
530 lua_setfield(L, -2, "_PACK");
531 }
532 lua_pushvalue(L, -1);
533 lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */
534 setfenv(L);
535 return 0;
536}
537
538
539/* }====================================================== */
540
541
542static const luaL_reg base_funcs[] = { 443static const luaL_reg base_funcs[] = {
543 {"error", luaB_error}, 444 {"error", luaB_error},
544 {"getmetatable", luaB_getmetatable}, 445 {"getmetatable", luaB_getmetatable},
@@ -564,8 +465,6 @@ static const luaL_reg base_funcs[] = {
564 {"dofile", luaB_dofile}, 465 {"dofile", luaB_dofile},
565 {"loadstring", luaB_loadstring}, 466 {"loadstring", luaB_loadstring},
566 {"load", luaB_load}, 467 {"load", luaB_load},
567 {"require", luaB_require},
568 {"module", luaB_module},
569 {NULL, NULL} 468 {NULL, NULL}
570}; 469};
571 470
@@ -713,11 +612,10 @@ static void auxopen (lua_State *L, const char *name,
713 612
714 613
715static void base_open (lua_State *L) { 614static void base_open (lua_State *L) {
716 const char *path;
717 lua_pushvalue(L, LUA_GLOBALSINDEX); 615 lua_pushvalue(L, LUA_GLOBALSINDEX);
718 luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */ 616 luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
719 lua_pushliteral(L, LUA_VERSION); 617 lua_pushliteral(L, LUA_VERSION);
720 lua_setfield(L, LUA_GLOBALSINDEX, "_VERSION"); /* set global _VERSION */ 618 lua_setglobal(L, "_VERSION"); /* set global _VERSION */
721 /* `ipairs' and `pairs' need auxiliary functions as upvalues */ 619 /* `ipairs' and `pairs' need auxiliary functions as upvalues */
722 auxopen(L, "ipairs", luaB_ipairs, ipairsaux); 620 auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
723 auxopen(L, "pairs", luaB_pairs, luaB_next); 621 auxopen(L, "pairs", luaB_pairs, luaB_next);
@@ -728,20 +626,16 @@ static void base_open (lua_State *L) {
728 lua_pushliteral(L, "kv"); 626 lua_pushliteral(L, "kv");
729 lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ 627 lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
730 lua_pushcclosure(L, luaB_newproxy, 1); 628 lua_pushcclosure(L, luaB_newproxy, 1);
731 lua_setfield(L, LUA_GLOBALSINDEX, "newproxy"); /* set global `newproxy' */ 629 lua_setglobal(L, "newproxy"); /* set global `newproxy' */
732 /* `require' needs a table to keep loaded chunks */ 630 /* create register._LOADED to track loaded modules */
733 lua_newtable(L); 631 lua_newtable(L);
734 lua_pushvalue(L, -1);
735 lua_setglobal(L, "_LOADED");
736 lua_setfield(L, LUA_REGISTRYINDEX, "_LOADED"); 632 lua_setfield(L, LUA_REGISTRYINDEX, "_LOADED");
633 /* create register._PRELOAD to allow pre-loaded modules */
634 lua_newtable(L);
635 lua_setfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
737 /* set global _G */ 636 /* set global _G */
738 lua_pushvalue(L, LUA_GLOBALSINDEX); 637 lua_pushvalue(L, LUA_GLOBALSINDEX);
739 lua_setfield(L, LUA_GLOBALSINDEX, "_G"); 638 lua_setglobal(L, "_G");
740 /* set global _PATH */
741 path = getenv(LUA_PATH);
742 if (path == NULL) path = LUA_PATH_DEFAULT;
743 lua_pushstring(L, path);
744 lua_setfield(L, LUA_GLOBALSINDEX, "_PATH");
745} 639}
746 640
747 641
diff --git a/loadlib.c b/loadlib.c
index ae5b8ca7..dabeaeba 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.8 2004/10/18 18:07:31 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.9 2004/11/11 15:42:57 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*
@@ -25,6 +25,10 @@
25* loaders on top of which loadlib could be implemented. 25* loaders on top of which loadlib could be implemented.
26*/ 26*/
27 27
28
29#include <string.h>
30
31
28#define loadlib_c 32#define loadlib_c
29#define LUA_LIB 33#define LUA_LIB
30 34
@@ -224,13 +228,180 @@ static int loadlib1 (lua_State *L) {
224 } 228 }
225} 229}
226 230
227LUALIB_API int luaopen_loadlib (lua_State *L) 231
228{ 232
229 luaL_newmetatable(L, "_LOADLIB"); 233/*
230 lua_pushcfunction(L, gctm); 234** {======================================================
231 lua_setfield(L, -2, "__gc"); 235** `require' and `module' functions
232 lua_register(L,"loadlib",loadlib1); 236** =======================================================
233 return 0; 237*/
238
239
240static const char *loadLua (lua_State *L, const char *fname, const char *name) {
241 const char *path;
242 /* try first `LUA_PATH' for compatibility */
243 lua_getglobal(L, "LUA_PATH");
244 path = lua_tostring(L, -1);
245 if (!path) {
246 lua_pop(L, 1);
247 luaL_getfield(L, LUA_GLOBALSINDEX, "package.path");
248 path = lua_tostring(L, -1);
249 }
250 if (path == NULL)
251 luaL_error(L, "`package.path' must be a string");
252 fname = luaL_searchpath(L, fname, path);
253 if (fname == NULL) return path; /* library not found in this path */
254 if (luaL_loadfile(L, fname) != 0)
255 luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1));
256 return NULL; /* library loaded successfully */
257}
258
259
260static const char *loadC (lua_State *L, const char *fname, const char *name) {
261 const char *path;
262 const char *funcname;
263 luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath");
264 path = lua_tostring(L, -1);
265 if (path == NULL)
266 luaL_error(L, "global _CPATH must be a string");
267 fname = luaL_searchpath(L, fname, path);
268 if (fname == NULL) return path; /* library not found in this path */
269 funcname = luaL_gsub(L, name, ".", "");
270 funcname = lua_pushfstring(L, "%s%s", LUA_POF, funcname);
271 if (loadlib(L, fname, funcname) != 0)
272 luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1));
273 return NULL; /* library loaded successfully */
274}
275
276
277static int ll_require (lua_State *L) {
278 const char *name = luaL_checkstring(L, 1);
279 lua_settop(L, 1);
280 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
281 lua_getfield(L, 2, name);
282 if (lua_toboolean(L, -1)) /* is it there? */
283 return 1; /* package is already loaded; return its result */
284 /* else must load it; first mark it as loaded */
285 lua_pushboolean(L, 1);
286 lua_setfield(L, 2, name); /* _LOADED[name] = true */
287 /* check whether it is preloaded */
288 lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
289 lua_getfield(L, -1, name);
290 if (lua_isnil(L, -1)) { /* no preload function for that module? */
291 const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP);
292 const char *cpath = loadC(L, fname, name); /* try a C module */
293 if (cpath) { /* not found? */
294 const char *path = loadLua(L, fname, name); /* try a Lua module */
295 if (path) { /* yet not found? */
296 lua_pushnil(L);
297 lua_setfield(L, 2, name); /* unmark _LOADED[name] */
298 return luaL_error(L, "package `%s' not found\n"
299 " cpath: %s\n path: %s",
300 name, cpath, path);
301 }
302 }
303 }
304 lua_pushvalue(L, 1); /* pass name as argument to module */
305 lua_call(L, 1, 1); /* run loaded module */
306 if (!lua_isnil(L, -1)) /* non-nil return? */
307 lua_setfield(L, 2, name); /* update _LOADED[name] with returned value */
308 lua_getfield(L, 2, name); /* return _LOADED[name] */
309 return 1;
310}
311
312
313static void setfenv (lua_State *L) {
314 lua_Debug ar;
315 lua_getstack(L, 1, &ar);
316 lua_getinfo(L, "f", &ar);
317 lua_pushvalue(L, -2);
318 lua_setfenv(L, -2);
319}
320
321
322static int ll_module (lua_State *L) {
323 const char *modname = luaL_checkstring(L, 1);
324 const char *dot;
325 lua_settop(L, 1);
326 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
327 /* try to find given table */
328 luaL_getfield(L, LUA_GLOBALSINDEX, modname);
329 if (lua_isnil(L, -1)) { /* not found? */
330 lua_pop(L, 1); /* remove previous result */
331 lua_newtable(L); /* create it */
332 /* register it with given name */
333 lua_pushvalue(L, -1);
334 luaL_setfield(L, LUA_GLOBALSINDEX, modname);
335 }
336 else if (!lua_istable(L, -1))
337 return luaL_error(L, "name conflict for module `%s'", modname);
338 /* check whether table already has a _NAME field */
339 lua_getfield(L, -1, "_NAME");
340 if (!lua_isnil(L, -1)) /* is table an initialized module? */
341 lua_pop(L, 1);
342 else { /* no; initialize it */
343 lua_pop(L, 1);
344 lua_newtable(L); /* create new metatable */
345 lua_pushvalue(L, LUA_GLOBALSINDEX);
346 lua_setfield(L, -2, "__index"); /* mt.__index = _G */
347 lua_setmetatable(L, -2);
348 lua_pushstring(L, modname);
349 lua_setfield(L, -2, "_NAME");
350 dot = strrchr(modname, '.'); /* look for last dot in module name */
351 if (dot == NULL) dot = modname;
352 else dot++;
353 /* set _PACKAGE as package name (full module name minus last part) */
354 lua_pushlstring(L, modname, dot - modname);
355 lua_setfield(L, -2, "_PACKAGE");
356 }
357 lua_pushvalue(L, -1);
358 lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */
359 setfenv(L);
360 return 0;
361}
362
363
364/* }====================================================== */
365
366
367static const luaL_reg ll_funcs[] = {
368 {"loadlib", loadlib1},
369 {"require", ll_require},
370 {"module", ll_module},
371 {NULL, NULL}
372};
373
374
375
376LUALIB_API int luaopen_loadlib (lua_State *L) {
377 const char *path;
378 /* create new type _LOADLIB */
379 luaL_newmetatable(L, "_LOADLIB");
380 lua_pushcfunction(L, gctm);
381 lua_setfield(L, -2, "__gc");
382 /* create `package' table */
383 lua_newtable(L);
384 lua_pushvalue(L, -1);
385 lua_setglobal(L, "package");
386 /* set field `path' */
387 path = getenv(LUA_PATH);
388 if (path == NULL) path = LUA_PATH_DEFAULT;
389 lua_pushstring(L, path);
390 lua_setfield(L, -2, "path");
391 /* set field `cpath' */
392 path = getenv(LUA_CPATH);
393 if (path == NULL) path = LUA_CPATH_DEFAULT;
394 lua_pushstring(L, path);
395 lua_setfield(L, -2, "cpath");
396 /* set field `loaded' */
397 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
398 lua_setfield(L, -2, "loaded");
399 /* set field `preload' */
400 lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
401 lua_setfield(L, -2, "preload");
402 lua_pushvalue(L, LUA_GLOBALSINDEX);
403 luaL_openlib(L, NULL, ll_funcs, 0); /* open lib into global table */
404 return 1;
234} 405}
235 406
236/* 407/*
diff --git a/lua.c b/lua.c
index 88ad392e..5e4ca9fb 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.131 2004/08/26 14:19:55 roberto Exp roberto $ 2** $Id: lua.c,v 1.132 2004/08/30 18:35:14 roberto Exp roberto $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -127,9 +127,9 @@ static int dostring (lua_State *L, const char *s, const char *name) {
127 127
128 128
129static int dolibrary (lua_State *L, const char *name) { 129static int dolibrary (lua_State *L, const char *name) {
130 lua_getfield(L, LUA_GLOBALSINDEX, "_PATH"); 130 luaL_getfield(L, LUA_GLOBALSINDEX, "package.path");
131 if (!lua_isstring(L, -1)) { 131 if (!lua_isstring(L, -1)) {
132 l_message(progname, "global _PATH must be a string"); 132 l_message(progname, "`package.path' must be a string");
133 return 1; 133 return 1;
134 } 134 }
135 name = luaL_searchpath(L, name, lua_tostring(L, -1)); 135 name = luaL_searchpath(L, name, lua_tostring(L, -1));
@@ -295,7 +295,7 @@ static int handle_argv (lua_State *L, char *argv[], int *interactive) {
295 print_usage(); 295 print_usage();
296 return 1; 296 return 1;
297 } 297 }
298 if (dostring(L, chunk, "=<command line>") != 0) 298 if (dostring(L, chunk, "=(command line)") != 0)
299 return 1; 299 return 1;
300 break; 300 break;
301 } 301 }
diff --git a/luaconf.h b/luaconf.h
index 2b3fc736..46c4998c 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: luaconf.h,v 1.14 2004/10/06 18:34:47 roberto Exp roberto $ 2** $Id: luaconf.h,v 1.15 2004/10/18 18:07:31 roberto Exp roberto $
3** Configuration file for Lua 3** Configuration file for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -31,7 +31,11 @@
31*/ 31*/
32 32
33/* default path */ 33/* default path */
34#define LUA_PATH_DEFAULT "?;?.lua" 34#define LUA_PATH_DEFAULT \
35 "./?.lua;/usr/local/share/lua/5.0/?.lua;/usr/local/share/lua/5.0/?/init.lua"
36#define LUA_CPATH_DEFAULT \
37 "./?.so;/usr/local/lib/lua/5.0/?.so;/usr/local/lib/lua/5.0/lib?.so"
38
35 39
36 40
37/* type of numbers in Lua */ 41/* type of numbers in Lua */
@@ -272,8 +276,16 @@
272 276
273/* `assert' options */ 277/* `assert' options */
274 278
275/* environment variable that holds the search path for packages */ 279/* environment variables that hold the search path for packages */
276#define LUA_PATH "LUA_PATH" 280#define LUA_PATH "LUA_PATH"
281#define LUA_CPATH "LUA_CPATH"
282
283/* prefix for open functions in C libraries */
284#if defined(__APPLE__) && defined(__MACH__)
285#define LUA_POF "_luaopen_"
286#else
287#define LUA_POF "luaopen_"
288#endif
277 289
278/* directory separator (for submodules) */ 290/* directory separator (for submodules) */
279#define LUA_DIRSEP "/" 291#define LUA_DIRSEP "/"