diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 41 |
1 files changed, 19 insertions, 22 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.70 2002/05/01 20:40:42 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.71 2002/05/02 17:12:27 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 | */ |
@@ -43,9 +43,7 @@ static int luaB__ERRORMESSAGE (lua_State *L) { | |||
43 | if (lua_getstack(L, 1, &ar)) { | 43 | if (lua_getstack(L, 1, &ar)) { |
44 | lua_getinfo(L, "Sl", &ar); | 44 | lua_getinfo(L, "Sl", &ar); |
45 | if (ar.source && ar.currentline > 0) { | 45 | if (ar.source && ar.currentline > 0) { |
46 | char buff[100]; | 46 | luaL_vstr(L, "\n <%s: line %d>", ar.short_src, ar.currentline); |
47 | sprintf(buff, "\n <%.70s: line %d>", ar.short_src, ar.currentline); | ||
48 | lua_pushstring(L, buff); | ||
49 | lua_concat(L, 2); | 47 | lua_concat(L, 2); |
50 | } | 48 | } |
51 | } | 49 | } |
@@ -72,7 +70,7 @@ static int luaB_print (lua_State *L) { | |||
72 | lua_rawcall(L, 1, 1); | 70 | lua_rawcall(L, 1, 1); |
73 | s = lua_tostring(L, -1); /* get result */ | 71 | s = lua_tostring(L, -1); /* get result */ |
74 | if (s == NULL) | 72 | if (s == NULL) |
75 | luaL_verror(L, "`tostring' must return a string to `print'"); | 73 | return luaL_verror(L, "`tostring' must return a string to `print'"); |
76 | if (i>1) fputs("\t", stdout); | 74 | if (i>1) fputs("\t", stdout); |
77 | fputs(s, stdout); | 75 | fputs(s, stdout); |
78 | lua_pop(L, 1); /* pop result */ | 76 | lua_pop(L, 1); /* pop result */ |
@@ -112,8 +110,7 @@ static int luaB_tonumber (lua_State *L) { | |||
112 | 110 | ||
113 | static int luaB_error (lua_State *L) { | 111 | static int luaB_error (lua_State *L) { |
114 | lua_settop(L, 1); | 112 | lua_settop(L, 1); |
115 | lua_errorobj(L); | 113 | return lua_errorobj(L); |
116 | return 0; /* to avoid warnings */ | ||
117 | } | 114 | } |
118 | 115 | ||
119 | 116 | ||
@@ -242,7 +239,7 @@ static int luaB_loadfile (lua_State *L) { | |||
242 | static int luaB_assert (lua_State *L) { | 239 | static int luaB_assert (lua_State *L) { |
243 | luaL_check_any(L, 1); | 240 | luaL_check_any(L, 1); |
244 | if (!lua_toboolean(L, 1)) | 241 | if (!lua_toboolean(L, 1)) |
245 | luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, "")); | 242 | return luaL_verror(L, "assertion failed! %s", luaL_opt_string(L, 2, "")); |
246 | lua_settop(L, 1); | 243 | lua_settop(L, 1); |
247 | return 1; | 244 | return 1; |
248 | } | 245 | } |
@@ -335,6 +332,7 @@ static const char *getpath (lua_State *L) { | |||
335 | const char *path; | 332 | const char *path; |
336 | lua_getglobal(L, LUA_PATH); /* try global variable */ | 333 | lua_getglobal(L, LUA_PATH); /* try global variable */ |
337 | path = lua_tostring(L, -1); | 334 | path = lua_tostring(L, -1); |
335 | lua_pop(L, 1); | ||
338 | if (path) return path; | 336 | if (path) return path; |
339 | path = getenv(LUA_PATH); /* else try environment variable */ | 337 | path = getenv(LUA_PATH); /* else try environment variable */ |
340 | if (path) return path; | 338 | if (path) return path; |
@@ -342,7 +340,7 @@ static const char *getpath (lua_State *L) { | |||
342 | } | 340 | } |
343 | 341 | ||
344 | 342 | ||
345 | static const char *nextpath (lua_State *L, const char *path) { | 343 | static const char *pushnextpath (lua_State *L, const char *path) { |
346 | const char *l; | 344 | const char *l; |
347 | if (*path == '\0') return NULL; /* no more pathes */ | 345 | if (*path == '\0') return NULL; /* no more pathes */ |
348 | if (*path == LUA_PATH_SEP) path++; /* skip separator */ | 346 | if (*path == LUA_PATH_SEP) path++; /* skip separator */ |
@@ -353,7 +351,7 @@ static const char *nextpath (lua_State *L, const char *path) { | |||
353 | } | 351 | } |
354 | 352 | ||
355 | 353 | ||
356 | static void composename (lua_State *L) { | 354 | static void pushcomposename (lua_State *L) { |
357 | const char *path = lua_tostring(L, -1); | 355 | const char *path = lua_tostring(L, -1); |
358 | const char *wild = strchr(path, '?'); | 356 | const char *wild = strchr(path, '?'); |
359 | if (wild == NULL) return; /* no wild char; path is the file name */ | 357 | if (wild == NULL) return; /* no wild char; path is the file name */ |
@@ -372,35 +370,34 @@ static int luaB_require (lua_State *L) { | |||
372 | lua_pushvalue(L, 1); | 370 | lua_pushvalue(L, 1); |
373 | lua_setglobal(L, "_REQUIREDNAME"); | 371 | lua_setglobal(L, "_REQUIREDNAME"); |
374 | lua_getglobal(L, REQTAB); | 372 | lua_getglobal(L, REQTAB); |
375 | if (!lua_istable(L, 2)) luaL_verror(L, REQTAB " is not a table"); | 373 | if (!lua_istable(L, 2)) return luaL_verror(L, REQTAB " is not a table"); |
376 | path = getpath(L); | 374 | path = getpath(L); |
377 | lua_pushvalue(L, 1); /* check package's name in book-keeping table */ | 375 | lua_pushvalue(L, 1); /* check package's name in book-keeping table */ |
378 | lua_gettable(L, 2); | 376 | lua_gettable(L, 2); |
379 | if (!lua_isnil(L, -1)) /* is it there? */ | 377 | if (!lua_isnil(L, -1)) /* is it there? */ |
380 | return 0; /* package is already loaded */ | 378 | return 0; /* package is already loaded */ |
381 | else { /* must load it */ | 379 | else { /* must load it */ |
382 | while (status == LUA_ERRFILE && (path = nextpath(L, path)) != NULL) { | 380 | while (status == LUA_ERRFILE) { |
383 | composename(L); | 381 | lua_settop(L, 3); /* reset stack position */ |
382 | if ((path = pushnextpath(L, path)) == NULL) break; | ||
383 | pushcomposename(L); | ||
384 | status = lua_loadfile(L, lua_tostring(L, -1)); /* try to load it */ | 384 | status = lua_loadfile(L, lua_tostring(L, -1)); /* try to load it */ |
385 | if (status == 0) | ||
386 | status = lua_pcall(L, 0, 0, 0); | ||
387 | lua_settop(L, 3); /* pop string and eventual results from dofile */ | ||
388 | } | 385 | } |
389 | } | 386 | } |
390 | switch (status) { | 387 | switch (status) { |
391 | case 0: { | 388 | case 0: { |
389 | lua_rawcall(L, 0, 0); /* run loaded module */ | ||
392 | lua_pushvalue(L, 1); | 390 | lua_pushvalue(L, 1); |
393 | lua_pushboolean(L, 1); | 391 | lua_pushboolean(L, 1); |
394 | lua_settable(L, 2); /* mark it as loaded */ | 392 | lua_settable(L, 2); /* mark it as loaded */ |
395 | return 0; | 393 | return 0; |
396 | } | 394 | } |
397 | case LUA_ERRFILE: { /* file not found */ | 395 | case LUA_ERRFILE: { /* file not found */ |
398 | luaL_verror(L, "could not load package `%.20s' from path `%.200s'", | 396 | return luaL_verror(L, "could not load package `%s' from path `%s'", |
399 | lua_tostring(L, 1), lua_tostring(L, 3)); | 397 | lua_tostring(L, 1), getpath(L)); |
400 | } | 398 | } |
401 | default: { | 399 | default: { |
402 | luaL_verror(L, "error loading package"); | 400 | return luaL_verror(L, "error loading package\n%s", lua_tostring(L, -1)); |
403 | return 0; /* to avoid warnings */ | ||
404 | } | 401 | } |
405 | } | 402 | } |
406 | } | 403 | } |
@@ -445,7 +442,7 @@ static int luaB_resume (lua_State *L) { | |||
445 | lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1)); | 442 | lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1)); |
446 | lua_settop(L, 0); | 443 | lua_settop(L, 0); |
447 | if (lua_resume(L, co) != 0) | 444 | if (lua_resume(L, co) != 0) |
448 | lua_errorobj(L); | 445 | return lua_errorobj(L); |
449 | return lua_gettop(L); | 446 | return lua_gettop(L); |
450 | } | 447 | } |
451 | 448 | ||
@@ -466,7 +463,7 @@ static int luaB_coroutine (lua_State *L) { | |||
466 | luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, | 463 | luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, |
467 | "Lua function expected"); | 464 | "Lua function expected"); |
468 | NL = lua_newthread(L); | 465 | NL = lua_newthread(L); |
469 | if (NL == NULL) luaL_verror(L, "unable to create new thread"); | 466 | if (NL == NULL) return luaL_verror(L, "unable to create new thread"); |
470 | /* move function and arguments from L to NL */ | 467 | /* move function and arguments from L to NL */ |
471 | for (i=0; i<n; i++) { | 468 | for (i=0; i<n; i++) { |
472 | ref = lua_ref(L, 1); | 469 | ref = lua_ref(L, 1); |