aboutsummaryrefslogtreecommitdiff
path: root/lbuiltin.c
diff options
context:
space:
mode:
Diffstat (limited to 'lbuiltin.c')
-rw-r--r--lbuiltin.c53
1 files changed, 32 insertions, 21 deletions
diff --git a/lbuiltin.c b/lbuiltin.c
index 7e0c832c..3a57e814 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,10 +1,19 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.92 2000/01/19 16:50:30 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.93 2000/02/22 18:12:46 roberto Exp roberto $
3** Built-in functions 3** Built-in functions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8/*
9** =========================================================================
10** All built-in functions are public (i.e. not static) and are named luaB_f,
11** where f is the function name in Lua. So, if you do not need all these
12** functions, you may register manually only the ones that you need.
13** =========================================================================
14*/
15
16
8#include <ctype.h> 17#include <ctype.h>
9#include <stdio.h> 18#include <stdio.h>
10#include <stdlib.h> 19#include <stdlib.h>
@@ -91,7 +100,7 @@ static Hash *gettable (lua_State *L, int arg) {
91 100
92 101
93/* 102/*
94** If your system does not support "stderr", redefine this function, or 103** If your system does not support `stderr', redefine this function, or
95** redefine _ERRORMESSAGE so that it won't need _ALERT. 104** redefine _ERRORMESSAGE so that it won't need _ALERT.
96*/ 105*/
97void luaB__ALERT (lua_State *L) { 106void luaB__ALERT (lua_State *L) {
@@ -116,9 +125,9 @@ void luaB__ERRORMESSAGE (lua_State *L) {
116 125
117 126
118/* 127/*
119** If your system does not support "stdout", you can just remove this function. 128** If your system does not support `stdout', you can just remove this function.
120** If you need, you can define your own "print" function, following this 129** If you need, you can define your own `print' function, following this
121** model but changing "fputs" to put the strings at a proper place 130** model but changing `fputs' to put the strings at a proper place
122** (a console window or a log file, for instance). 131** (a console window or a log file, for instance).
123*/ 132*/
124#ifndef MAXPRINT 133#ifndef MAXPRINT
@@ -174,17 +183,17 @@ void luaB_error (lua_State *L) {
174} 183}
175 184
176void luaB_setglobal (lua_State *L) { 185void luaB_setglobal (lua_State *L) {
177 const char *n = luaL_check_string(L, 1); 186 const char *name = luaL_check_string(L, 1);
178 lua_Object value = luaL_nonnullarg(L, 2); 187 lua_Object value = luaL_nonnullarg(L, 2);
179 lua_pushobject(L, value); 188 lua_pushobject(L, value);
180 lua_setglobal(L, n); 189 lua_setglobal(L, name);
181} 190}
182 191
183void luaB_rawsetglobal (lua_State *L) { 192void luaB_rawsetglobal (lua_State *L) {
184 const char *n = luaL_check_string(L, 1); 193 const char *name = luaL_check_string(L, 1);
185 lua_Object value = luaL_nonnullarg(L, 2); 194 lua_Object value = luaL_nonnullarg(L, 2);
186 lua_pushobject(L, value); 195 lua_pushobject(L, value);
187 lua_rawsetglobal(L, n); 196 lua_rawsetglobal(L, name);
188} 197}
189 198
190void luaB_getglobal (lua_State *L) { 199void luaB_getglobal (lua_State *L) {
@@ -236,7 +245,7 @@ void luaB_settagmethod (lua_State *L) {
236 "function or nil expected"); 245 "function or nil expected");
237#ifndef LUA_COMPAT_GC 246#ifndef LUA_COMPAT_GC
238 if (strcmp(event, "gc") == 0 && tag != LUA_T_NIL) 247 if (strcmp(event, "gc") == 0 && tag != LUA_T_NIL)
239 lua_error(L, "cannot set this tag method from Lua"); 248 lua_error(L, "cannot set this `gc' tag method from Lua");
240#endif 249#endif
241 lua_pushobject(L, nf); 250 lua_pushobject(L, nf);
242 lua_pushobject(L, lua_settagmethod(L, tag, event)); 251 lua_pushobject(L, lua_settagmethod(L, tag, event));
@@ -325,7 +334,7 @@ void luaB_call (lua_State *L) {
325 return; /* return nil to signal the error */ 334 return; /* return nil to signal the error */
326 } 335 }
327 else 336 else
328 lua_error(L, NULL); 337 lua_error(L, NULL); /* propagate error without additional messages */
329 } 338 }
330 else { /* no errors */ 339 else { /* no errors */
331 if (strchr(options, 'p')) { /* pack results? */ 340 if (strchr(options, 'p')) { /* pack results? */
@@ -340,14 +349,14 @@ void luaB_call (lua_State *L) {
340 349
341void luaB_nextvar (lua_State *L) { 350void luaB_nextvar (lua_State *L) {
342 lua_Object o = luaL_nonnullarg(L, 1); 351 lua_Object o = luaL_nonnullarg(L, 1);
343 TaggedString *g; 352 TaggedString *name;
344 if (ttype(o) == LUA_T_NIL) 353 if (ttype(o) == LUA_T_NIL)
345 g = NULL; 354 name = NULL;
346 else { 355 else {
347 luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "variable name expected"); 356 luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "variable name expected");
348 g = tsvalue(o); 357 name = tsvalue(o);
349 } 358 }
350 if (!luaA_nextvar(L, g)) 359 if (!luaA_nextvar(L, name))
351 lua_pushnil(L); 360 lua_pushnil(L);
352} 361}
353 362
@@ -355,7 +364,7 @@ void luaB_nextvar (lua_State *L) {
355void luaB_next (lua_State *L) { 364void luaB_next (lua_State *L) {
356 const Hash *a = gettable(L, 1); 365 const Hash *a = gettable(L, 1);
357 lua_Object k = luaL_nonnullarg(L, 2); 366 lua_Object k = luaL_nonnullarg(L, 2);
358 int i; /* will get first element after `i' */ 367 int i; /* `luaA_next' gets first element after `i' */
359 if (ttype(k) == LUA_T_NIL) 368 if (ttype(k) == LUA_T_NIL)
360 i = 0; /* get first */ 369 i = 0; /* get first */
361 else { 370 else {
@@ -390,7 +399,8 @@ void luaB_tostring (lua_State *L) {
390 sprintf(buff, "function: %p", o->value.f); 399 sprintf(buff, "function: %p", o->value.f);
391 break; 400 break;
392 case LUA_T_USERDATA: 401 case LUA_T_USERDATA:
393 sprintf(buff, "userdata: %p", o->value.ts->u.d.value); 402 sprintf(buff, "userdata: %p(%d)", o->value.ts->u.d.value,
403 o->value.ts->u.d.tag);
394 break; 404 break;
395 case LUA_T_NIL: 405 case LUA_T_NIL:
396 lua_pushstring(L, "nil"); 406 lua_pushstring(L, "nil");
@@ -435,7 +445,7 @@ void luaB_foreachi (lua_State *L) {
435 luaD_call(L, L->top-3, 1); 445 luaD_call(L, L->top-3, 1);
436 if (ttype(L->top-1) != LUA_T_NIL) 446 if (ttype(L->top-1) != LUA_T_NIL)
437 return; 447 return;
438 L->top--; 448 L->top--; /* remove nil result */
439 } 449 }
440} 450}
441 451
@@ -499,7 +509,7 @@ void luaB_tinsert (lua_State *L) {
499 pos = n+1; 509 pos = n+1;
500 } 510 }
501 luaV_setn(L, a, n+1); /* a.n = n+1 */ 511 luaV_setn(L, a, n+1); /* a.n = n+1 */
502 for ( ;n>=pos; n--) 512 for (; n>=pos; n--)
503 luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */ 513 luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */
504 luaH_setint(L, a, pos, v); /* a[pos] = v */ 514 luaH_setint(L, a, pos, v); /* a[pos] = v */
505} 515}
@@ -521,6 +531,7 @@ void luaB_tremove (lua_State *L) {
521/* 531/*
522** {====================================================== 532** {======================================================
523** Quicksort 533** Quicksort
534** (based on `Algorithms in MODULA-3', Robert Sedgewick; Addison-Wesley, 1993.)
524*/ 535*/
525 536
526static void swap (lua_State *L, Hash *a, int i, int j) { 537static void swap (lua_State *L, Hash *a, int i, int j) {
@@ -602,7 +613,7 @@ void luaB_sort (lua_State *L) {
602 lua_Object func = lua_getparam(L, 2); 613 lua_Object func = lua_getparam(L, 2);
603 luaL_arg_check(L, func == LUA_NOOBJECT || lua_isfunction(L, func), 2, 614 luaL_arg_check(L, func == LUA_NOOBJECT || lua_isfunction(L, func), 2,
604 "function expected"); 615 "function expected");
605 luaD_checkstack(L, 4); /* for Pivot, f, a, b (sort_comp) */ 616 luaD_checkstack(L, 4); /* for pivot, f, a, b (sort_comp) */
606 auxsort(L, a, 1, n, func); 617 auxsort(L, a, 1, n, func);
607 lua_pushobject(L, t); 618 lua_pushobject(L, t);
608} 619}
@@ -640,7 +651,7 @@ static const struct luaL_reg builtin_funcs[] = {
640 {"tonumber", luaB_tonumber}, 651 {"tonumber", luaB_tonumber},
641 {"tostring", luaB_tostring}, 652 {"tostring", luaB_tostring},
642 {"type", luaB_type}, 653 {"type", luaB_type},
643 /* "Extra" functions */ 654/* "Extra" functions */
644 {"assert", luaB_assert}, 655 {"assert", luaB_assert},
645 {"foreach", luaB_foreach}, 656 {"foreach", luaB_foreach},
646 {"foreachi", luaB_foreachi}, 657 {"foreachi", luaB_foreachi},