diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 84 |
1 files changed, 42 insertions, 42 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.105 2002/11/07 15:39:23 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.106 2002/11/14 12:01:35 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 | */ |
@@ -47,19 +47,19 @@ static int luaB_print (lua_State *L) { | |||
47 | 47 | ||
48 | 48 | ||
49 | static int luaB_tonumber (lua_State *L) { | 49 | static int luaB_tonumber (lua_State *L) { |
50 | int base = luaL_opt_int(L, 2, 10); | 50 | int base = luaL_optint(L, 2, 10); |
51 | if (base == 10) { /* standard conversion */ | 51 | if (base == 10) { /* standard conversion */ |
52 | luaL_check_any(L, 1); | 52 | luaL_checkany(L, 1); |
53 | if (lua_isnumber(L, 1)) { | 53 | if (lua_isnumber(L, 1)) { |
54 | lua_pushnumber(L, lua_tonumber(L, 1)); | 54 | lua_pushnumber(L, lua_tonumber(L, 1)); |
55 | return 1; | 55 | return 1; |
56 | } | 56 | } |
57 | } | 57 | } |
58 | else { | 58 | else { |
59 | const char *s1 = luaL_check_string(L, 1); | 59 | const char *s1 = luaL_checkstring(L, 1); |
60 | char *s2; | 60 | char *s2; |
61 | unsigned long n; | 61 | unsigned long n; |
62 | luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range"); | 62 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); |
63 | n = strtoul(s1, &s2, base); | 63 | n = strtoul(s1, &s2, base); |
64 | if (s1 != s2) { /* at least one valid digit? */ | 64 | if (s1 != s2) { /* at least one valid digit? */ |
65 | while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ | 65 | while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ |
@@ -75,8 +75,8 @@ static int luaB_tonumber (lua_State *L) { | |||
75 | 75 | ||
76 | 76 | ||
77 | static int luaB_error (lua_State *L) { | 77 | static int luaB_error (lua_State *L) { |
78 | int level = luaL_opt_int(L, 2, 1); | 78 | int level = luaL_optint(L, 2, 1); |
79 | luaL_check_any(L, 1); | 79 | luaL_checkany(L, 1); |
80 | if (!lua_isstring(L, 1) || level == 0) | 80 | if (!lua_isstring(L, 1) || level == 0) |
81 | lua_pushvalue(L, 1); /* propagate error mesage without changes */ | 81 | lua_pushvalue(L, 1); /* propagate error mesage without changes */ |
82 | else { /* add extra information */ | 82 | else { /* add extra information */ |
@@ -89,7 +89,7 @@ static int luaB_error (lua_State *L) { | |||
89 | 89 | ||
90 | 90 | ||
91 | static int luaB_getmetatable (lua_State *L) { | 91 | static int luaB_getmetatable (lua_State *L) { |
92 | luaL_check_any(L, 1); | 92 | luaL_checkany(L, 1); |
93 | if (!lua_getmetatable(L, 1)) { | 93 | if (!lua_getmetatable(L, 1)) { |
94 | lua_pushnil(L); | 94 | lua_pushnil(L); |
95 | return 1; /* no metatable */ | 95 | return 1; /* no metatable */ |
@@ -101,8 +101,8 @@ static int luaB_getmetatable (lua_State *L) { | |||
101 | 101 | ||
102 | static int luaB_setmetatable (lua_State *L) { | 102 | static int luaB_setmetatable (lua_State *L) { |
103 | int t = lua_type(L, 2); | 103 | int t = lua_type(L, 2); |
104 | luaL_check_type(L, 1, LUA_TTABLE); | 104 | luaL_checktype(L, 1, LUA_TTABLE); |
105 | luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, | 105 | luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, |
106 | "nil or table expected"); | 106 | "nil or table expected"); |
107 | if (luaL_getmetafield(L, 1, "__metatable")) | 107 | if (luaL_getmetafield(L, 1, "__metatable")) |
108 | luaL_error(L, "cannot change a protected metatable"); | 108 | luaL_error(L, "cannot change a protected metatable"); |
@@ -116,8 +116,8 @@ static void getfunc (lua_State *L) { | |||
116 | if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); | 116 | if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); |
117 | else { | 117 | else { |
118 | lua_Debug ar; | 118 | lua_Debug ar; |
119 | int level = luaL_opt_int(L, 1, 1); | 119 | int level = luaL_optint(L, 1, 1); |
120 | luaL_arg_check(L, level >= 0, 1, "level must be non-negative"); | 120 | luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); |
121 | if (lua_getstack(L, level, &ar) == 0) | 121 | if (lua_getstack(L, level, &ar) == 0) |
122 | luaL_argerror(L, 1, "invalid level"); | 122 | luaL_argerror(L, 1, "invalid level"); |
123 | lua_getinfo(L, "f", &ar); | 123 | lua_getinfo(L, "f", &ar); |
@@ -142,7 +142,7 @@ static int luaB_getglobals (lua_State *L) { | |||
142 | 142 | ||
143 | 143 | ||
144 | static int luaB_setglobals (lua_State *L) { | 144 | static int luaB_setglobals (lua_State *L) { |
145 | luaL_check_type(L, 2, LUA_TTABLE); | 145 | luaL_checktype(L, 2, LUA_TTABLE); |
146 | getfunc(L); | 146 | getfunc(L); |
147 | if (aux_getglobals(L)) /* __globals defined? */ | 147 | if (aux_getglobals(L)) /* __globals defined? */ |
148 | luaL_error(L, "cannot change a protected global table"); | 148 | luaL_error(L, "cannot change a protected global table"); |
@@ -156,24 +156,24 @@ static int luaB_setglobals (lua_State *L) { | |||
156 | 156 | ||
157 | 157 | ||
158 | static int luaB_rawequal (lua_State *L) { | 158 | static int luaB_rawequal (lua_State *L) { |
159 | luaL_check_any(L, 1); | 159 | luaL_checkany(L, 1); |
160 | luaL_check_any(L, 2); | 160 | luaL_checkany(L, 2); |
161 | lua_pushboolean(L, lua_rawequal(L, 1, 2)); | 161 | lua_pushboolean(L, lua_rawequal(L, 1, 2)); |
162 | return 1; | 162 | return 1; |
163 | } | 163 | } |
164 | 164 | ||
165 | 165 | ||
166 | static int luaB_rawget (lua_State *L) { | 166 | static int luaB_rawget (lua_State *L) { |
167 | luaL_check_type(L, 1, LUA_TTABLE); | 167 | luaL_checktype(L, 1, LUA_TTABLE); |
168 | luaL_check_any(L, 2); | 168 | luaL_checkany(L, 2); |
169 | lua_rawget(L, 1); | 169 | lua_rawget(L, 1); |
170 | return 1; | 170 | return 1; |
171 | } | 171 | } |
172 | 172 | ||
173 | static int luaB_rawset (lua_State *L) { | 173 | static int luaB_rawset (lua_State *L) { |
174 | luaL_check_type(L, 1, LUA_TTABLE); | 174 | luaL_checktype(L, 1, LUA_TTABLE); |
175 | luaL_check_any(L, 2); | 175 | luaL_checkany(L, 2); |
176 | luaL_check_any(L, 3); | 176 | luaL_checkany(L, 3); |
177 | lua_rawset(L, 1); | 177 | lua_rawset(L, 1); |
178 | return 1; | 178 | return 1; |
179 | } | 179 | } |
@@ -187,20 +187,20 @@ static int luaB_gcinfo (lua_State *L) { | |||
187 | 187 | ||
188 | 188 | ||
189 | static int luaB_collectgarbage (lua_State *L) { | 189 | static int luaB_collectgarbage (lua_State *L) { |
190 | lua_setgcthreshold(L, luaL_opt_int(L, 1, 0)); | 190 | lua_setgcthreshold(L, luaL_optint(L, 1, 0)); |
191 | return 0; | 191 | return 0; |
192 | } | 192 | } |
193 | 193 | ||
194 | 194 | ||
195 | static int luaB_type (lua_State *L) { | 195 | static int luaB_type (lua_State *L) { |
196 | luaL_check_any(L, 1); | 196 | luaL_checkany(L, 1); |
197 | lua_pushstring(L, lua_typename(L, lua_type(L, 1))); | 197 | lua_pushstring(L, lua_typename(L, lua_type(L, 1))); |
198 | return 1; | 198 | return 1; |
199 | } | 199 | } |
200 | 200 | ||
201 | 201 | ||
202 | static int luaB_next (lua_State *L) { | 202 | static int luaB_next (lua_State *L) { |
203 | luaL_check_type(L, 1, LUA_TTABLE); | 203 | luaL_checktype(L, 1, LUA_TTABLE); |
204 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ | 204 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ |
205 | if (lua_next(L, 1)) | 205 | if (lua_next(L, 1)) |
206 | return 2; | 206 | return 2; |
@@ -212,7 +212,7 @@ static int luaB_next (lua_State *L) { | |||
212 | 212 | ||
213 | 213 | ||
214 | static int luaB_pairs (lua_State *L) { | 214 | static int luaB_pairs (lua_State *L) { |
215 | luaL_check_type(L, 1, LUA_TTABLE); | 215 | luaL_checktype(L, 1, LUA_TTABLE); |
216 | lua_getglobal(L, "next"); /* return generator, */ | 216 | lua_getglobal(L, "next"); /* return generator, */ |
217 | lua_pushvalue(L, 1); /* state, */ | 217 | lua_pushvalue(L, 1); /* state, */ |
218 | lua_pushnil(L); /* and initial value */ | 218 | lua_pushnil(L); /* and initial value */ |
@@ -222,7 +222,7 @@ static int luaB_pairs (lua_State *L) { | |||
222 | 222 | ||
223 | static int luaB_ipairs (lua_State *L) { | 223 | static int luaB_ipairs (lua_State *L) { |
224 | lua_Number i = lua_tonumber(L, 2); | 224 | lua_Number i = lua_tonumber(L, 2); |
225 | luaL_check_type(L, 1, LUA_TTABLE); | 225 | luaL_checktype(L, 1, LUA_TTABLE); |
226 | if (i == 0 && lua_isnull(L, 2)) { /* `for' start? */ | 226 | if (i == 0 && lua_isnull(L, 2)) { /* `for' start? */ |
227 | lua_getglobal(L, "ipairs"); /* return generator, */ | 227 | lua_getglobal(L, "ipairs"); /* return generator, */ |
228 | lua_pushvalue(L, 1); /* state, */ | 228 | lua_pushvalue(L, 1); /* state, */ |
@@ -250,8 +250,8 @@ static int passresults (lua_State *L, int status) { | |||
250 | 250 | ||
251 | static int luaB_loadstring (lua_State *L) { | 251 | static int luaB_loadstring (lua_State *L) { |
252 | size_t l; | 252 | size_t l; |
253 | const char *s = luaL_check_lstr(L, 1, &l); | 253 | const char *s = luaL_checklstring(L, 1, &l); |
254 | const char *chunkname = luaL_opt_string(L, 2, s); | 254 | const char *chunkname = luaL_optstring(L, 2, s); |
255 | return passresults(L, luaL_loadbuffer(L, s, l, chunkname)); | 255 | return passresults(L, luaL_loadbuffer(L, s, l, chunkname)); |
256 | } | 256 | } |
257 | 257 | ||
@@ -265,7 +265,7 @@ static int writer (lua_State *L, const void* b, size_t size, void* B) { | |||
265 | 265 | ||
266 | static int luaB_stringdump (lua_State *L) { | 266 | static int luaB_stringdump (lua_State *L) { |
267 | luaL_Buffer b; | 267 | luaL_Buffer b; |
268 | luaL_check_type(L, 1, LUA_TFUNCTION); | 268 | luaL_checktype(L, 1, LUA_TFUNCTION); |
269 | luaL_buffinit(L,&b); | 269 | luaL_buffinit(L,&b); |
270 | if (!lua_dump(L, writer, &b)) | 270 | if (!lua_dump(L, writer, &b)) |
271 | luaL_error(L, "unable to dump given function"); | 271 | luaL_error(L, "unable to dump given function"); |
@@ -276,13 +276,13 @@ static int luaB_stringdump (lua_State *L) { | |||
276 | 276 | ||
277 | 277 | ||
278 | static int luaB_loadfile (lua_State *L) { | 278 | static int luaB_loadfile (lua_State *L) { |
279 | const char *fname = luaL_opt_string(L, 1, NULL); | 279 | const char *fname = luaL_optstring(L, 1, NULL); |
280 | return passresults(L, luaL_loadfile(L, fname)); | 280 | return passresults(L, luaL_loadfile(L, fname)); |
281 | } | 281 | } |
282 | 282 | ||
283 | 283 | ||
284 | static int luaB_dofile (lua_State *L) { | 284 | static int luaB_dofile (lua_State *L) { |
285 | const char *fname = luaL_opt_string(L, 1, NULL); | 285 | const char *fname = luaL_optstring(L, 1, NULL); |
286 | int status = luaL_loadfile(L, fname); | 286 | int status = luaL_loadfile(L, fname); |
287 | if (status != 0) lua_error(L); | 287 | if (status != 0) lua_error(L); |
288 | lua_call(L, 0, LUA_MULTRET); | 288 | lua_call(L, 0, LUA_MULTRET); |
@@ -291,9 +291,9 @@ static int luaB_dofile (lua_State *L) { | |||
291 | 291 | ||
292 | 292 | ||
293 | static int luaB_assert (lua_State *L) { | 293 | static int luaB_assert (lua_State *L) { |
294 | luaL_check_any(L, 1); | 294 | luaL_checkany(L, 1); |
295 | if (!lua_toboolean(L, 1)) | 295 | if (!lua_toboolean(L, 1)) |
296 | return luaL_error(L, "%s", luaL_opt_string(L, 2, "assertion failed!")); | 296 | return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); |
297 | lua_settop(L, 1); | 297 | lua_settop(L, 1); |
298 | return 1; | 298 | return 1; |
299 | } | 299 | } |
@@ -301,12 +301,12 @@ static int luaB_assert (lua_State *L) { | |||
301 | 301 | ||
302 | static int luaB_unpack (lua_State *L) { | 302 | static int luaB_unpack (lua_State *L) { |
303 | int n, i; | 303 | int n, i; |
304 | luaL_check_type(L, 1, LUA_TTABLE); | 304 | luaL_checktype(L, 1, LUA_TTABLE); |
305 | lua_pushliteral(L, "n"); | 305 | lua_pushliteral(L, "n"); |
306 | lua_rawget(L, 1); | 306 | lua_rawget(L, 1); |
307 | n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1; | 307 | n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1; |
308 | for (i=0; i<n || n==-1; i++) { /* push arg[1...n] */ | 308 | for (i=0; i<n || n==-1; i++) { /* push arg[1...n] */ |
309 | luaL_check_stack(L, LUA_MINSTACK, "table too big to unpack"); | 309 | luaL_checkstack(L, LUA_MINSTACK, "table too big to unpack"); |
310 | lua_rawgeti(L, 1, i+1); | 310 | lua_rawgeti(L, 1, i+1); |
311 | if (n == -1) { /* no explicit limit? */ | 311 | if (n == -1) { /* no explicit limit? */ |
312 | if (lua_isnil(L, -1)) { /* stop at first `nil' element */ | 312 | if (lua_isnil(L, -1)) { /* stop at first `nil' element */ |
@@ -321,7 +321,7 @@ static int luaB_unpack (lua_State *L) { | |||
321 | 321 | ||
322 | static int luaB_pcall (lua_State *L) { | 322 | static int luaB_pcall (lua_State *L) { |
323 | int status; | 323 | int status; |
324 | luaL_check_any(L, 1); | 324 | luaL_checkany(L, 1); |
325 | status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); | 325 | status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); |
326 | lua_pushboolean(L, (status == 0)); | 326 | lua_pushboolean(L, (status == 0)); |
327 | lua_insert(L, 1); | 327 | lua_insert(L, 1); |
@@ -331,7 +331,7 @@ static int luaB_pcall (lua_State *L) { | |||
331 | 331 | ||
332 | static int luaB_xpcall (lua_State *L) { | 332 | static int luaB_xpcall (lua_State *L) { |
333 | int status; | 333 | int status; |
334 | luaL_check_any(L, 2); | 334 | luaL_checkany(L, 2); |
335 | lua_settop(L, 2); | 335 | lua_settop(L, 2); |
336 | lua_insert(L, 1); /* put error function under function to be called */ | 336 | lua_insert(L, 1); /* put error function under function to be called */ |
337 | status = lua_pcall(L, 0, LUA_MULTRET, 1); | 337 | status = lua_pcall(L, 0, LUA_MULTRET, 1); |
@@ -396,7 +396,7 @@ static int luaB_newproxy (lua_State *L) { | |||
396 | validproxy = lua_toboolean(L, -1); | 396 | validproxy = lua_toboolean(L, -1); |
397 | lua_pop(L, 1); /* remove value */ | 397 | lua_pop(L, 1); /* remove value */ |
398 | } | 398 | } |
399 | luaL_arg_check(L, validproxy, 1, "boolean/proxy expected"); | 399 | luaL_argcheck(L, validproxy, 1, "boolean/proxy expected"); |
400 | lua_getmetatable(L, 1); /* metatable is valid; get it */ | 400 | lua_getmetatable(L, 1); /* metatable is valid; get it */ |
401 | } | 401 | } |
402 | lua_setmetatable(L, 2); | 402 | lua_setmetatable(L, 2); |
@@ -463,7 +463,7 @@ static void pushcomposename (lua_State *L) { | |||
463 | static int luaB_require (lua_State *L) { | 463 | static int luaB_require (lua_State *L) { |
464 | const char *path; | 464 | const char *path; |
465 | int status = LUA_ERRFILE; /* not found (yet) */ | 465 | int status = LUA_ERRFILE; /* not found (yet) */ |
466 | luaL_check_string(L, 1); | 466 | luaL_checkstring(L, 1); |
467 | lua_settop(L, 1); | 467 | lua_settop(L, 1); |
468 | lua_pushvalue(L, 1); | 468 | lua_pushvalue(L, 1); |
469 | lua_setglobal(L, "_REQUIREDNAME"); | 469 | lua_setglobal(L, "_REQUIREDNAME"); |
@@ -563,7 +563,7 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { | |||
563 | static int luaB_coresume (lua_State *L) { | 563 | static int luaB_coresume (lua_State *L) { |
564 | lua_State *co = lua_tothread(L, 1); | 564 | lua_State *co = lua_tothread(L, 1); |
565 | int r; | 565 | int r; |
566 | luaL_arg_check(L, co, 1, "coroutine/thread expected"); | 566 | luaL_argcheck(L, co, 1, "coroutine/thread expected"); |
567 | r = auxresume(L, co, lua_gettop(L) - 1); | 567 | r = auxresume(L, co, lua_gettop(L) - 1); |
568 | if (r < 0) { | 568 | if (r < 0) { |
569 | lua_pushboolean(L, 0); | 569 | lua_pushboolean(L, 0); |
@@ -588,7 +588,7 @@ static int luaB_auxwrap (lua_State *L) { | |||
588 | 588 | ||
589 | static int luaB_cocreate (lua_State *L) { | 589 | static int luaB_cocreate (lua_State *L) { |
590 | lua_State *NL = lua_newthread(L); | 590 | lua_State *NL = lua_newthread(L); |
591 | luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, | 591 | luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, |
592 | "Lua function expected"); | 592 | "Lua function expected"); |
593 | lua_pushvalue(L, 1); /* move function to top */ | 593 | lua_pushvalue(L, 1); /* move function to top */ |
594 | lua_xmove(L, NL, 1); /* move function from L to NL */ | 594 | lua_xmove(L, NL, 1); /* move function from L to NL */ |
@@ -622,7 +622,7 @@ static const luaL_reg co_funcs[] = { | |||
622 | static void base_open (lua_State *L) { | 622 | static void base_open (lua_State *L) { |
623 | lua_pushliteral(L, "_G"); | 623 | lua_pushliteral(L, "_G"); |
624 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 624 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
625 | luaL_openlib(L, base_funcs, 0); /* open lib into global table */ | 625 | luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */ |
626 | lua_pushliteral(L, "_VERSION"); | 626 | lua_pushliteral(L, "_VERSION"); |
627 | lua_pushliteral(L, LUA_VERSION); | 627 | lua_pushliteral(L, LUA_VERSION); |
628 | lua_rawset(L, -3); /* set global _VERSION */ | 628 | lua_rawset(L, -3); /* set global _VERSION */ |
@@ -642,7 +642,7 @@ static void base_open (lua_State *L) { | |||
642 | 642 | ||
643 | LUALIB_API int lua_baselibopen (lua_State *L) { | 643 | LUALIB_API int lua_baselibopen (lua_State *L) { |
644 | base_open(L); | 644 | base_open(L); |
645 | luaL_opennamedlib(L, LUA_COLIBNAME, co_funcs, 0); | 645 | luaL_openlib(L, LUA_COLIBNAME, co_funcs, 0); |
646 | lua_newtable(L); | 646 | lua_newtable(L); |
647 | lua_setglobal(L, REQTAB); | 647 | lua_setglobal(L, REQTAB); |
648 | return 0; | 648 | return 0; |