diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-08-21 14:43:44 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-08-21 14:43:44 -0300 |
commit | cc117253c83ec32d226a8f2226d5ca144804f873 (patch) | |
tree | 46551b01d3febf98d65d55276d5831a75e2e0062 | |
parent | 8e226e6a09390a80fde98f192833fc85a5537376 (diff) | |
download | lua-cc117253c83ec32d226a8f2226d5ca144804f873.tar.gz lua-cc117253c83ec32d226a8f2226d5ca144804f873.tar.bz2 lua-cc117253c83ec32d226a8f2226d5ca144804f873.zip |
new implementation for error handling: on error, function _ERRORMESSAGE
is called, which in turn calls _ALERT to write a message to stderr.
-rw-r--r-- | lapi.c | 17 | ||||
-rw-r--r-- | lbuiltin.c | 19 | ||||
-rw-r--r-- | ldo.c | 33 | ||||
-rw-r--r-- | liolib.c | 87 | ||||
-rw-r--r-- | lstate.h | 3 | ||||
-rw-r--r-- | ltm.c | 4 | ||||
-rw-r--r-- | lua.h | 7 | ||||
-rw-r--r-- | manual.tex | 99 |
8 files changed, 133 insertions, 136 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.25 1998/06/05 22:17:44 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.26 1998/07/12 16:16:02 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -29,9 +29,8 @@ char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" | |||
29 | 29 | ||
30 | 30 | ||
31 | 31 | ||
32 | TObject *luaA_Address (lua_Object o) | 32 | TObject *luaA_Address (lua_Object o) { |
33 | { | 33 | return (o != LUA_NOOBJECT) ? Address(o) : NULL; |
34 | return Address(o); | ||
35 | } | 34 | } |
36 | 35 | ||
37 | 36 | ||
@@ -150,12 +149,12 @@ lua_Object lua_settagmethod (int tag, char *event) | |||
150 | } | 149 | } |
151 | 150 | ||
152 | 151 | ||
153 | lua_Object lua_seterrormethod (void) | 152 | lua_Object lua_seterrormethod (void) { |
154 | { | 153 | lua_Object temp; |
155 | TObject temp = L->errorim; | ||
156 | checkCparams(1); | 154 | checkCparams(1); |
157 | L->errorim = *(--L->stack.top); | 155 | temp = lua_getglobal("_ERRORMESSAGE"); |
158 | return put_luaObject(&temp); | 156 | lua_setglobal("_ERRORMESSAGE"); |
157 | return temp; | ||
159 | } | 158 | } |
160 | 159 | ||
161 | 160 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.32 1998/06/29 18:24:06 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.33 1998/07/12 16:16:43 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 | */ |
@@ -192,6 +192,19 @@ static void luaI_print (void) { | |||
192 | } | 192 | } |
193 | 193 | ||
194 | 194 | ||
195 | static void luaB_message (void) { | ||
196 | fputs(luaL_check_string(1), stderr); | ||
197 | } | ||
198 | |||
199 | |||
200 | static void error_message (void) { | ||
201 | char buff[200]; | ||
202 | sprintf(buff, "lua error: %.180s\n", luaL_check_string(1)); | ||
203 | lua_pushstring(buff); | ||
204 | lua_call("_ALERT"); | ||
205 | } | ||
206 | |||
207 | |||
195 | static void luaI_type (void) | 208 | static void luaI_type (void) |
196 | { | 209 | { |
197 | lua_Object o = luaL_nonnullarg(1); | 210 | lua_Object o = luaL_nonnullarg(1); |
@@ -568,6 +581,7 @@ static struct luaL_reg int_funcs[] = { | |||
568 | {"copytagmethods", copytagmethods}, | 581 | {"copytagmethods", copytagmethods}, |
569 | {"dostring", internaldostring}, | 582 | {"dostring", internaldostring}, |
570 | {"error", luaI_error}, | 583 | {"error", luaI_error}, |
584 | {"_ERRORMESSAGE", error_message}, | ||
571 | {"foreach", foreach}, | 585 | {"foreach", foreach}, |
572 | {"foreachvar", foreachvar}, | 586 | {"foreachvar", foreachvar}, |
573 | {"getglobal", getglobal}, | 587 | {"getglobal", getglobal}, |
@@ -588,7 +602,8 @@ static struct luaL_reg int_funcs[] = { | |||
588 | {"tonumber", luaB_tonumber}, | 602 | {"tonumber", luaB_tonumber}, |
589 | {"tostring", to_string}, | 603 | {"tostring", to_string}, |
590 | {"tag", luatag}, | 604 | {"tag", luatag}, |
591 | {"type", luaI_type} | 605 | {"type", luaI_type}, |
606 | {"_ALERT", luaB_message} | ||
592 | }; | 607 | }; |
593 | 608 | ||
594 | 609 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.27 1998/06/19 18:47:06 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.28 1998/07/12 16:14:34 roberto Exp roberto $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -17,6 +17,7 @@ | |||
17 | #include "lobject.h" | 17 | #include "lobject.h" |
18 | #include "lparser.h" | 18 | #include "lparser.h" |
19 | #include "lstate.h" | 19 | #include "lstate.h" |
20 | #include "lstring.h" | ||
20 | #include "ltm.h" | 21 | #include "ltm.h" |
21 | #include "lua.h" | 22 | #include "lua.h" |
22 | #include "luadebug.h" | 23 | #include "luadebug.h" |
@@ -32,27 +33,13 @@ | |||
32 | 33 | ||
33 | 34 | ||
34 | 35 | ||
35 | /* | ||
36 | ** Error messages | ||
37 | */ | ||
38 | |||
39 | static void stderrorim (void) | ||
40 | { | ||
41 | fprintf(stderr, "lua error: %s\n", lua_getstring(lua_getparam(1))); | ||
42 | } | ||
43 | |||
44 | |||
45 | |||
46 | #define STACK_UNIT 128 | 36 | #define STACK_UNIT 128 |
47 | 37 | ||
48 | 38 | ||
49 | void luaD_init (void) | 39 | void luaD_init (void) { |
50 | { | ||
51 | L->stack.stack = luaM_newvector(STACK_UNIT, TObject); | 40 | L->stack.stack = luaM_newvector(STACK_UNIT, TObject); |
52 | L->stack.top = L->stack.stack; | 41 | L->stack.top = L->stack.stack; |
53 | L->stack.last = L->stack.stack+(STACK_UNIT-1); | 42 | L->stack.last = L->stack.stack+(STACK_UNIT-1); |
54 | ttype(&L->errorim) = LUA_T_CPROTO; | ||
55 | fvalue(&L->errorim) = stderrorim; | ||
56 | } | 43 | } |
57 | 44 | ||
58 | 45 | ||
@@ -246,12 +233,13 @@ void luaD_travstack (int (*fn)(TObject *)) | |||
246 | 233 | ||
247 | 234 | ||
248 | 235 | ||
249 | static void message (char *s) | 236 | static void message (char *s) { |
250 | { | 237 | TObject *em = &(luaS_new("_ERRORMESSAGE")->u.s.globalval); |
251 | TObject im = L->errorim; | 238 | if (ttype(em) != LUA_T_NIL) { |
252 | if (ttype(&im) != LUA_T_NIL) { | 239 | *L->stack.top = *em; |
240 | incr_top; | ||
253 | lua_pushstring(s); | 241 | lua_pushstring(s); |
254 | luaD_callTM(&im, 1, 0); | 242 | luaD_calln(1, 0); |
255 | } | 243 | } |
256 | } | 244 | } |
257 | 245 | ||
@@ -264,7 +252,8 @@ void lua_error (char *s) | |||
264 | if (L->errorJmp) | 252 | if (L->errorJmp) |
265 | longjmp(*((jmp_buf *)L->errorJmp), 1); | 253 | longjmp(*((jmp_buf *)L->errorJmp), 1); |
266 | else { | 254 | else { |
267 | fprintf (stderr, "lua: exit(1). Unable to recover\n"); | 255 | lua_pushstring("lua: exit(1). Unable to recover.\n"); |
256 | lua_call("_ALERT"); | ||
268 | exit(1); | 257 | exit(1); |
269 | } | 258 | } |
270 | } | 259 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.20 1998/06/05 22:17:44 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.21 1998/06/18 17:04:28 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -353,71 +353,75 @@ static void io_debug (void) | |||
353 | } | 353 | } |
354 | 354 | ||
355 | 355 | ||
356 | static void lua_printstack (FILE *f) | 356 | #define MESSAGESIZE 150 |
357 | { | 357 | #define MAXMESSAGE (MESSAGESIZE*10) |
358 | |||
359 | static void errorfb (void) { | ||
360 | char buff[MAXMESSAGE]; | ||
358 | int level = 1; /* skip level 0 (it's this function) */ | 361 | int level = 1; /* skip level 0 (it's this function) */ |
359 | lua_Object func; | 362 | lua_Object func; |
363 | sprintf(buff, "lua: %.200s\n", lua_getstring(lua_getparam(1))); | ||
360 | while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) { | 364 | while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) { |
361 | char *name; | 365 | char *name; |
362 | int currentline; | 366 | int currentline; |
363 | char *filename; | 367 | char *chunkname; |
364 | int linedefined; | 368 | int linedefined; |
365 | lua_funcinfo(func, &filename, &linedefined); | 369 | lua_funcinfo(func, &chunkname, &linedefined); |
366 | fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t"); | 370 | strcat(buff, (level==2) ? "Active Stack:\n\t" : "\t"); |
371 | if (strlen(buff) > MAXMESSAGE-MESSAGESIZE) { | ||
372 | strcat(buff, "...\n"); | ||
373 | break; /* buffer is full */ | ||
374 | } | ||
367 | switch (*lua_getobjname(func, &name)) { | 375 | switch (*lua_getobjname(func, &name)) { |
368 | case 'g': | 376 | case 'g': |
369 | fprintf(f, "function %s", name); | 377 | sprintf(buff+strlen(buff), "function %.50s", name); |
370 | break; | 378 | break; |
371 | case 't': | 379 | case 't': |
372 | fprintf(f, "`%s' tag method", name); | 380 | sprintf(buff+strlen(buff), "`%.50s' tag method", name); |
373 | break; | 381 | break; |
374 | default: { | 382 | default: { |
375 | if (linedefined == 0) | 383 | if (linedefined == 0) |
376 | fprintf(f, "main of %s", filename); | 384 | sprintf(buff+strlen(buff), "main of %.50s", chunkname); |
377 | else if (linedefined < 0) | 385 | else if (linedefined < 0) |
378 | fprintf(f, "%s", filename); | 386 | sprintf(buff+strlen(buff), "%.50s", chunkname); |
379 | else | 387 | else |
380 | fprintf(f, "function (%s:%d)", filename, linedefined); | 388 | sprintf(buff+strlen(buff), "function (%.50s:%d)", |
381 | filename = NULL; | 389 | chunkname, linedefined); |
390 | chunkname = NULL; | ||
382 | } | 391 | } |
383 | } | 392 | } |
384 | if ((currentline = lua_currentline(func)) > 0) | 393 | if ((currentline = lua_currentline(func)) > 0) |
385 | fprintf(f, " at line %d", currentline); | 394 | sprintf(buff+strlen(buff), " at line %d", currentline); |
386 | if (filename) | 395 | if (chunkname) |
387 | fprintf(f, " [in file %s]", filename); | 396 | sprintf(buff+strlen(buff), " [in chunk %.50s]", chunkname); |
388 | fprintf(f, "\n"); | 397 | strcat(buff, "\n"); |
389 | } | 398 | } |
390 | } | 399 | lua_pushstring(buff); |
391 | 400 | lua_call("_ALERT"); | |
392 | |||
393 | static void errorfb (void) | ||
394 | { | ||
395 | fprintf(stderr, "lua: %s\n", lua_getstring(lua_getparam(1))); | ||
396 | lua_printstack(stderr); | ||
397 | } | 401 | } |
398 | 402 | ||
399 | 403 | ||
400 | 404 | ||
401 | static struct luaL_reg iolib[] = { | 405 | static struct luaL_reg iolib[] = { |
402 | {"setlocale", setloc}, | 406 | {"setlocale", setloc}, |
403 | {"execute", io_execute}, | 407 | {"execute", io_execute}, |
404 | {"remove", io_remove}, | 408 | {"remove", io_remove}, |
405 | {"rename", io_rename}, | 409 | {"rename", io_rename}, |
406 | {"tmpname", io_tmpname}, | 410 | {"tmpname", io_tmpname}, |
407 | {"getenv", io_getenv}, | 411 | {"getenv", io_getenv}, |
408 | {"date", io_date}, | 412 | {"date", io_date}, |
409 | {"clock", io_clock}, | 413 | {"clock", io_clock}, |
410 | {"exit", io_exit}, | 414 | {"exit", io_exit}, |
411 | {"debug", io_debug}, | 415 | {"debug", io_debug}, |
412 | {"print_stack", errorfb} | 416 | {"_ERRORMESSAGE", errorfb} |
413 | }; | 417 | }; |
414 | 418 | ||
415 | static struct luaL_reg iolibtag[] = { | 419 | static struct luaL_reg iolibtag[] = { |
416 | {"readfrom", io_readfrom}, | 420 | {"readfrom", io_readfrom}, |
417 | {"writeto", io_writeto}, | 421 | {"writeto", io_writeto}, |
418 | {"appendto", io_appendto}, | 422 | {"appendto", io_appendto}, |
419 | {"read", io_read}, | 423 | {"read", io_read}, |
420 | {"write", io_write} | 424 | {"write", io_write} |
421 | }; | 425 | }; |
422 | 426 | ||
423 | static void openwithtags (void) | 427 | static void openwithtags (void) |
@@ -439,10 +443,7 @@ static void openwithtags (void) | |||
439 | setfile(stderr, "_STDERR", iotag); | 443 | setfile(stderr, "_STDERR", iotag); |
440 | } | 444 | } |
441 | 445 | ||
442 | void lua_iolibopen (void) | 446 | void lua_iolibopen (void) { |
443 | { | ||
444 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); | 447 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); |
445 | openwithtags(); | 448 | openwithtags(); |
446 | lua_pushcfunction(errorfb); | ||
447 | lua_seterrormethod(); | ||
448 | } | 449 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.h,v 1.10 1998/06/19 16:14:09 roberto Exp roberto $ | 2 | ** $Id: lstate.h,v 1.11 1998/06/24 13:33:00 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -61,7 +61,6 @@ struct lua_State { | |||
61 | struct C_Lua_Stack Cblocks[MAX_C_BLOCKS]; | 61 | struct C_Lua_Stack Cblocks[MAX_C_BLOCKS]; |
62 | int numCblocks; /* number of nested Cblocks */ | 62 | int numCblocks; /* number of nested Cblocks */ |
63 | /* global state */ | 63 | /* global state */ |
64 | TObject errorim; /* error tag method */ | ||
65 | GCnode rootproto; /* list of all prototypes */ | 64 | GCnode rootproto; /* list of all prototypes */ |
66 | GCnode rootcl; /* list of all closures */ | 65 | GCnode rootcl; /* list of all closures */ |
67 | GCnode roottable; /* list of all tables */ | 66 | GCnode roottable; /* list of all tables */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltm.c,v 1.15 1998/03/11 13:59:50 roberto Exp roberto $ | 2 | ** $Id: ltm.c,v 1.16 1998/06/18 16:57:03 roberto Exp roberto $ |
3 | ** Tag methods | 3 | ** Tag methods |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -158,8 +158,6 @@ void luaT_settagmethod (int t, char *event, TObject *func) | |||
158 | char *luaT_travtagmethods (int (*fn)(TObject *)) | 158 | char *luaT_travtagmethods (int (*fn)(TObject *)) |
159 | { | 159 | { |
160 | int e; | 160 | int e; |
161 | if (fn(&L->errorim)) | ||
162 | return "error"; | ||
163 | for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */ | 161 | for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */ |
164 | int t; | 162 | int t; |
165 | for (t=0; t>=L->last_tag; t--) | 163 | for (t=0; t>=L->last_tag; t--) |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.h,v 1.22 1998/06/15 21:34:14 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.23 1998/06/18 16:51:53 roberto Exp roberto $ |
3 | ** Lua - An Extensible Extension Language | 3 | ** Lua - An Extensible Extension Language |
4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil | 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil |
5 | ** e-mail: lua@tecgraf.puc-rio.br | 5 | ** e-mail: lua@tecgraf.puc-rio.br |
@@ -11,7 +11,7 @@ | |||
11 | #ifndef lua_h | 11 | #ifndef lua_h |
12 | #define lua_h | 12 | #define lua_h |
13 | 13 | ||
14 | #define LUA_VERSION "Lua 3.1" | 14 | #define LUA_VERSION "Lua 3.2 (alpha)" |
15 | #define LUA_COPYRIGHT "Copyright (C) 1994-1998 TeCGraf, PUC-Rio" | 15 | #define LUA_COPYRIGHT "Copyright (C) 1994-1998 TeCGraf, PUC-Rio" |
16 | #define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo" | 16 | #define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo" |
17 | 17 | ||
@@ -32,7 +32,6 @@ lua_State *lua_setstate (lua_State *st); | |||
32 | 32 | ||
33 | lua_Object lua_settagmethod (int tag, char *event); /* In: new method */ | 33 | lua_Object lua_settagmethod (int tag, char *event); /* In: new method */ |
34 | lua_Object lua_gettagmethod (int tag, char *event); | 34 | lua_Object lua_gettagmethod (int tag, char *event); |
35 | lua_Object lua_seterrormethod (void); /* In: new method */ | ||
36 | 35 | ||
37 | int lua_newtag (void); | 36 | int lua_newtag (void); |
38 | int lua_copytagmethods (int tagto, int tagfrom); | 37 | int lua_copytagmethods (int tagto, int tagfrom); |
@@ -125,6 +124,8 @@ int (lua_clonetag) (int t); | |||
125 | #define lua_clonetag(t) lua_copytagmethods(lua_newtag(), (t)) | 124 | #define lua_clonetag(t) lua_copytagmethods(lua_newtag(), (t)) |
126 | 125 | ||
127 | 126 | ||
127 | lua_Object lua_seterrormethod (void); /* In: new method */ | ||
128 | |||
128 | /* ========================================================================== | 129 | /* ========================================================================== |
129 | ** for compatibility with old versions. Avoid using these macros/functions | 130 | ** for compatibility with old versions. Avoid using these macros/functions |
130 | ** If your program does need any of these, define LUA_COMPAT2_5 | 131 | ** If your program does need any of these, define LUA_COMPAT2_5 |
@@ -1,8 +1,10 @@ | |||
1 | % $Id: manual.tex,v 1.16 1998/06/19 18:47:06 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.17 1998/06/29 18:09:28 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentclass[11pt]{article} | 3 | \documentclass[11pt]{article} |
4 | \usepackage{fullpage,bnf} | 4 | \usepackage{fullpage,bnf} |
5 | 5 | ||
6 | \catcode`\_=12 | ||
7 | |||
6 | \newcommand{\See}[1]{Section~\ref{#1}} | 8 | \newcommand{\See}[1]{Section~\ref{#1}} |
7 | \newcommand{\see}[1]{(see \See{#1})} | 9 | \newcommand{\see}[1]{(see \See{#1})} |
8 | \newcommand{\M}[1]{\emph{#1}} | 10 | \newcommand{\M}[1]{\emph{#1}} |
@@ -19,7 +21,7 @@ | |||
19 | 21 | ||
20 | \newcommand{\ff}{$\bullet$\ } | 22 | \newcommand{\ff}{$\bullet$\ } |
21 | 23 | ||
22 | \newcommand{\Version}{3.1} | 24 | \newcommand{\Version}{3.2 (alpha)} |
23 | 25 | ||
24 | \makeindex | 26 | \makeindex |
25 | 27 | ||
@@ -39,7 +41,7 @@ Waldemar Celes | |||
39 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 41 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
40 | } | 42 | } |
41 | 43 | ||
42 | %\date{\small \verb$Date: 1998/06/19 18:47:06 $} | 44 | %\date{\small \verb$Date: 1998/06/29 18:09:28 $} |
43 | 45 | ||
44 | \maketitle | 46 | \maketitle |
45 | 47 | ||
@@ -810,7 +812,7 @@ If the function is called in a place that can hold many values | |||
810 | (syntactically denoted by the non-terminal \M{exp}), | 812 | (syntactically denoted by the non-terminal \M{exp}), |
811 | then no adjustment is made. | 813 | then no adjustment is made. |
812 | Note that the only place that can hold many values | 814 | Note that the only place that can hold many values |
813 | is the last expression (or the only one) in an assignment | 815 | is the last (or the only) expression in an assignment |
814 | or in a return statement; see examples below. | 816 | or in a return statement; see examples below. |
815 | \begin{verbatim} | 817 | \begin{verbatim} |
816 | f(); -- adjusted to 0 | 818 | f(); -- adjusted to 0 |
@@ -1263,22 +1265,20 @@ Because Lua is an extension language, | |||
1263 | all Lua actions start from C code in the host program | 1265 | all Lua actions start from C code in the host program |
1264 | calling a function from the Lua library. | 1266 | calling a function from the Lua library. |
1265 | Whenever an error occurs during Lua compilation or execution, | 1267 | Whenever an error occurs during Lua compilation or execution, |
1266 | the \Def{error method} is called, | 1268 | function \verb|_ERRORMESSAGE| is called \Deffunc{_ERRORMESSAGE} |
1269 | (provided it is different from \nil), | ||
1267 | and then the corresponding function from the library | 1270 | and then the corresponding function from the library |
1268 | (\verb|lua_dofile|, \verb|lua_dostring|, | 1271 | (\verb|lua_dofile|, \verb|lua_dostring|, |
1269 | \verb|lua_dobuffer|, or \verb|lua_callfunction|) | 1272 | \verb|lua_dobuffer|, or \verb|lua_callfunction|) |
1270 | is terminated, returning an error condition. | 1273 | is terminated, returning an error condition. |
1271 | 1274 | ||
1272 | The only argument to the error method is a string | 1275 | The only argument to \verb|_ERRORMESSAGE| is a string |
1273 | describing the error. | 1276 | describing the error. |
1274 | The default method prints this message to \verb|stderr|. | 1277 | The default definition for this function calls \verb|_ALERT|, |
1275 | If needed, it is possible to change the error method with the | 1278 | which prints the message to \verb|stderr| \see{alert}. |
1276 | function \verb|seterrormethod|, | 1279 | The standard I/O library redefines \verb|_ERRORMESSAGE|, |
1277 | which gets the new error handler as its only parameter | 1280 | and uses the debug facilities \see{debugI} |
1278 | \see{pdf-seterrormethod}. | 1281 | to print some extra information, |
1279 | The standard I/O library uses this facility to redefine the error method, | ||
1280 | using the debug facilities \see{debugI}, | ||
1281 | in order to print some extra information, | ||
1282 | such as the call stack. | 1282 | such as the call stack. |
1283 | 1283 | ||
1284 | To provide more information about errors, | 1284 | To provide more information about errors, |
@@ -1347,11 +1347,11 @@ For that, you must set \verb|lua_state| back to \verb|NULL| before | |||
1347 | calling \verb|lua_open|. | 1347 | calling \verb|lua_open|. |
1348 | An easy way to do that is defining an auxiliary function: | 1348 | An easy way to do that is defining an auxiliary function: |
1349 | \begin{verbatim} | 1349 | \begin{verbatim} |
1350 | lua_State *lua_newstate (void) { | 1350 | lua_State *lua_newstate (void) { |
1351 | lua_State *old = lua_setstate(NULL); | 1351 | lua_State *old = lua_setstate(NULL); |
1352 | lua_open(); | 1352 | lua_open(); |
1353 | return lua_setstate(old); | 1353 | return lua_setstate(old); |
1354 | } | 1354 | } |
1355 | \end{verbatim} | 1355 | \end{verbatim} |
1356 | This function creates a new state without changing the current state | 1356 | This function creates a new state without changing the current state |
1357 | of the interpreter. | 1357 | of the interpreter. |
@@ -1373,14 +1373,14 @@ If \verb|lua_state| is already \verb|NULL|, | |||
1373 | \verb|lua_close| has no effect. | 1373 | \verb|lua_close| has no effect. |
1374 | 1374 | ||
1375 | If you are using multiple states, | 1375 | If you are using multiple states, |
1376 | you may find useful the following function, | 1376 | you may find useful to define the following function, |
1377 | which releases a given state: | 1377 | which releases a given state: |
1378 | \begin{verbatim} | 1378 | \begin{verbatim} |
1379 | void lua_freestate (lua_State *st) { | 1379 | void lua_freestate (lua_State *st) { |
1380 | lua_State *old = lua_setstate(st); | 1380 | lua_State *old = lua_setstate(st); |
1381 | lua_close(); | 1381 | lua_close(); |
1382 | if (old != st) lua_setstate(old); | 1382 | if (old != st) lua_setstate(old); |
1383 | } | 1383 | } |
1384 | \end{verbatim} | 1384 | \end{verbatim} |
1385 | 1385 | ||
1386 | \subsection{Exchanging Values between C and Lua} \label{valuesCLua} | 1386 | \subsection{Exchanging Values between C and Lua} \label{valuesCLua} |
@@ -1736,18 +1736,10 @@ If the C function has been called from Lua, | |||
1736 | then the corresponding Lua execution terminates, | 1736 | then the corresponding Lua execution terminates, |
1737 | as if an error had occurred inside Lua code. | 1737 | as if an error had occurred inside Lua code. |
1738 | Otherwise, the whole program terminates with a call to \verb|exit(1)|. | 1738 | Otherwise, the whole program terminates with a call to \verb|exit(1)|. |
1739 | The \verb|message| is passed to the error handler method. | 1739 | The \verb|message| is passed to the error handler function, |
1740 | \verb|_ERRORMESSAGE|. | ||
1740 | If \verb|message| is \verb|NULL|, | 1741 | If \verb|message| is \verb|NULL|, |
1741 | the error handler method is not called. | 1742 | \verb|_ERRORMESSAGE| is not called. |
1742 | |||
1743 | The error handler method \see{error} can be | ||
1744 | changed with: \Deffunc{lua_seterrormethod} | ||
1745 | \begin{verbatim} | ||
1746 | lua_Object lua_seterrormethod (void); | ||
1747 | \end{verbatim} | ||
1748 | This function sets the object at the top of C2lua | ||
1749 | as the new error method, | ||
1750 | and returns the old error method value. | ||
1751 | 1743 | ||
1752 | Tag methods can be changed with: \Deffunc{lua_settagmethod} | 1744 | Tag methods can be changed with: \Deffunc{lua_settagmethod} |
1753 | \begin{verbatim} | 1745 | \begin{verbatim} |
@@ -1885,7 +1877,7 @@ and \verb|lua_iolibopen|, declared in \verb|lualib.h|. | |||
1885 | 1877 | ||
1886 | \subsection{Predefined Functions} \label{predefined} | 1878 | \subsection{Predefined Functions} \label{predefined} |
1887 | 1879 | ||
1888 | \subsubsection*{\ff \T{call (func, arg [, mode [, errmethod]])}}\Deffunc{call} | 1880 | \subsubsection*{\ff \T{call (func, arg [, mode [, errhandler]])}}\Deffunc{call} |
1889 | \label{pdf-call} | 1881 | \label{pdf-call} |
1890 | This function calls function \verb|func| with | 1882 | This function calls function \verb|func| with |
1891 | the arguments given by the table \verb|arg|. | 1883 | the arguments given by the table \verb|arg|. |
@@ -1917,14 +1909,15 @@ if an error occurs during the function call, | |||
1917 | the error is propagated. | 1909 | the error is propagated. |
1918 | If the string \verb|mode| contains \verb|"x"|, | 1910 | If the string \verb|mode| contains \verb|"x"|, |
1919 | then the call is \emph{protected}.\index{protected calls} | 1911 | then the call is \emph{protected}.\index{protected calls} |
1920 | In this mode, function \verb|call| does not generate an error, | 1912 | In this mode, function \verb|call| does not propagate an error, |
1921 | whatever happens during the call. | 1913 | whatever happens during the call. |
1922 | Instead, it returns \nil\ to signal the error | 1914 | Instead, it returns \nil\ to signal the error |
1923 | (besides calling the appropriated error method). | 1915 | (besides calling the appropriated error handler). |
1924 | 1916 | ||
1925 | If provided, \verb|errmethod| is temporarily set as the error method, | 1917 | If provided, |
1926 | while \verb|func| runs. | 1918 | \verb|errhandler| is temporarily set as the error function |
1927 | As a particular case, if \verb|errmethod| is \nil, | 1919 | \verb|_ERRORMESSAGE|, while \verb|func| runs. |
1920 | As a particular example, if \verb|errhandler| is \nil, | ||
1928 | no error messages will be issued during the execution of the called function. | 1921 | no error messages will be issued during the execution of the called function. |
1929 | 1922 | ||
1930 | \subsubsection*{\ff \T{collectgarbage ([limit])}}\Deffunc{collectgarbage} | 1923 | \subsubsection*{\ff \T{collectgarbage ([limit])}}\Deffunc{collectgarbage} |
@@ -2055,9 +2048,16 @@ This function receives any number of arguments, | |||
2055 | and prints their values using the strings returned by \verb|tostring|. | 2048 | and prints their values using the strings returned by \verb|tostring|. |
2056 | This function is not intended for formatted output, | 2049 | This function is not intended for formatted output, |
2057 | but only as a quick way to show a value, | 2050 | but only as a quick way to show a value, |
2058 | for instance for error messages or debugging. | 2051 | for instance for debugging. |
2059 | See \See{libio} for functions for formatted output. | 2052 | See \See{libio} for functions for formatted output. |
2060 | 2053 | ||
2054 | \subsubsection*{\ff \T{_ALERT (message)}}\Deffunc{alert}\label{alert} | ||
2055 | This function prints its only string argument to \IndexVerb{stderr}. | ||
2056 | All error messages in Lua are printed through this function. | ||
2057 | Therefore, a program may redefine it | ||
2058 | to change the way such messages are shown | ||
2059 | (for instance, for systems without \verb|stderr|). | ||
2060 | |||
2061 | \subsubsection*{\ff \T{tonumber (e [, base])}}\Deffunc{tonumber} | 2061 | \subsubsection*{\ff \T{tonumber (e [, base])}}\Deffunc{tonumber} |
2062 | This function receives one argument, | 2062 | This function receives one argument, |
2063 | and tries to convert it to a number. | 2063 | and tries to convert it to a number. |
@@ -2164,13 +2164,6 @@ Its full semantics is explained in \See{tag-method}. | |||
2164 | The string \verb|name| does not need to be a | 2164 | The string \verb|name| does not need to be a |
2165 | syntactically valid variable name. | 2165 | syntactically valid variable name. |
2166 | 2166 | ||
2167 | \subsubsection*{\ff \T{seterrormethod (newmethod)}} | ||
2168 | \label{pdf-seterrormethod} | ||
2169 | Sets the error handler \see{error}. | ||
2170 | \verb|newmethod| must be a function or \nil, | ||
2171 | in which case the error handler does nothing. | ||
2172 | Returns the old error handler. | ||
2173 | |||
2174 | \subsubsection*{\ff \T{settagmethod (tag, event, newmethod)}} | 2167 | \subsubsection*{\ff \T{settagmethod (tag, event, newmethod)}} |
2175 | \Deffunc{settagmethod} | 2168 | \Deffunc{settagmethod} |
2176 | This function sets a new tag method to the given pair \M{(tag, event)}. | 2169 | This function sets a new tag method to the given pair \M{(tag, event)}. |
@@ -2930,7 +2923,7 @@ so any existing program that opens at least one standard | |||
2930 | library before calling Lua does not need to be modified. | 2923 | library before calling Lua does not need to be modified. |
2931 | 2924 | ||
2932 | \item Function \verb|dostring| no longer accepts an optional second argument, | 2925 | \item Function \verb|dostring| no longer accepts an optional second argument, |
2933 | with a temporary error method. | 2926 | with a temporary error handler. |
2934 | This facility is now provided by function \verb|call|. | 2927 | This facility is now provided by function \verb|call|. |
2935 | 2928 | ||
2936 | \item Function \verb|gsub| no longer accepts an optional fourth argument | 2929 | \item Function \verb|gsub| no longer accepts an optional fourth argument |
@@ -2951,8 +2944,10 @@ programs should use an explicit assignment instead, such as | |||
2951 | 2944 | ||
2952 | \end{itemize} | 2945 | \end{itemize} |
2953 | 2946 | ||
2947 | % restore underscore to usual meaning | ||
2948 | \catcode`\_=8 | ||
2949 | |||
2954 | \newcommand{\indexentry}[2]{\item {#1} #2} | 2950 | \newcommand{\indexentry}[2]{\item {#1} #2} |
2955 | %\catcode`\_=12 | ||
2956 | \begin{theindex} | 2951 | \begin{theindex} |
2957 | \input{manual.id} | 2952 | \input{manual.id} |
2958 | \end{theindex} | 2953 | \end{theindex} |