aboutsummaryrefslogtreecommitdiff
path: root/src/lua/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lua/lbaselib.c (renamed from src/lua-5.3/lbaselib.c)139
1 files changed, 84 insertions, 55 deletions
diff --git a/src/lua-5.3/lbaselib.c b/src/lua/lbaselib.c
index 6460e4f..747fd45 100644
--- a/src/lua-5.3/lbaselib.c
+++ b/src/lua/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.314.1.1 2017/04/19 17:39:34 roberto Exp $ 2** $Id: lbaselib.c $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -24,18 +24,12 @@
24static int luaB_print (lua_State *L) { 24static int luaB_print (lua_State *L) {
25 int n = lua_gettop(L); /* number of arguments */ 25 int n = lua_gettop(L); /* number of arguments */
26 int i; 26 int i;
27 lua_getglobal(L, "tostring"); 27 for (i = 1; i <= n; i++) { /* for each argument */
28 for (i=1; i<=n; i++) {
29 const char *s;
30 size_t l; 28 size_t l;
31 lua_pushvalue(L, -1); /* function to be called */ 29 const char *s = luaL_tolstring(L, i, &l); /* convert it to string */
32 lua_pushvalue(L, i); /* value to print */ 30 if (i > 1) /* not the first element? */
33 lua_call(L, 1, 1); 31 lua_writestring("\t", 1); /* add a tab before it */
34 s = lua_tolstring(L, -1, &l); /* get result */ 32 lua_writestring(s, l); /* print it */
35 if (s == NULL)
36 return luaL_error(L, "'tostring' must return a string to 'print'");
37 if (i>1) lua_writestring("\t", 1);
38 lua_writestring(s, l);
39 lua_pop(L, 1); /* pop result */ 33 lua_pop(L, 1); /* pop result */
40 } 34 }
41 lua_writeline(); 35 lua_writeline();
@@ -43,13 +37,31 @@ static int luaB_print (lua_State *L) {
43} 37}
44 38
45 39
40/*
41** Creates a warning with all given arguments.
42** Check first for errors; otherwise an error may interrupt
43** the composition of a warning, leaving it unfinished.
44*/
45static int luaB_warn (lua_State *L) {
46 int n = lua_gettop(L); /* number of arguments */
47 int i;
48 luaL_checkstring(L, 1); /* at least one argument */
49 for (i = 2; i <= n; i++)
50 luaL_checkstring(L, i); /* make sure all arguments are strings */
51 for (i = 1; i < n; i++) /* compose warning */
52 lua_warning(L, lua_tostring(L, i), 1);
53 lua_warning(L, lua_tostring(L, n), 0); /* close warning */
54 return 0;
55}
56
57
46#define SPACECHARS " \f\n\r\t\v" 58#define SPACECHARS " \f\n\r\t\v"
47 59
48static const char *b_str2int (const char *s, int base, lua_Integer *pn) { 60static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
49 lua_Unsigned n = 0; 61 lua_Unsigned n = 0;
50 int neg = 0; 62 int neg = 0;
51 s += strspn(s, SPACECHARS); /* skip initial spaces */ 63 s += strspn(s, SPACECHARS); /* skip initial spaces */
52 if (*s == '-') { s++; neg = 1; } /* handle signal */ 64 if (*s == '-') { s++; neg = 1; } /* handle sign */
53 else if (*s == '+') s++; 65 else if (*s == '+') s++;
54 if (!isalnum((unsigned char)*s)) /* no digit? */ 66 if (!isalnum((unsigned char)*s)) /* no digit? */
55 return NULL; 67 return NULL;
@@ -68,7 +80,6 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
68 80
69static int luaB_tonumber (lua_State *L) { 81static int luaB_tonumber (lua_State *L) {
70 if (lua_isnoneornil(L, 2)) { /* standard conversion? */ 82 if (lua_isnoneornil(L, 2)) { /* standard conversion? */
71 luaL_checkany(L, 1);
72 if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ 83 if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
73 lua_settop(L, 1); /* yes; return it */ 84 lua_settop(L, 1); /* yes; return it */
74 return 1; 85 return 1;
@@ -79,6 +90,7 @@ static int luaB_tonumber (lua_State *L) {
79 if (s != NULL && lua_stringtonumber(L, s) == l + 1) 90 if (s != NULL && lua_stringtonumber(L, s) == l + 1)
80 return 1; /* successful conversion to number */ 91 return 1; /* successful conversion to number */
81 /* else not a number */ 92 /* else not a number */
93 luaL_checkany(L, 1); /* (but there must be some parameter) */
82 } 94 }
83 } 95 }
84 else { 96 else {
@@ -94,7 +106,7 @@ static int luaB_tonumber (lua_State *L) {
94 return 1; 106 return 1;
95 } /* else not a number */ 107 } /* else not a number */
96 } /* else not a number */ 108 } /* else not a number */
97 lua_pushnil(L); /* not a number */ 109 luaL_pushfail(L); /* not a number */
98 return 1; 110 return 1;
99} 111}
100 112
@@ -125,8 +137,7 @@ static int luaB_getmetatable (lua_State *L) {
125static int luaB_setmetatable (lua_State *L) { 137static int luaB_setmetatable (lua_State *L) {
126 int t = lua_type(L, 2); 138 int t = lua_type(L, 2);
127 luaL_checktype(L, 1, LUA_TTABLE); 139 luaL_checktype(L, 1, LUA_TTABLE);
128 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 140 luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
129 "nil or table expected");
130 if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) 141 if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)
131 return luaL_error(L, "cannot change a protected metatable"); 142 return luaL_error(L, "cannot change a protected metatable");
132 lua_settop(L, 2); 143 lua_settop(L, 2);
@@ -145,8 +156,8 @@ static int luaB_rawequal (lua_State *L) {
145 156
146static int luaB_rawlen (lua_State *L) { 157static int luaB_rawlen (lua_State *L) {
147 int t = lua_type(L, 1); 158 int t = lua_type(L, 1);
148 luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, 159 luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
149 "table or string expected"); 160 "table or string");
150 lua_pushinteger(L, lua_rawlen(L, 1)); 161 lua_pushinteger(L, lua_rawlen(L, 1));
151 return 1; 162 return 1;
152} 163}
@@ -170,27 +181,58 @@ static int luaB_rawset (lua_State *L) {
170} 181}
171 182
172 183
184static int pushmode (lua_State *L, int oldmode) {
185 lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational");
186 return 1;
187}
188
189
173static int luaB_collectgarbage (lua_State *L) { 190static int luaB_collectgarbage (lua_State *L) {
174 static const char *const opts[] = {"stop", "restart", "collect", 191 static const char *const opts[] = {"stop", "restart", "collect",
175 "count", "step", "setpause", "setstepmul", 192 "count", "step", "setpause", "setstepmul",
176 "isrunning", NULL}; 193 "isrunning", "generational", "incremental", NULL};
177 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, 194 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
178 LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, 195 LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
179 LUA_GCISRUNNING}; 196 LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
180 int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; 197 int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
181 int ex = (int)luaL_optinteger(L, 2, 0);
182 int res = lua_gc(L, o, ex);
183 switch (o) { 198 switch (o) {
184 case LUA_GCCOUNT: { 199 case LUA_GCCOUNT: {
185 int b = lua_gc(L, LUA_GCCOUNTB, 0); 200 int k = lua_gc(L, o);
186 lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024)); 201 int b = lua_gc(L, LUA_GCCOUNTB);
202 lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
203 return 1;
204 }
205 case LUA_GCSTEP: {
206 int step = (int)luaL_optinteger(L, 2, 0);
207 int res = lua_gc(L, o, step);
208 lua_pushboolean(L, res);
209 return 1;
210 }
211 case LUA_GCSETPAUSE:
212 case LUA_GCSETSTEPMUL: {
213 int p = (int)luaL_optinteger(L, 2, 0);
214 int previous = lua_gc(L, o, p);
215 lua_pushinteger(L, previous);
187 return 1; 216 return 1;
188 } 217 }
189 case LUA_GCSTEP: case LUA_GCISRUNNING: { 218 case LUA_GCISRUNNING: {
219 int res = lua_gc(L, o);
190 lua_pushboolean(L, res); 220 lua_pushboolean(L, res);
191 return 1; 221 return 1;
192 } 222 }
223 case LUA_GCGEN: {
224 int minormul = (int)luaL_optinteger(L, 2, 0);
225 int majormul = (int)luaL_optinteger(L, 3, 0);
226 return pushmode(L, lua_gc(L, o, minormul, majormul));
227 }
228 case LUA_GCINC: {
229 int pause = (int)luaL_optinteger(L, 2, 0);
230 int stepmul = (int)luaL_optinteger(L, 3, 0);
231 int stepsize = (int)luaL_optinteger(L, 4, 0);
232 return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize));
233 }
193 default: { 234 default: {
235 int res = lua_gc(L, o);
194 lua_pushinteger(L, res); 236 lua_pushinteger(L, res);
195 return 1; 237 return 1;
196 } 238 }
@@ -206,23 +248,6 @@ static int luaB_type (lua_State *L) {
206} 248}
207 249
208 250
209static int pairsmeta (lua_State *L, const char *method, int iszero,
210 lua_CFunction iter) {
211 luaL_checkany(L, 1);
212 if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */
213 lua_pushcfunction(L, iter); /* will return generator, */
214 lua_pushvalue(L, 1); /* state, */
215 if (iszero) lua_pushinteger(L, 0); /* and initial value */
216 else lua_pushnil(L);
217 }
218 else {
219 lua_pushvalue(L, 1); /* argument 'self' to metamethod */
220 lua_call(L, 1, 3); /* get 3 values from metamethod */
221 }
222 return 3;
223}
224
225
226static int luaB_next (lua_State *L) { 251static int luaB_next (lua_State *L) {
227 luaL_checktype(L, 1, LUA_TTABLE); 252 luaL_checktype(L, 1, LUA_TTABLE);
228 lua_settop(L, 2); /* create a 2nd argument if there isn't one */ 253 lua_settop(L, 2); /* create a 2nd argument if there isn't one */
@@ -236,7 +261,17 @@ static int luaB_next (lua_State *L) {
236 261
237 262
238static int luaB_pairs (lua_State *L) { 263static int luaB_pairs (lua_State *L) {
239 return pairsmeta(L, "__pairs", 0, luaB_next); 264 luaL_checkany(L, 1);
265 if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
266 lua_pushcfunction(L, luaB_next); /* will return generator, */
267 lua_pushvalue(L, 1); /* state, */
268 lua_pushnil(L); /* and initial value */
269 }
270 else {
271 lua_pushvalue(L, 1); /* argument 'self' to metamethod */
272 lua_call(L, 1, 3); /* get 3 values from metamethod */
273 }
274 return 3;
240} 275}
241 276
242 277
@@ -255,15 +290,11 @@ static int ipairsaux (lua_State *L) {
255** (The given "table" may not be a table.) 290** (The given "table" may not be a table.)
256*/ 291*/
257static int luaB_ipairs (lua_State *L) { 292static int luaB_ipairs (lua_State *L) {
258#if defined(LUA_COMPAT_IPAIRS)
259 return pairsmeta(L, "__ipairs", 1, ipairsaux);
260#else
261 luaL_checkany(L, 1); 293 luaL_checkany(L, 1);
262 lua_pushcfunction(L, ipairsaux); /* iteration function */ 294 lua_pushcfunction(L, ipairsaux); /* iteration function */
263 lua_pushvalue(L, 1); /* state */ 295 lua_pushvalue(L, 1); /* state */
264 lua_pushinteger(L, 0); /* initial value */ 296 lua_pushinteger(L, 0); /* initial value */
265 return 3; 297 return 3;
266#endif
267} 298}
268 299
269 300
@@ -277,9 +308,9 @@ static int load_aux (lua_State *L, int status, int envidx) {
277 return 1; 308 return 1;
278 } 309 }
279 else { /* error (message is on top of the stack) */ 310 else { /* error (message is on top of the stack) */
280 lua_pushnil(L); 311 luaL_pushfail(L);
281 lua_insert(L, -2); /* put before error message */ 312 lua_insert(L, -2); /* put before error message */
282 return 2; /* return nil plus error message */ 313 return 2; /* return fail plus error message */
283 } 314 }
284} 315}
285 316
@@ -459,13 +490,11 @@ static const luaL_Reg base_funcs[] = {
459 {"ipairs", luaB_ipairs}, 490 {"ipairs", luaB_ipairs},
460 {"loadfile", luaB_loadfile}, 491 {"loadfile", luaB_loadfile},
461 {"load", luaB_load}, 492 {"load", luaB_load},
462#if defined(LUA_COMPAT_LOADSTRING)
463 {"loadstring", luaB_load},
464#endif
465 {"next", luaB_next}, 493 {"next", luaB_next},
466 {"pairs", luaB_pairs}, 494 {"pairs", luaB_pairs},
467 {"pcall", luaB_pcall}, 495 {"pcall", luaB_pcall},
468 {"print", luaB_print}, 496 {"print", luaB_print},
497 {"warn", luaB_warn},
469 {"rawequal", luaB_rawequal}, 498 {"rawequal", luaB_rawequal},
470 {"rawlen", luaB_rawlen}, 499 {"rawlen", luaB_rawlen},
471 {"rawget", luaB_rawget}, 500 {"rawget", luaB_rawget},
@@ -477,7 +506,7 @@ static const luaL_Reg base_funcs[] = {
477 {"type", luaB_type}, 506 {"type", luaB_type},
478 {"xpcall", luaB_xpcall}, 507 {"xpcall", luaB_xpcall},
479 /* placeholders */ 508 /* placeholders */
480 {"_G", NULL}, 509 {LUA_GNAME, NULL},
481 {"_VERSION", NULL}, 510 {"_VERSION", NULL},
482 {NULL, NULL} 511 {NULL, NULL}
483}; 512};
@@ -489,7 +518,7 @@ LUAMOD_API int luaopen_base (lua_State *L) {
489 luaL_setfuncs(L, base_funcs, 0); 518 luaL_setfuncs(L, base_funcs, 0);
490 /* set global _G */ 519 /* set global _G */
491 lua_pushvalue(L, -1); 520 lua_pushvalue(L, -1);
492 lua_setfield(L, -2, "_G"); 521 lua_setfield(L, -2, LUA_GNAME);
493 /* set global _VERSION */ 522 /* set global _VERSION */
494 lua_pushliteral(L, LUA_VERSION); 523 lua_pushliteral(L, LUA_VERSION);
495 lua_setfield(L, -2, "_VERSION"); 524 lua_setfield(L, -2, "_VERSION");