aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-17 15:45:16 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-17 15:45:16 -0300
commit70160320b120a6afb629c8fd1ebb3005682bafb9 (patch)
tree576bc4b84c28265dee3076717ac5c3dc746f44c5
parentbfbf56f15a544633c841666e0e33bd66ef398312 (diff)
downloadlua-70160320b120a6afb629c8fd1ebb3005682bafb9.tar.gz
lua-70160320b120a6afb629c8fd1ebb3005682bafb9.tar.bz2
lua-70160320b120a6afb629c8fd1ebb3005682bafb9.zip
first version for Lua 3.0 (with tag methods, etc)
-rw-r--r--manual.tex1005
1 files changed, 694 insertions, 311 deletions
diff --git a/manual.tex b/manual.tex
index 532662f8..8c63f576 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,9 +1,10 @@
1% $Id: manual.tex,v 1.29 1997/03/06 22:19:08 roberto Exp roberto $ 1% $Id: manual.tex,v 1.30 1997/06/09 18:16:33 roberto Exp roberto $
2 2
3\documentstyle[fullpage,11pt,bnf]{article} 3\documentstyle[fullpage,11pt,bnf]{article}
4 4
5\newcommand{\rw}[1]{{\bf #1}} 5\newcommand{\rw}[1]{{\bf #1}}
6\newcommand{\see}[1]{(see Section~\ref{#1})} 6\newcommand{\See}[1]{Section~\ref{#1}}
7\newcommand{\see}[1]{(see \See{#1})}
7\newcommand{\M}[1]{$#1$} 8\newcommand{\M}[1]{$#1$}
8\newcommand{\nil}{{\bf nil}} 9\newcommand{\nil}{{\bf nil}}
9\newcommand{\Line}{\rule{\linewidth}{.5mm}} 10\newcommand{\Line}{\rule{\linewidth}{.5mm}}
@@ -16,7 +17,7 @@
16 17
17\newcommand{\ff}{$\bullet$\ } 18\newcommand{\ff}{$\bullet$\ }
18 19
19\newcommand{\Version}{2.5} 20\newcommand{\Version}{3.0}
20 21
21\makeindex 22\makeindex
22 23
@@ -36,7 +37,7 @@ Waldemar Celes
36\tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio 37\tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio
37} 38}
38 39
39\date{\small \verb$Date: 1997/03/06 22:19:08 $} 40\date{\small \verb$Date: 1997/06/09 18:16:33 $}
40 41
41\maketitle 42\maketitle
42 43
@@ -70,7 +71,7 @@ a intera\c{c}\~ao entre programas Lua e programas C hospedeiros.
70\begin{quotation} 71\begin{quotation}
71\noindent 72\noindent
72\footnotesize 73\footnotesize
73Copyright (c) 1994--1996 TeCGraf, PUC-Rio. Written by Waldemar Celes Filho, 74Copyright (c) 1994--1997 TeCGraf, PUC-Rio. Written by Waldemar Celes Filho,
74Roberto Ierusalimschy, Luiz Henrique de Figueiredo. All rights reserved. 75Roberto Ierusalimschy, Luiz Henrique de Figueiredo. All rights reserved.
75% 76%
76Permission is hereby granted, without written agreement and without license or 77Permission is hereby granted, without written agreement and without license or
@@ -111,9 +112,8 @@ documentation.
111Lua is an extension programming language designed to support 112Lua is an extension programming language designed to support
112general procedural programming features with data description 113general procedural programming features with data description
113facilities. 114facilities.
114It is intended to be used as a 115It is intended to be used as a light-weight, but powerful,
115light-weight, but powerful, configuration language for any 116configuration language for any program that needs one.
116program that needs one.
117Lua has been designed and implemented by 117Lua has been designed and implemented by
118W.~Celes, 118W.~Celes,
119R.~Ierusalimschy and 119R.~Ierusalimschy and
@@ -184,20 +184,22 @@ are interchangeable.
184Lua automatically detects the file type and acts accordingly. 184Lua automatically detects the file type and acts accordingly.
185\index{pre-compilation} 185\index{pre-compilation}
186 186
187\section{\Index{Types}} \label{TypesSec} 187\section{\Index{Types and Tags}} \label{TypesSec}
188 188
189Lua is a dynamically typed language. 189Lua is a dynamically typed language.
190Variables do not have types; only values do. 190Variables do not have types; only values do.
191All values carry their own type.
192Therefore, there are no type definitions in the language. 191Therefore, there are no type definitions in the language.
192All values carry their own type.
193Besides a type, all values also have a \Index{tag}.
193 194
194There are seven \Index{basic types} in Lua: \Def{nil}, \Def{number}, 195There are six \Index{basic types} in Lua: \Def{nil}, \Def{number},
195\Def{string}, \Def{function}, \Def{CFunction}, \Def{userdata}, 196\Def{string}, \Def{function}, \Def{userdata}, and \Def{table}.
196and \Def{table}.
197{\em Nil\/} is the type of the value \nil, 197{\em Nil\/} is the type of the value \nil,
198whose main property is to be different from any other value. 198whose main property is to be different from any other value.
199{\em Number\/} represents real (floating point) numbers, 199{\em Number\/} represents real (floating point) numbers,
200while {\em string\/} has the usual meaning. 200while {\em string\/} has the usual meaning.
201The function \verb|type| returns a string describing the type
202of a given value.
201 203
202Functions are considered first-class values in Lua. 204Functions are considered first-class values in Lua.
203This means that functions can be stored in variables, 205This means that functions can be stored in variables,
@@ -205,14 +207,18 @@ passed as arguments to other functions and returned as results.
205When a function is defined in Lua, its body is compiled and stored 207When a function is defined in Lua, its body is compiled and stored
206in a given variable. 208in a given variable.
207Lua can call (and manipulate) functions written in Lua and 209Lua can call (and manipulate) functions written in Lua and
208functions written in C; the latter have type {\em CFunction}. 210functions written in C.
211They can be distinguished by their tags:
212all Lua functions have the same tag,
213which is different from the tag for all C functions.
209 214
210The type {\em userdata\/} is provided to allow 215The type {\em userdata\/} is provided to allow
211arbitrary \Index{C pointers} to be stored in Lua variables. 216arbitrary \Index{C pointers} to be stored in Lua variables.
212It corresponds to \verb|void*| and has no pre-defined operations in Lua, 217It corresponds to a \verb|void*| and has no pre-defined operations in Lua,
213besides assignment and equality test. 218besides assignment and equality test.
214However, by using fallbacks, the programmer may define operations 219However, by using {\em tag methods},
215for {\em userdata\/} values; \see{fallback}. 220the programmer may define operations for {\em userdata\/} values
221\see{tag-method}.
216 222
217The type {\em table\/} implements \Index{associative arrays}, 223The type {\em table\/} implements \Index{associative arrays},
218that is, \Index{arrays} that can be indexed not only with numbers, 224that is, \Index{arrays} that can be indexed not only with numbers,
@@ -236,6 +242,19 @@ to tables, and do not imply any kind of copy.
236Moreover, tables must be explicitly created before used 242Moreover, tables must be explicitly created before used
237\see{tableconstructor}. 243\see{tableconstructor}.
238 244
245Tags are mainly used to select tag methods when
246some events occur \see{tag-method}.
247Each of the types nil, number and string have a different tag.
248All values of each of these types have this same pre-defined tag.
249Values of type function may have two different tags,
250depending whether they are Lua or C functions.
251Finally,
252values of type userdata and table can have
253as many different tags as needed \see{tag-method}.
254Tags are created with the function \verb|newtag|,
255and the function \verb|tag| returns the tag of a given value.
256To change the tag of a given userdata or table,
257there is the function \verb|settag| \see{pdf-newtag}.
239 258
240 259
241\section{The Language} 260\section{The Language}
@@ -260,7 +279,7 @@ The following words are reserved, and cannot be used as identifiers:
260The following strings denote other \Index{tokens}: 279The following strings denote other \Index{tokens}:
261\begin{verbatim} 280\begin{verbatim}
262 ~= <= >= < > == = .. + - * / 281 ~= <= >= < > == = .. + - * /
263 % ( ) { } [ ] ; , . 282 % ( ) { } [ ] ; , . ...
264\end{verbatim} 283\end{verbatim}
265 284
266\Index{Literal strings} can be delimited by matching single or double quotes, 285\Index{Literal strings} can be delimited by matching single or double quotes,
@@ -271,7 +290,8 @@ Literals in this bracketed form may run for several lines,
271may contain nested \verb|[[ ... ]]| pairs, 290may contain nested \verb|[[ ... ]]| pairs,
272and do not interpret escape sequences. 291and do not interpret escape sequences.
273This form is specially convenient for 292This form is specially convenient for
274handling text that has quoted strings in it. 293handling strings that contain program pieces or
294other quoted strings.
275 295
276\Index{Comments} start anywhere outside a string with a 296\Index{Comments} start anywhere outside a string with a
277double hyphen (\verb|--|) and run until the end of the line. 297double hyphen (\verb|--|) and run until the end of the line.
@@ -287,6 +307,35 @@ Examples of valid numerical constants are:
287 4 4.0 0.4 4.57e-3 0.3e12 307 4 4.0 0.4 4.57e-3 0.3e12
288\end{verbatim} 308\end{verbatim}
289 309
310\subsection{The \Index{Pre-processor}} \label{pre-processor}
311
312All lines that start with a \verb|$| are handled by a pre-processor.
313The \verb|$| can be followed by any of the following directives:
314\begin{description}
315\item[{\tt debug}] --- turn on some debugging facilities \see{pragma}.
316\item[{\tt nodebug}] --- turn off some debugging facilities \see{pragma}.
317\item[{\tt if \M{cond}}] --- starts a condicional part.
318If \M{cond} is false, this part is skiped by the lexical analizer.
319\item[{\tt ifnot \M{cond}}] --- starts a condicional part.
320If \M{cond} is true, this part is skiped by the lexical analizer.
321\item[{\tt end}] --- ends a conditional part.
322\item[{\tt else}] --- starts an ``else'' conditional part,
323switching the ``skip'' status.
324\item[{\tt endinput}] --- ends the lexical parse of the file.
325\end{description}
326
327Directives can be freely nested.
328Particularlly, a \verb|$endinput| may ocurr inside a \verb|$if|;
329in that case, even the matching \verb|$end| is not parsed.
330
331A \M{cond} part may be:
332\begin{description}
333\item[{\tt nil}] --- always false.
334\item[{\tt 1}] --- always true.
335\item[\M{name}] --- true if global variable \M{name} is different from \nil.
336Notice that this is evaluated before the chunk starts its execution.
337Therefore, actions in a chunk do not affect its own conditional directives.
338\end{description}
290 339
291\subsection{\Index{Coercion}} \label{coercion} 340\subsection{\Index{Coercion}} \label{coercion}
292 341
@@ -336,7 +385,7 @@ Any statement can be optionally followed by a semicolon:
336\end{Produc}% 385\end{Produc}%
337For syntactic reasons, a \IndexVerb{return} statement can only be written 386For syntactic reasons, a \IndexVerb{return} statement can only be written
338as the last statement of a block. 387as the last statement of a block.
339This restriction also avoids some ``statement not reached'' errors. 388This restriction also avoids some ``statement not reached'' conditions.
340 389
341\subsubsection{\Index{Assignment}} \label{assignment} 390\subsubsection{\Index{Assignment}} \label{assignment}
342The language allows \Index{multiple assignment}. 391The language allows \Index{multiple assignment}.
@@ -366,12 +415,17 @@ Square brackets are used to index a table:
366\begin{Produc} 415\begin{Produc}
367\produc{var}{var \ter{[} exp1 \ter{]}} 416\produc{var}{var \ter{[} exp1 \ter{]}}
368\end{Produc}% 417\end{Produc}%
369If \verb|var| results in a table value, 418The \verb|var| should result in a table value,
370the field indexed by the expression value gets the assigned value. 419where the field indexed by the expression value gets the assigned value.
371Otherwise, the fallback {\em settable\/} is called, 420
372with three parameters: the value of \verb|var|, 421The meaning of assignments and evaluations of global variables and
373the value of expression, and the value being assigned to it; 422indexed variables can be changed by tag methods \see{tag-method}.
374\see{fallback}. 423Actually,
424an assignment \verb|x = val|, where \verb|x| is a global variable,
425is equivalent to a call \verb|setglobal('x', val)|,
426and an assignment \verb|t[i] = val| is equivalent to
427\verb|settable(t, i, val)|.
428See \See{tag-method} for a description of these ``functions''.
375 429
376The syntax \verb|var.NAME| is just syntactic sugar for 430The syntax \verb|var.NAME| is just syntactic sugar for
377\verb|var["NAME"]|: 431\verb|var["NAME"]|:
@@ -441,6 +495,12 @@ Numbers (numerical constants) and
441string literals are explained in Section~\ref{lexical}. 495string literals are explained in Section~\ref{lexical}.
442Variables are explained in Section~\ref{assignment}. 496Variables are explained in Section~\ref{assignment}.
443 497
498An access to a global variable \verb|x| is equivalent to a
499call \verb|getglobal('x')|;
500an access to an indexed variable \verb|t[i]| is equivalent to
501a call \verb|gettable(t, i)|.
502See \See{tag-method} for a description of these ``functions''.
503
444The non-terminal \verb|exp1| is used to indicate that the values 504The non-terminal \verb|exp1| is used to indicate that the values
445returned by an expression must be adjusted to one single value: 505returned by an expression must be adjusted to one single value:
446\begin{Produc} 506\begin{Produc}
@@ -458,9 +518,9 @@ and the unary \verb|-| (negation).
458If the operands are numbers, or strings that can be converted to 518If the operands are numbers, or strings that can be converted to
459numbers, according to the rules given in Section~\ref{coercion}, 519numbers, according to the rules given in Section~\ref{coercion},
460then all operations except exponentiation have the usual meaning. 520then all operations except exponentiation have the usual meaning.
461Otherwise, the fallback ``arith'' is called \see{fallback}. 521Otherwise, a tag method is called \see{tag-method}.
462An exponentiation always calls this fallback. 522An exponentiation always calls a tag method.
463The standard mathematical library redefines this fallback, 523The standard mathematical library redefines this method for numbers,
464giving the expected meaning to \Index{exponentiation} 524giving the expected meaning to \Index{exponentiation}
465\see{mathlib}. 525\see{mathlib}.
466 526
@@ -469,25 +529,24 @@ Lua provides the following \Index{relational operators}:
469\begin{verbatim} 529\begin{verbatim}
470 < > <= >= ~= == 530 < > <= >= ~= ==
471\end{verbatim} 531\end{verbatim}
472All these return \nil\ as false and a value different from \nil\ 532All these return \nil\ as false and a value different from \nil\ as true.
473(actually the number 1) as true.
474 533
475Equality first compares the types of its operands. 534Equality first compares the types of its operands.
476If they are different, then the result is \nil. 535If they are different, then the result is \nil.
477Otherwise, their values are compared. 536Otherwise, their values are compared.
478Numbers and strings are compared in the usual way. 537Numbers and strings are compared in the usual way.
479Tables, CFunctions, and functions are compared by reference, 538Tables, userdatas and functions are compared by reference,
480that is, two tables are considered equal only if they are the same table. 539that is, two tables are considered equal only if they are the same table.
481The operator \verb|~=| is exactly the negation of equality (\verb|==|). 540The operator \verb|~=| is exactly the negation of equality (\verb|==|).
482Note that the conversion rules of Section~\ref{coercion} 541Note that the conversion rules of Section~\ref{coercion}
483do not apply to equality comparisons. 542{\em do not\/} apply to equality comparisons.
484Thus, \verb|"0"==0| evaluates to false. 543Thus, \verb|"0"==0| evaluates to false.
485 544
486The other operators work as follows. 545The other operators work as follows.
487If both arguments are numbers, then they are compared as such. 546If both arguments are numbers, then they are compared as such.
488Otherwise, if both arguments can be converted to strings, 547Otherwise, if both arguments are strings,
489their values are compared using lexicographical order. 548their values are compared using lexicographical order.
490Otherwise, the ``order'' fallback is called \see{fallback}. 549Otherwise, the ``order'' tag method is called \see{tag-method}.
491%Note that the conversion rules of Section~\ref{coercion} 550%Note that the conversion rules of Section~\ref{coercion}
492%do apply to order operators. 551%do apply to order operators.
493%Thus, \verb|"2">"12"| evaluates to true. 552%Thus, \verb|"2">"12"| evaluates to true.
@@ -507,14 +566,14 @@ if it is different from \nil;
507otherwise it returns its second argument. 566otherwise it returns its second argument.
508Both \verb|and| and \verb|or| use \Index{short-cut evaluation}, 567Both \verb|and| and \verb|or| use \Index{short-cut evaluation},
509that is, 568that is,
510the second operand is evaluated only if necessary. 569the second operand is evaluated only when necessary.
511 570
512\subsubsection{Concatenation} 571\subsubsection{Concatenation}
513Lua offers a string \Index{concatenation} operator, 572Lua offers a string \Index{concatenation} operator,
514denoted by ``\IndexVerb{..}''. 573denoted by ``\IndexVerb{..}''.
515If operands are strings or numbers, then they are converted to 574If operands are strings or numbers, then they are converted to
516strings according to the rules in Section~\ref{coercion}. 575strings according to the rules in Section~\ref{coercion}.
517Otherwise, the fallback ``concat'' is called \see{fallback}. 576Otherwise, the tag method ``concat'' is called \see{tag-method}.
518 577
519\subsubsection{Precedence} 578\subsubsection{Precedence}
520\Index{Operator precedence} follows the table below, 579\Index{Operator precedence} follows the table below,
@@ -565,23 +624,29 @@ is essentialy equivalent to:
565 a = temp 624 a = temp
566\end{verbatim} 625\end{verbatim}
567 626
568The next form initializes named fields in a table: 627The form {\em ffieldlist1\/} initializes other fields in a table:
569\begin{Produc} 628\begin{Produc}
570\produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}} 629\produc{ffieldlist1}{ffield \rep{\ter{,} ffield} \opt{\ter{,}}}
571\produc{ffield}{name \ter{=} exp} 630\produc{ffield}{\ter{[} exp \ter{]} \ter {=} exp \Or name \ter{=} exp}
572\end{Produc}% 631\end{Produc}%
573For example: 632For example:
574\begin{verbatim} 633\begin{verbatim}
575 a = {x = 1, y = 3} 634 a = {[f(k)] = g(y), x = 1, y = 3, [0] = b+c}
576\end{verbatim} 635\end{verbatim}
577is essentialy equivalent to: 636is essentialy equivalent to:
578\begin{verbatim} 637\begin{verbatim}
579 temp = {} 638 temp = {}
639 temp[f(k)] = g(y)
580 temp.x = 1 -- or temp["x"] = 1 640 temp.x = 1 -- or temp["x"] = 1
581 temp.y = 3 -- or temp["y"] = 3 641 temp.y = 3 -- or temp["y"] = 3
642 temp[0] = b+c
582 a = temp 643 a = temp
583\end{verbatim} 644\end{verbatim}
584 645Notice that an expression like \verb|{x = 1, y = 4}| is
646in fact a syntactic sugar for
647\begin{verbatim}
648 {["x"] = 1, ["y"] = 4}
649\end{verbatim}
585 650
586\subsubsection{Function Calls} \label{functioncall} 651\subsubsection{Function Calls} \label{functioncall}
587A \Index{function call} has the following syntax: 652A \Index{function call} has the following syntax:
@@ -589,9 +654,9 @@ A \Index{function call} has the following syntax:
589\produc{functioncall}{var realParams} 654\produc{functioncall}{var realParams}
590\end{Produc}% 655\end{Produc}%
591Here, \verb|var| can be any variable (global, local, indexed, etc). 656Here, \verb|var| can be any variable (global, local, indexed, etc).
592If its value has type {\em function\/} or {\em CFunction}, 657If its value has type {\em function\/},
593then this function is called. 658then this function is called.
594Otherwise, the ``function'' fallback is called, 659Otherwise, the ``function'' tag method is called,
595having as first parameter the value of \verb|var|, 660having as first parameter the value of \verb|var|,
596and then the original call parameters. 661and then the original call parameters.
597 662
@@ -612,10 +677,7 @@ except that \verb|var| is evaluated only once.
612\produc{realParams}{tableconstructor} 677\produc{realParams}{tableconstructor}
613\produc{explist1}{exp1 \rep{\ter{,} exp1}} 678\produc{explist1}{exp1 \rep{\ter{,} exp1}}
614\end{Produc}% 679\end{Produc}%
615All argument expressions are evaluated before the call; 680All argument expressions are evaluated before the call.
616then the list of \Index{arguments} is adjusted to
617the length of the list of parameters \see{adjust};
618finally, this list is assigned to the formal parameters.
619A call of the form \verb|f{...}| is syntactic sugar for 681A call of the form \verb|f{...}| is syntactic sugar for
620\verb|f({...})|, that is, 682\verb|f({...})|, that is,
621the parameter list is a single new table. 683the parameter list is a single new table.
@@ -629,8 +691,7 @@ thus discarding all returned values.
629If the function is called in a place that needs a single value 691If the function is called in a place that needs a single value
630(syntactically denoted by the non-terminal \verb|exp1|), 692(syntactically denoted by the non-terminal \verb|exp1|),
631then its return list is adjusted to 1, 693then its return list is adjusted to 1,
632thus discarding all returned values, 694thus discarding all returned values but the first one.
633except the first one.
634If the function is called in a place that can hold many values 695If the function is called in a place that can hold many values
635(syntactically denoted by the non-terminal \verb|exp|), 696(syntactically denoted by the non-terminal \verb|exp|),
636then no adjustment is made. 697then no adjustment is made.
@@ -656,8 +717,39 @@ a function definition is an assignment to a global variable.
656Parameters act as local variables, 717Parameters act as local variables,
657initialized with the argument values. 718initialized with the argument values.
658\begin{Produc} 719\begin{Produc}
659\produc{parlist1}{name \rep{\ter{,} name}} 720\produc{parlist1}{\ter{\ldots}}
721\produc{parlist1}{name \rep{\ter{,} name} \opt{\ter{,} \ter{\ldots}}}
660\end{Produc} 722\end{Produc}
723\label{vararg}
724When a function is called,
725the list of \Index{arguments} is adjusted to
726the length of the list of parameters \see{adjust},
727unless the function is a \Def{vararg} function,
728indicated by the dots (\ldots) at the end of its parameter list.
729A vararg function does not adjust its argument list;
730instead, it collects any extra arguments in an implicit parameter,
731called \Def{arg}.
732This parameter is always initialized as a table,
733with a field \verb|n| with the number of extra arguments,
734and the extra arguments at positions 1, 2, \ldots.
735
736As an example, suppose definitions like:
737\begin{verbatim}
738 function f(a, b) end
739 function g(a, b, ...) end
740\end{verbatim}
741Then, we have the following mapping from arguments to parameters:
742\begin{verbatim}
743 CALL PARAMETERS
744
745 f(3) a=3, b=nil
746 f(3, 4) a=3, b=4
747 f(3, 4, 5) a=3, b=4
748
749 g(3) a=3, b=nil, arg={n=0}
750 g(3, 4) a=3, b=4, arg={n=0}
751 g(3, 4, 5, 8) a=3, b=4, arg={5, 8; n=2}
752\end{verbatim}
661 753
662Results are returned using the \verb|return| statement \see{return}. 754Results are returned using the \verb|return| statement \see{return}.
663If control reaches the end of a function without a return instruction, 755If control reaches the end of a function without a return instruction,
@@ -683,95 +775,298 @@ end
683\end{verbatim} 775\end{verbatim}
684that is, the function gets an extra formal parameter called \verb|self|. 776that is, the function gets an extra formal parameter called \verb|self|.
685Notice that 777Notice that
686the variable \verb|v| must have been previously initialized with a table value. 778the variable \verb|v| must have been
779previously initialized with a table value.
687 780
688 781
689\subsection{Fallbacks} \label{fallback} 782\subsection{Tag Methods} \label{tag-method}
690 783
691Lua provides a powerful mechanism to extend its semantics, 784Lua provides a powerful mechanism to extend its semantics,
692called \Def{fallbacks}. 785called \Def{Tag Methods}.
693A fallback is a programmer defined function 786A tag method (TM) is a programmer defined function
694that is called whenever Lua does not know how to proceed. 787that can be called in many key points of the evaluation of a program,
788allowing a programmer to change the standard Lua behavior at these points.
789Each of these points is called an \Def{event}.
790
791The tag method called for any specific event is selected
792accordingly with the tag~\see{TypesSec} of the values involved
793in the event.
794The function \IndexVerb{settagmethod} changes the tag method
795associated with a given pair $<tag, event>$.
796Its first parameter is the tag, the second the event name
797(a string, see below),
798and the third parameter is the new method (a function),
799or \nil\ to restore the default behavior.
800The function returns the previous tag method.
801Another function, \IndexVerb{gettagmethod},
802receives a tag and an event name and returns the
803current method associated with the pair.
804
805Tag methods are called in the following events,
806identified by the given names.
807The semantics of tag methods is better explained by a Lua function
808describing the behavior of the interpreter at each event.
809The function not only shows when a tag method is called,
810but also its arguments, its results and the default behavior.
811Please notice that the code shown here is only illustrative;
812the real behavior is hard coded in the interpreter,
813and it is much more eficient than this simulation.
814All functions used in these descriptions
815(\verb|rawgetglobal|, \verb|tonumber|, \verb|call|, etc)
816are described in \See{predefined}.
695 817
696Lua supports the following fallbacks,
697identified by the given strings:
698\begin{description} 818\begin{description}
699\item[``arith'':]\index{arithmetic fallback} 819
700called when an arithmetic operation is applied to non numerical operands, 820\item[``add'':]\index{add event}
701or when the binary \verb|^| operation (exponentiation) is called. 821called when a \verb|+| operation is applied to non numerical operands.
702It receives three arguments: 822
703the two operands (the second one is \nil\ when the operation is unary minus) 823The function \verb|getbinmethod| defines how Lua chooses a tag method
704and one of the following strings describing the offended operator: 824for a binary operation.
705\begin{verbatim} 825First Lua trys the first operand.
706 add sub mul div pow unm 826If its tag does not define a tag method for the operation,
707\end{verbatim} 827Lua trys the second operand.
708Its return value is the final result of the arithmetic operation. 828If it also fails, it gets a tag method from tag 0:
709The default handler issues an error. 829\begin{verbatim}
710\item[``order'':]\index{order fallback} 830 function getbinmethod (op1, op2, event)
711called when an order comparison is applied to non numerical or 831 return gettagmethod(tag(op1), event) or
712non string operands. 832 gettagmethod(tag(op2), event) or
713It receives three arguments: 833 gettagmethod(0, event)
714the two operands and 834 end
715one of the following strings describing the offended operator: 835\end{verbatim}
716\begin{verbatim} 836\begin{verbatim}
717 lt gt le ge 837 function add_event (op1, op2)
718\end{verbatim} 838 local o1, o2 = tonumber(op1), tonumber(op2)
719Its return value is the final result of the comparison operation. 839 if o1 and o2 then -- both operands are numeric
720The default handler issues an error. 840 return o1+o2 -- '+' here is the primitive 'add'
721\item[``concat'':]\index{concatenation fallback} 841 else -- at least one of the operands is not numeric.
842 local tm = getbinmethod(op1, op2, "add")
843 if tm then
844 -- call the method with both operands and an extra
845 -- argument with the event name
846 return tm(op1, op2, "add")
847 else -- no tag method available: Default behavior
848 error("unexpected type at arithmetic operation")
849 end
850 end
851 end
852\end{verbatim}
853
854\item[``sub'':]\index{sub event}
855called when a \verb|-| operation is applied to non numerical operands.
856Behavior is similar to \verb|"add"| event.
857
858\item[``mul'':]\index{mul event}
859called when a \verb|*| operation is applied to non numerical operands.
860Behavior is similar to \verb|"add"| event.
861
862\item[``div'':]\index{div event}
863called when a \verb|/| operation is applied to non numerical operands.
864Behavior is similar to \verb|"add"| event.
865
866\item[``pow'':]\index{pow event}
867called whenenver a \verb|^| operation is applied.
868\begin{verbatim}
869 function pow_event (op1, op2)
870 local tm = getbinmethod(op1, op2, "pow")
871 if tm then
872 -- call the method with both operands and an extra
873 -- argument with the event name
874 return tm(op1, op2, "pow")
875 else -- no tag method available: Default behavior
876 error("unexpected type at arithmetic operation")
877 end
878 end
879\end{verbatim}
880
881\item[``unm'':]\index{unm event}
882called when an unary \verb|-| operation is applied to a non numerical operand.
883\begin{verbatim}
884 function unm_event (op)
885 local o = tonumber(op)
886 if o then -- operand is numeric
887 return -o -- '-' here is the primitive 'unm'
888 else -- the operand is not numeric.
889 -- Try to get a tag method from the operand;
890 -- if it does not have one, try a "global" one (tag 0)
891 local tm = gettagmethod(tag(op), "unm") or
892 gettagmethod(0, "unm")
893 if tm then
894 -- call the method with the operand, nil, and an extra
895 -- argument with the event name
896 return tm(op, nil, "unm")
897 else -- no tag method available: Default behavior
898 error("unexpected type at arithmetic operation")
899 end
900 end
901 end
902\end{verbatim}
903
904\item[``lt'':]\index{lt event}
905called when a \verb|<| operation is applied to non numerical
906or non string operands.
907\begin{verbatim}
908 function lt_event (op1, op2)
909 if type(op1) == "number" and type(op2) == "number" then
910 return op1 < op2 -- numeric comparison
911 elseif type(op1) == "string" and type(op2) == "string" then
912 return op1 < op2 -- lexicographic comparison
913 else
914 local tm = getbinmethod(op1, op2, "lt")
915 if tm then
916 return tm(op1, op2, "lt")
917 else
918 error("unexpected type at comparison");
919 end
920 end
921 end
922\end{verbatim}
923
924\item[``gt'':]\index{gt event}
925called when a \verb|>| operation is applied to non numerical
926or non string operands.
927Behavior is similar to \verb|"lt"| event.
928
929\item[``le'':]\index{le event}
930called when a \verb|<=| operation is applied to non numerical
931or non string operands.
932Behavior is similar to \verb|"lt"| event.
933
934\item[``ge'':]\index{ge event}
935called when a \verb|>=| operation is applied to non numerical
936or non string operands.
937Behavior is similar to \verb|"lt"| event.
938
939\item[``concat'':]\index{concatenation event}
722called when a concatenation is applied to non string operands. 940called when a concatenation is applied to non string operands.
723It receives the two operands as arguments. 941\begin{verbatim}
724Its return value is the final result of the concatenation operation. 942 function concat_event (op1, op2)
725The default handler issues an error. 943 if (type(op1) == "string" or type(op1) == "number") and
726\item[``index'':]\index{index fallback} 944 (type(op2) == "string" or type(op2) == "number") then
945 return op1..op2 -- primitive string concatenation
946 else
947 local tm = getbinmethod(op1, op2, "concat")
948 if tm then
949 return tm(op1, op2, "concat")
950 else
951 error("unexpected type for concatenation")
952 end
953 end
954 end
955\end{verbatim}
956
957\item[``index'':]\index{index event}
727called when Lua tries to retrieve the value of an index 958called when Lua tries to retrieve the value of an index
728not present in a table. 959not present in a table.
729It receives as arguments the table and the index. 960See event \verb|"gettable"| for its semantics.
730Its return value is the final result of the indexing operation. 961
731The default handler returns \nil. 962\item[``getglobal'':]\index{getglobal event}
732\item[``getglobal'':]\index{index getglobal} 963called whenever Lua accesses a global variable.
733called when Lua tries to retrieve the value of a global variable 964\begin{verbatim}
734which has a \nil\ value (or which has not been initialized). 965 function getglobal (varname)
735It receives as argument the name of the variable. 966 local value = rawgetglobal(varname)
736Its return value is the final result of the expression. 967 local tm = gettagmethod(tag(value), "getglobal")
737The default handler returns \nil. 968 if not tm then
738\item[``gettable'':]\index{gettable fallback} 969 return value
739called when Lua tries to index a non table value. 970 else
740It receives as arguments the non table value and the index. 971 return tm(varname, value)
741Its return value is the final result of the indexing operation. 972 end
742The default handler issues an error. 973 end
743\item[``settable'':]\index{settable fallback} 974\end{verbatim}
744called when Lua tries to assign to an index in a non table value. 975Notice: the function \verb|getglobal| is pre-defined in Lua \see{predefined}.
745It receives as arguments the non table value, 976
746the index, and the assigned value. 977\item[``setglobal'':]\index{setglobal event}
747The default handler issues an error. 978called whenever Lua assigns to a global variable.
748\item[``function'':]\index{function fallback} 979\begin{verbatim}
980 function setglobal (varname, newvalue)
981 local oldvalue = rawgetglobal(varname)
982 local tm = gettagmethod(tag(oldvalue), "setglobal")
983 if not tm then
984 return rawsetglobal(varname, newvalue)
985 else
986 return tm(varname, oldvalue, newvalue)
987 end
988 end
989\end{verbatim}
990Notice: the function \verb|setglobal| is pre-defined in Lua \see{predefined}.
991
992\item[``gettable'':]\index{gettable event}
993called whenever Lua accesses an indexed variable.
994\begin{verbatim}
995 function gettable_event (table, index)
996 local tm = gettagmethod(tag(table), "gettable")
997 if tm then
998 return tm(table, index)
999 elseif type(table) ~= "table" then
1000 error("indexed expression not a table");
1001 else
1002 local v = rawgettable(table, index)
1003 tm = gettagmethod(tag(table), "index")
1004 if (v == nil) and tm then
1005 return tm(table, index)
1006 else
1007 return v
1008 end
1009 end
1010 end
1011\end{verbatim}
1012
1013\item[``settable'':]\index{settable event}
1014called whenever Lua assigns to an indexed variable.
1015in a table.
1016\begin{verbatim}
1017 function settable_event (table, index, value)
1018 local tm = gettagmethod(tag(table), "settable")
1019 if tm then
1020 return tm(table, index, value)
1021 elseif type(table) ~= "table" then
1022 error("indexed expression not a table")
1023 else
1024 rawsettable(table, index, value)
1025 end
1026 end
1027\end{verbatim}
1028
1029\item[``function'':]\index{function event}
749called when Lua tries to call a non function value. 1030called when Lua tries to call a non function value.
750It receives as arguments the non function value and the 1031\begin{verbatim}
751arguments given in the original call. 1032 function function_event (func, ...)
752Its return values are the final results of the call operation. 1033 if type(func) == "function" then
753The default handler issues an error. 1034 return call(func, arg)
754\item[``gc'':] 1035 else
755called during garbage collection. 1036 local tm = gettagmethod(tag(func), "function")
756It receives as argument the table being collected. 1037 if tm then
757After each run of the collector this function is called with argument \nil, 1038 local i = arg.n
758to signal the completion of the garbage collection. 1039 while i > 0 do
759Because this function operates during garbage collection, 1040 arg[i+1] = arg[i]
760it must be used with great care, 1041 i = i-1
761and programmers should avoid the creation of new objects 1042 end
762(tables or strings) in this function. 1043 arg.n = arg.n+1
763The default handler does nothing. 1044 arg[1] = func
764\item[``error'':]\index{error fallback} 1045 return call(tm, arg)
765called when an error occurs. 1046 else
766It receives as argument a string describing the error. 1047 error("call expression not a function")
767The default handler prints the message on the standard error output 1048 end
768(\verb|stderr|). 1049 end
1050 end
1051\end{verbatim}
1052
1053\item[``gc'':]\index{gc event}
1054called when Lua is garbage collecting an object.
1055For each object to be collected,
1056Lua does the equivalent of the following function:
1057\begin{verbatim}
1058 function gc_event (obj)
1059 local tm = gettagmethod(tag(obj), "gc")
1060 if tm then
1061 tm(obj)
1062 end
1063 end
1064\end{verbatim}
1065Moreover, at the end of a garbage collection cicle,
1066Lua does the equivalent to the call \verb|gc_event(nil)|.
1067
769\end{description} 1068\end{description}
770 1069
771The function \IndexVerb{setfallback} is used to change a fallback handler.
772Its first argument is the name of a fallback condition,
773and the second argument is the new function to be called.
774It returns the old handler function for the given fallback.
775 1070
776 1071
777\subsection{Error Handling} \label{error} 1072\subsection{Error Handling} \label{error}
@@ -779,27 +1074,26 @@ It returns the old handler function for the given fallback.
779Because Lua is an extension language, 1074Because Lua is an extension language,
780all Lua actions start from C code calling a function from the Lua library. 1075all Lua actions start from C code calling a function from the Lua library.
781Whenever an error occurs during Lua compilation or execution, 1076Whenever an error occurs during Lua compilation or execution,
782the ``error'' fallback function is called, 1077the \Def{error method} is called,
783and then the corresponding function from the library 1078and then the corresponding function from the library
784(\verb|lua_dofile|, \verb|lua_dostring|, 1079(\verb|lua_dofile|, \verb|lua_dostring|, or \verb|lua_callfunction|)
785\verb|lua_call|, or \verb|lua_callfunction|)
786is terminated returning an error condition. 1080is terminated returning an error condition.
787 1081
788The only argument to the ``error'' fallback function is a string 1082The only argument to the error method is a string
789describing the error. 1083describing the error.
790The standard I/O library redefines this fallback, 1084The standard I/O library redefines this method,
791using the debug facilities \see{debugI}, 1085using the debug facilities \see{debugI},
792in order to print some extra information, 1086in order to print some extra information,
793like the call stack. 1087like the call stack.
794To provide more information about errors, 1088To provide more information about errors,
795Lua programs can include the compilation pragma \verb|$debug|. 1089Lua programs can include the compilation pragma \verb|$debug|.
796\index{debug pragma}\label{pragma} 1090\index{debug pragma}\label{pragma}
797This pragma must be written in a line by itself.
798When an error occurs in a program compiled with this option, 1091When an error occurs in a program compiled with this option,
799the error routine is able to print the number of the lines where the calls 1092the error routine is able to print the number of the lines where the calls
800(and the error) were made. 1093(and the error) were made.
801If needed, it is possible to change the ``error'' fallback handler 1094If needed, it is possible to change the error method with the
802\see{fallback}. 1095function \verb|seterrormethod|,
1096which gets the new error handler as its only parameter.
803 1097
804Lua code can explicitly generate an error by calling the built-in 1098Lua code can explicitly generate an error by calling the built-in
805function \verb|error| \see{pdf-error}. 1099function \verb|error| \see{pdf-error}.
@@ -832,12 +1126,7 @@ for instance,
832the comparisson of two \verb|lua_Object's| is undefined. 1126the comparisson of two \verb|lua_Object's| is undefined.
833 1127
834To check the type of a \verb|lua_Object|, 1128To check the type of a \verb|lua_Object|,
835the following function is available: 1129the following functions are available:
836\Deffunc{lua_type}
837\begin{verbatim}
838int lua_type (lua_Object object);
839\end{verbatim}
840plus the following functions:
841\Deffunc{lua_isnil}\Deffunc{lua_isnumber}\Deffunc{lua_isstring} 1130\Deffunc{lua_isnil}\Deffunc{lua_isnumber}\Deffunc{lua_isstring}
842\Deffunc{lua_istable}\Deffunc{lua_iscfunction}\Deffunc{lua_isuserdata} 1131\Deffunc{lua_istable}\Deffunc{lua_iscfunction}\Deffunc{lua_isuserdata}
843\Deffunc{lua_isfunction} 1132\Deffunc{lua_isfunction}
@@ -856,15 +1145,20 @@ The function \verb|lua_isnumber| accepts numbers and numerical strings,
856whereas 1145whereas
857\verb|lua_isstring| accepts strings and numbers \see{coercion}, 1146\verb|lua_isstring| accepts strings and numbers \see{coercion},
858and \verb|lua_isfunction| accepts Lua and C functions. 1147and \verb|lua_isfunction| accepts Lua and C functions.
859The function \verb|lua_type| can be used to distinguish between 1148
860different kinds of user data. 1149To check the tag of a \verb|lua_Object|,
1150the following function is available:
1151\Deffunc{lua_tag}
1152\begin{verbatim}
1153int lua_tag (lua_Object object);
1154\end{verbatim}
861 1155
862To translate a value from type \verb|lua_Object| to a specific C type, 1156To translate a value from type \verb|lua_Object| to a specific C type,
863the programmer can use: 1157the programmer can use:
864\Deffunc{lua_getnumber}\Deffunc{lua_getstring} 1158\Deffunc{lua_getnumber}\Deffunc{lua_getstring}
865\Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata} 1159\Deffunc{lua_getcfunction}\Deffunc{lua_getuserdata}
866\begin{verbatim} 1160\begin{verbatim}
867double lua_getnumber (lua_Object object); 1161float lua_getnumber (lua_Object object);
868char *lua_getstring (lua_Object object); 1162char *lua_getstring (lua_Object object);
869lua_CFunction lua_getcfunction (lua_Object object); 1163lua_CFunction lua_getcfunction (lua_Object object);
870void *lua_getuserdata (lua_Object object); 1164void *lua_getuserdata (lua_Object object);
@@ -876,10 +1170,10 @@ This \verb|lua_Object| must be a number or a string convertible to number
876\verb|lua_getstring| converts a \verb|lua_Object| to a string (\verb|char *|). 1170\verb|lua_getstring| converts a \verb|lua_Object| to a string (\verb|char *|).
877This \verb|lua_Object| must be a string or a number; 1171This \verb|lua_Object| must be a string or a number;
878otherwise, the function returns 0 (the \verb|NULL| pointer). 1172otherwise, the function returns 0 (the \verb|NULL| pointer).
879This function does not create a new string, but returns a pointer to 1173This function does not create a new string,
880a string inside the Lua environment. 1174but returns a pointer to a string inside the Lua environment.
881Because Lua has garbage collection, there is no guarantee that such 1175Because Lua has garbage collection,
882pointer will be valid after the block ends. 1176there is no guarantee that such pointer will be valid after the block ends.
883 1177
884\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function. 1178\verb|lua_getcfunction| converts a \verb|lua_Object| to a C function.
885This \verb|lua_Object| must have type {\em CFunction\/}; 1179This \verb|lua_Object| must have type {\em CFunction\/};
@@ -899,6 +1193,16 @@ It is good programming practice to convert Lua objects to C values
899as soon as they are available, 1193as soon as they are available,
900and never to store \verb|lua_Object|s in C global variables. 1194and never to store \verb|lua_Object|s in C global variables.
901 1195
1196A garbage collection cicle can be forced by:
1197\Deffunc{lua_collectgarbage}
1198\begin{verbatim}
1199long lua_collectgarbage (long limit);
1200\end{verbatim}
1201This function returns the number of objects collected.
1202The argument \verb|limit| makes the next cicle occur when that number
1203of new objects have been created.
1204If \verb|limit|=0, Lua uses an adaptative algorithm to set this limit.
1205
902 1206
903All comunication between Lua and C is done through two 1207All comunication between Lua and C is done through two
904abstract data types, called \Def{lua2C} and \Def{C2lua}. 1208abstract data types, called \Def{lua2C} and \Def{C2lua}.
@@ -906,8 +1210,6 @@ The first one, as the name implies, is used to pass values
906from Lua to C: parameters when Lua calls C and results when C calls Lua. 1210from Lua to C: parameters when Lua calls C and results when C calls Lua.
907The structure C2lua is used in the reverse direction: 1211The structure C2lua is used in the reverse direction:
908parameters when C calls Lua and results when Lua calls C. 1212parameters when C calls Lua and results when Lua calls C.
909Notice that the structure lua2C cannot be directly modified by C code,
910while the structure C2lua cannot be ``read'' by C code.
911 1213
912The structure lua2C is an abstract array, 1214The structure lua2C is an abstract array,
913which can be indexed with the function: 1215which can be indexed with the function:
@@ -917,11 +1219,11 @@ lua_Object lua_lua2C (int number);
917\end{verbatim} 1219\end{verbatim}
918where \verb|number| starts with 1. 1220where \verb|number| starts with 1.
919When called with a number larger than the array size, 1221When called with a number larger than the array size,
920this function returns 1222this function returns \verb|LUA_NOOBJECT|\Deffunc{LUA_NOOBJECT}.
921\verb|LUA_NOOBJECT|\Deffunc{LUA_NOOBJECT}.
922In this way, it is possible to write C functions that receive 1223In this way, it is possible to write C functions that receive
923a variable number of parameters, 1224a variable number of parameters,
924and to call Lua functions that return a variable number of results. 1225and to call Lua functions that return a variable number of results.
1226Notice that the structure lua2C cannot be directly modified by C code.
925 1227
926The second structure, C2lua, is a stack. 1228The second structure, C2lua, is a stack.
927Pushing elements into this stack 1229Pushing elements into this stack
@@ -938,32 +1240,47 @@ void lua_pushusertag (void *u, int tag);
938void lua_pushnil (void); 1240void lua_pushnil (void);
939void lua_pushobject (lua_Object object); 1241void lua_pushobject (lua_Object object);
940\end{verbatim} 1242\end{verbatim}
941plus the macro:
942\begin{verbatim}
943void lua_pushuserdata (void *u);
944\end{verbatim}
945All of them receive a C value, 1243All of them receive a C value,
946convert it to a corresponding \verb|lua_Object|, 1244convert it to a corresponding \verb|lua_Object|,
947and leave the result on the top of C2lua. 1245and leave the result on the top of C2lua.
1246The function
1247\Deffunc{lua_pop}
1248\begin{verbatim}
1249lua_Object lua_pop (void);
1250\end{verbatim}
1251returns a reference to the object at the top of the C2lua stack,
1252and pops it.
1253
1254As a general rule, all API functions pop from the stack
1255all elements that they use.
1256
1257Because userdata are objects,
1258the function \verb|lua_pushusertag| may create a new userdata.
1259If Lua has a userdata with the given value (\verb|void *|) and tag,
1260that userdata is pushed.
1261Otherwise, a new userdata is created, with the given value and tag.
1262If this function is called with
1263\verb|tag|=\verb|LUA_ANYTAG|\Deffunc{LUA_ANYTAG},
1264then Lua will try to find any userdata with the given value,
1265no matter its tag.
1266If there is no userdata with that value, a new one is created,
1267with tag=0.
948 1268
949User data can have different tags, 1269User data can have different tags,
950whose semantics are only known to the host program. 1270whose semantics are only known to the host program.
951Any positive integer can be used to tag a user datum. 1271Tags are created with the function:
952When a user datum is retrieved, 1272\Deffunc{lua_newtag}
953the function \verb|lua_type| can be used to get its tag. 1273\begin{verbatim}
954 1274int lua_newtag (void);
955{\em Please note:} most functions in the Lua API 1275\end{verbatim}
956use the structures lua2C and C2lua, 1276The function \verb|lua_settag| changes the tag of
957and therefore change their contents. 1277the object on the top of C2lua (and pops it);
958Great care must be taken, 1278the object must be a userdata or a table.
959specially when pushing a sequence of objects into C2lua, 1279\Deffunc{lua_settag}
960to avoid using those functions. 1280\begin{verbatim}
961The family of functions \verb|lua_get*|, \verb|lua_is*|, 1281void lua_settag (int tag);
962plus the function \verb|lua_lua2C|, 1282\end{verbatim}
963are safe to be called without modifying these structures; 1283\verb|tag| must be a value created with \verb|lua_newtag|.
964the family \verb|lua_push*| does not modify lua2C.
965All other functions may change lua2C and C2lua,
966unless noticed otherwise.
967 1284
968When C code calls Lua repeatedly, as in a loop, 1285When C code calls Lua repeatedly, as in a loop,
969objects returned by these calls accumulate, 1286objects returned by these calls accumulate,
@@ -995,8 +1312,10 @@ executes the \verb|stdin| stream.
995Function \verb|lua_dofile| is also able to execute pre-compiled chunks. 1312Function \verb|lua_dofile| is also able to execute pre-compiled chunks.
996It automatically detects whether the file is text or binary, 1313It automatically detects whether the file is text or binary,
997and loads it accordingly (see program \IndexVerb{luac}). 1314and loads it accordingly (see program \IndexVerb{luac}).
998These functions also return, in structure lua2C, 1315
1316These functions return, in structure lua2C,
999any values eventually returned by the chunks. 1317any values eventually returned by the chunks.
1318They also empty the stack C2lua.
1000 1319
1001 1320
1002\subsection{Manipulating Lua Objects} 1321\subsection{Manipulating Lua Objects}
@@ -1006,37 +1325,63 @@ one uses the function:
1006\begin{verbatim} 1325\begin{verbatim}
1007lua_Object lua_getglobal (char *varname); 1326lua_Object lua_getglobal (char *varname);
1008\end{verbatim} 1327\end{verbatim}
1009As in Lua, if the value of the global is \nil, 1328As in Lua, this function may call a tag method.
1010then the ``getglobal'' fallback is called. 1329To read the real value of any global variable,
1330without invoking any tag method,
1331this function has a {\em raw\/} version:
1332\Deffunc{lua_rawgetglobal}
1333\begin{verbatim}
1334lua_Object lua_rawgetglobal (char *varname);
1335\end{verbatim}
1011 1336
1012To store a value previously pushed onto C2lua in a global variable, 1337To store a value previously pushed onto C2lua in a global variable,
1013there is the function: 1338there is the function:
1014\Deffunc{lua_storeglobal} 1339\Deffunc{lua_storeglobal}
1015\begin{verbatim} 1340\begin{verbatim}
1016void lua_storeglobal (char *varname); 1341void lua_setglobal (char *varname);
1342\end{verbatim}
1343As in Lua, this function may call a tag method.
1344To set the real value of any global variable,
1345without invoking any tag method,
1346this function has a {\em raw\/} version:
1347\Deffunc{lua_rawgetglobal}
1348\begin{verbatim}
1349void lua_rawsetglobal (char *varname);
1017\end{verbatim} 1350\end{verbatim}
1018 1351
1019Tables can also be manipulated via the API. 1352Tables can also be manipulated via the API.
1020The function 1353The function
1021\Deffunc{lua_getsubscript} 1354\Deffunc{lua_gettable}
1022\begin{verbatim} 1355\begin{verbatim}
1023lua_Object lua_getsubscript (void); 1356lua_Object lua_gettable (void);
1024\end{verbatim} 1357\end{verbatim}
1025expects on the stack C2lua a table and an index, 1358pops from the stack C2lua a table and an index,
1026and returns the contents of the table at that index. 1359and returns the contents of the table at that index.
1027As in Lua, if the first object is not a table, 1360As in Lua, this operation may call a tag method.
1028or the index is not present in the table, 1361To get the real value of any table index,
1029the corresponding fallback is called. 1362without invoking any tag method,
1363this function has a {\em raw\/} version:
1364\Deffunc{lua_rawgetglobal}
1365\begin{verbatim}
1366lua_Object lua_rawgettable (void);
1367\end{verbatim}
1030 1368
1031To store a value in an index, 1369To store a value in an index,
1032the program must push the table, the index, 1370the program must push the table, the index,
1033and the value onto C2lua, 1371and the value onto C2lua,
1034and then call the function: 1372and then call the function:
1035\Deffunc{lua_storesubscript} 1373\Deffunc{lua_settable}
1036\begin{verbatim} 1374\begin{verbatim}
1037void lua_storesubscript (void); 1375void lua_settable (void);
1376\end{verbatim}
1377Again, the tag method for ``settable'' may be called.
1378To set the real value of any table index,
1379without invoking any tag method,
1380this function has a {\em raw\/} version:
1381\Deffunc{lua_rawsettable}
1382\begin{verbatim}
1383void lua_rawsettable (void);
1038\end{verbatim} 1384\end{verbatim}
1039Again, the ``settable'' fallback is called if a non-table value is used.
1040 1385
1041Finally, the function 1386Finally, the function
1042\Deffunc{lua_createtable} 1387\Deffunc{lua_createtable}
@@ -1045,33 +1390,6 @@ lua_Object lua_createtable (void);
1045\end{verbatim} 1390\end{verbatim}
1046creates and returns a new, empty table. 1391creates and returns a new, empty table.
1047 1392
1048As already noted,
1049most functions from the Lua library receive parameters through C2lua.
1050Because other functions also use this stack,
1051it is important that these
1052parameters be pushed just before the corresponding call,
1053without intermediate calls to the Lua library.
1054For instance, suppose the user wants the value of \verb|a[i]|,
1055where \verb|a| and \verb|i| are global Lua variables.
1056A simplistic solution would be:
1057\begin{verbatim}
1058 /* Warning: WRONG CODE */
1059 lua_Object result;
1060 lua_pushobject(lua_getglobal("a")); /* push table */
1061 lua_pushobject(lua_getglobal("i")); /* push index */
1062 result = lua_getsubscript();
1063\end{verbatim}
1064This code is incorrect because
1065the call \verb|lua_getglobal("i")| modifies the stack,
1066and invalidates the previous pushed value.
1067A correct solution could be:
1068\begin{verbatim}
1069 lua_Object result;
1070 lua_Object index = lua_getglobal("i");
1071 lua_pushobject(lua_getglobal("a")); /* push table */
1072 lua_pushobject(index); /* push index */
1073 result = lua_getsubscript();
1074\end{verbatim}
1075 1393
1076\subsection{Calling Lua Functions} 1394\subsection{Calling Lua Functions}
1077Functions defined in Lua by a chunk executed with 1395Functions defined in Lua by a chunk executed with
@@ -1079,39 +1397,38 @@ Functions defined in Lua by a chunk executed with
1079This is done using the following protocol: 1397This is done using the following protocol:
1080first, the arguments to the function are pushed onto C2lua 1398first, the arguments to the function are pushed onto C2lua
1081\see{pushing}, in direct order, i.e., the first argument is pushed first. 1399\see{pushing}, in direct order, i.e., the first argument is pushed first.
1082Again, it is important to emphasize that, during this phase,
1083most other Lua functions cannot be called.
1084 1400
1085Then, the function is called using 1401Then, the function is called using
1086\Deffunc{lua_call}\Deffunc{lua_callfunction} 1402\Deffunc{lua_callfunction}
1087\begin{verbatim}
1088int lua_call (char *functionname);
1089\end{verbatim}
1090or
1091\begin{verbatim} 1403\begin{verbatim}
1092int lua_callfunction (lua_Object function); 1404int lua_callfunction (lua_Object function);
1093\end{verbatim} 1405\end{verbatim}
1094Both functions return an error code: 1406This function returns an error code:
10950, in case of success; non zero, in case of errors. 14070, in case of success; non zero, in case of errors.
1096Finally, the results (a Lua function may return many values) 1408Finally, the results (a Lua function may return many values)
1097are returned in structure lua2C, 1409are returned in structure lua2C,
1098and can be retrieved with the macro \verb|lua_getresult|, 1410and can be retrieved with the macro \verb|lua_getresult|,
1099\Deffunc{lua_getresult} 1411\Deffunc{lua_getresult}
1100which is just another name to the function \verb|lua_lua2C|. 1412which is just another name to the function \verb|lua_lua2C|.
1413Notice that the function \verb|lua_callfunction|
1414pops all elements from the C2lua stack.
1101 1415
1102The following example shows how a C program may call the 1416The following example shows how a C program may do the
1103\verb|strsub| function in Lua to extract a piece of a string: 1417equivalent to the Lua code:
1104\begin{verbatim} 1418\begin{verbatim}
1105 /* assume that 's' and 'r' are strings (char *), 'i' and 'j' integers */ 1419 a = f(t.x, 4)
1106 lua_pushstring(s); /* 1st argument */ 1420\end{verbatim}
1107 lua_pushnumber(i); /* 2nd argument */ 1421\begin{verbatim}
1108 lua_pushnumber(j); /* 3rd argument */ 1422 lua_pushobject(lua_getglobal("t")); /* push value of global 't' */
1109 lua_call("strsub"); /* call Lua function */ 1423 lua_pushstring("x"); /* push the string 'x' */
1110 r = lua_getstring(lua_getresult(1)); /* r = strsub(s, i, j) */ 1424 lua_pushobject(lua_gettable()); /* push result of t.x (= t['x']) */
1425 lua_pushnumber(4); /* 2nd argument */
1426 lua_callfunction(lua_getglobal("f")); /* call Lua function */
1427 lua_pushobject(lua_getresult(1)); /* push first result of the call */
1428 lua_setglobal("a"); /* sets global variable 'a' */
1111\end{verbatim} 1429\end{verbatim}
1112 1430
1113Two special Lua functions have exclusive interfaces: 1431Some special Lua functions have exclusive interfaces.
1114\verb|error| and \verb|setfallback|.
1115A C function can generate a Lua error calling the function 1432A C function can generate a Lua error calling the function
1116\Deffunc{lua_error} 1433\Deffunc{lua_error}
1117\begin{verbatim} 1434\begin{verbatim}
@@ -1122,19 +1439,31 @@ If the C function has been called from Lua,
1122then the corresponding Lua execution terminates, 1439then the corresponding Lua execution terminates,
1123as if an error had occurred inside Lua code. 1440as if an error had occurred inside Lua code.
1124Otherwise, the whole program terminates with a call to \verb|exit(1)|. 1441Otherwise, the whole program terminates with a call to \verb|exit(1)|.
1125%%LHF: proponho lua_error(char* m, int rc), gerando exit(rc)
1126 1442
1127Fallbacks can be changed with: 1443The error handler method \see{error} can be changed with:
1128\Deffunc{lua_setfallback} 1444\Deffunc{lua_seterrormethod}
1445\begin{verbatim}
1446lua_Object lua_seterrormethod (lua_CFunction method);
1447\end{verbatim}
1448This function returns a \verb|lua_Object|,
1449which is the old error method value.
1450
1451Tag methods can be changed with:
1452\Deffunc{lua_settagmethod}
1129\begin{verbatim} 1453\begin{verbatim}
1130lua_Object lua_setfallback (char *name, lua_CFunction fallback); 1454lua_Object lua_settagmethod (int tag, char *event, lua_CFunction method);
1131\end{verbatim} 1455\end{verbatim}
1132The first parameter is the fallback name \see{fallback}, 1456The first parameter is the tag,
1133and the second is a CFunction to be used as the new fallback. 1457the second is the event name \see{tag-method},
1458and the third is a CFunction to be used as the new method.
1134This function returns a \verb|lua_Object|, 1459This function returns a \verb|lua_Object|,
1135which is the old fallback value, 1460which is the old tag method value.
1136or \nil\ on failure (invalid fallback name). 1461To only get the current value of a tag method,
1137This old value can be used for chaining fallbacks. 1462there is the function
1463\Deffunc{lua_gettagmethod}
1464\begin{verbatim}
1465lua_Object lua_gettagmethod (int tag, char *event);
1466\end{verbatim}
1138 1467
1139 1468
1140\subsection{C Functions} \label{LuacallC} 1469\subsection{C Functions} \label{LuacallC}
@@ -1168,39 +1497,7 @@ in direct order \see{valuesCLua}.
1168Like a Lua function, a C function called by Lua can also return 1497Like a Lua function, a C function called by Lua can also return
1169many results. 1498many results.
1170 1499
1171As an example, 1500For some examples, see files \verb|strlib.c|,
1172the code below shows a CFunction to compute the maximum of
1173a variable number of arguments:
1174\begin{verbatim}
1175void math_max (void)
1176{
1177 int i=1; /* argument count */
1178 double d, dmax;
1179 lua_Object o;
1180 /* the function must get at least one argument */
1181 if ((o = lua_getparam(i++)) == LUA_NOOBJECT)
1182 lua_error("too few arguments to function `max'");
1183 /* and this argument must be a number */
1184 if (!lua_isnumber(o))
1185 lua_error("incorrect argument to function `max'");
1186 dmax = lua_getnumber(o);
1187 /* loops until there is no more arguments */
1188 while ((o = lua_getparam(i++)) != LUA_NOOBJECT) {
1189 if (!lua_isnumber(o))
1190 lua_error("incorrect argument to function `max'");
1191 d = lua_getnumber(o);
1192 if (d > dmax) dmax = d;
1193 }
1194 /* push the result to be returned */
1195 lua_pushnumber(dmax);
1196}
1197\end{verbatim}
1198To be available in Lua, this function must be registered:
1199\begin{verbatim}
1200lua_register ("max", math_max);
1201\end{verbatim}
1202
1203For more examples, see files \verb|strlib.c|,
1204\verb|iolib.c| and \verb|mathlib.c| in Lua distribution. 1501\verb|iolib.c| and \verb|mathlib.c| in Lua distribution.
1205 1502
1206\subsection{References to Lua Objects} 1503\subsection{References to Lua Objects}
@@ -1211,11 +1508,10 @@ outside block boundaries,
1211it must create a \Def{reference} to the object. 1508it must create a \Def{reference} to the object.
1212The routines to manipulate references are the following: 1509The routines to manipulate references are the following:
1213\Deffunc{lua_ref}\Deffunc{lua_getref} 1510\Deffunc{lua_ref}\Deffunc{lua_getref}
1214\Deffunc{lua_pushref}\Deffunc{lua_unref} 1511\Deffunc{lua_unref}
1215\begin{verbatim} 1512\begin{verbatim}
1216int lua_ref (int lock); 1513int lua_ref (int lock);
1217lua_Object lua_getref (int ref); 1514lua_Object lua_getref (int ref);
1218void lua_pushref (int ref);
1219void lua_unref (int ref); 1515void lua_unref (int ref);
1220\end{verbatim} 1516\end{verbatim}
1221The function \verb|lua_ref| creates a reference 1517The function \verb|lua_ref| creates a reference
@@ -1226,19 +1522,13 @@ this means the object will not be garbage collected.
1226Notice that an unlocked reference may be garbage collected. 1522Notice that an unlocked reference may be garbage collected.
1227Whenever the referenced object is needed, 1523Whenever the referenced object is needed,
1228a call to \verb|lua_getref| 1524a call to \verb|lua_getref|
1229returns a handle to it, 1525returns a handle to it;
1230whereas \verb|lua_pushref| pushes the object on the stack. 1526if the object has been collected,
1231If the object has been collected, 1527\verb|lua_getref| returns \verb|LUA_NOOBJECT|.
1232then \verb|lua_getref| returns \verb|LUA_NOOBJECT|,
1233and \verb|lua_pushobject| issues an error.
1234 1528
1235When a reference is no longer needed, 1529When a reference is no longer needed,
1236it can be freed with a call to \verb|lua_unref|. 1530it can be freed with a call to \verb|lua_unref|.
1237 1531
1238The function \verb|lua_pushref| does not corrupt the
1239structures lua2C and C2lua, and therefore is safe to
1240be called when pushing parameters onto C2lua.
1241
1242 1532
1243 1533
1244\section{Predefined Functions and Libraries} 1534\section{Predefined Functions and Libraries}
@@ -1266,7 +1556,44 @@ the host program must call the functions
1266declared in \verb|lualib.h|. 1556declared in \verb|lualib.h|.
1267 1557
1268 1558
1269\subsection{Predefined Functions} 1559\subsection{Predefined Functions} \label{predefined}
1560
1561\subsection*{\ff{\tt call (func, arg, [retmode])}}\Deffunc{call}
1562This function calls function \verb|func| with
1563the arguments given by the table \verb|arg|.
1564The call is equivalent to
1565\begin{verbatim}
1566 func(arg[1], arg[2], ..., arg[arg.n])
1567\end{verbatim}
1568If \verb|arg.n| is not defined,
1569Lua gets the arguments from \verb|arg[1]| until the first nil value.
1570
1571If \verb|retmode| is equal to \verb|"plain"| or is absent,
1572all results from \verb|func| are just returned by the call.
1573If \verb|retmode| is equal to \verb|"pack"|,
1574the results are {\em packed\/} in a single table.\index{packed results}
1575That is, \verb|call| returns just one table.
1576At index \verb|n| the table has the total number of results
1577from the call;
1578the first result is at index 1, etc.
1579
1580For instance, the following calls produce the following results:
1581\begin{verbatim}
1582a = call(sin, {5}) -- a = 0.0871557 = sin(5)
1583a = call(max, {1,4,5; n=2}) -- a = 4 (only 1 and 4 are arguments)
1584t = {x=1}
1585a = call(next, {t,nil;n=2}, "pack") -- a={"x", 1; n=2}
1586\end{verbatim}
1587
1588\subsection*{\ff{\tt callgc ([nextgc])}}\Deffunc{callgc}
1589Forces a garbage collection cicle.
1590Returns the number of objects collected.
1591An optional argument, \verb|nextgc|, is a number that
1592makes the next cicle occur when that number of new
1593objects have been created.
1594If absent, Lua uses an adaptative algorithm to set
1595this limit.
1596\verb|nextgc| is simply an interface to \verb|lua_collectgarbage|.
1270 1597
1271\subsubsection*{\ff{\tt dofile (filename)}}\Deffunc{dofile} 1598\subsubsection*{\ff{\tt dofile (filename)}}\Deffunc{dofile}
1272This function receives a file name, 1599This function receives a file name,
@@ -1287,6 +1614,10 @@ Otherwise, it returns the values returned by the chunk,
1287or a non \nil\ value if the chunk returns no values. 1614or a non \nil\ value if the chunk returns no values.
1288\verb|dostring| is simply an interface to \verb|lua_dostring|. 1615\verb|dostring| is simply an interface to \verb|lua_dostring|.
1289 1616
1617\subsubsection*{\ff{\tt newtag ()}}\Deffunc{newtag}\label{pdf-newtag}
1618Returns a new tag.
1619\verb|newtag| is simply an interface to \verb|lua_newtag|.
1620
1290\subsubsection*{\ff{\tt next (table, index)}}\Deffunc{next} 1621\subsubsection*{\ff{\tt next (table, index)}}\Deffunc{next}
1291This function allows a program to traverse all fields of a table. 1622This function allows a program to traverse all fields of a table.
1292Its first argument is a table and its second argument 1623Its first argument is a table and its second argument
@@ -1351,17 +1682,21 @@ The possible results of this function are
1351\verb|"number"|, 1682\verb|"number"|,
1352\verb|"string"|, 1683\verb|"string"|,
1353\verb|"table"|, 1684\verb|"table"|,
1354\verb|"function"| (returned both for C functions and Lua functions), 1685\verb|"function"|,
1355and \verb|"userdata"|. 1686and \verb|"userdata"|.
1356 1687
1357Besides this string, the function returns a second result,
1358which is the \Def{tag} of the value.
1359This tag can be used to distinguish between user
1360data with different tags,
1361and between C functions and Lua functions.
1362
1363\verb|type| is simply an interface to \verb|lua_type|. 1688\verb|type| is simply an interface to \verb|lua_type|.
1364 1689
1690\subsubsection*{\ff{\tt tag (v)}}\Deffunc{tag}
1691This function allows Lua to test the tag of a value \see{TypesSec}.
1692It receives one argument, and returns its tag (a number).
1693
1694\subsubsection*{\ff{\tt settag (o, tag)}}\Deffunc{settag}
1695This function sets the tag of a given object \see{TypesSec}.
1696The object \verb|o| must be a userdata or a table.
1697\verb|tag| must be a value created with \verb|newtag|
1698\see{pdf-newtag}.
1699
1365\subsubsection*{\ff{\tt assert (v)}}\Deffunc{assert} 1700\subsubsection*{\ff{\tt assert (v)}}\Deffunc{assert}
1366This function issues an {\em ``assertion failed!''} error 1701This function issues an {\em ``assertion failed!''} error
1367when its argument is \nil. 1702when its argument is \nil.
@@ -1369,27 +1704,63 @@ when its argument is \nil.
1369\subsubsection*{\ff{\tt error (message)}}\Deffunc{error}\label{pdf-error} 1704\subsubsection*{\ff{\tt error (message)}}\Deffunc{error}\label{pdf-error}
1370This function issues an error message and terminates 1705This function issues an error message and terminates
1371the last called function from the library 1706the last called function from the library
1372(\verb|lua_dofile|, \verb|lua_dostring|, \ldots). 1707(\verb|lua_dofile|, \verb|lua_dostring|, or \verb|lua_callfunction|).
1373It never returns. 1708It never returns.
1374\verb|error| is simply an interface to \verb|lua_error|. 1709\verb|error| is simply an interface to \verb|lua_error|.
1375 1710
1376\subsubsection*{\ff{\tt setglobal (name, value)}}\Deffunc{setglobal} 1711\subsubsection*{\ff{\tt rawgettable (table, index)}}\Deffunc{rawgettable}
1712Gets the real value of \verb|table[index]|,
1713without invoking any tag method.
1714\verb|table| must be a table,
1715and \verb|index| is any value different from \nil.
1716
1717\subsubsection*{\ff{\tt rawsettable (table, index, value)}}\Deffunc{rawsettable}
1718Sets the real value \verb|table[index]=value|,
1719without invoking any tag method.
1720\verb|table| must be a table,
1721\verb|index| is any value different from \nil,
1722and \verb|value| is any Lua value.
1723
1724\subsubsection*{\ff{\tt rawsetglobal (name, value)}}\Deffunc{rawsetglobal}
1377This function assigns the given value to a global variable. 1725This function assigns the given value to a global variable.
1378The string \verb|name| does not need to be a syntactically valid variable name. 1726The string \verb|name| does not need to be a syntactically valid variable name.
1379Therefore, this function can set global variables with strange names like 1727Therefore, this function can set global variables with strange names like
1380\verb|`m v 1'| or \verb|34|. 1728\verb|`m v 1'| or \verb|34|.
1381It returns the value of its second argument. 1729It returns the value of its second argument.
1382\verb|setglobal| is simply an interface to \verb|lua_storeglobal|.
1383 1730
1384\subsubsection*{\ff{\tt getglobal (name)}}\Deffunc{getglobal} 1731\subsubsection*{\ff{\tt setglobal (name, value)}}\Deffunc{setglobal}
1732This function assigns the given value to a global variable,
1733or calls a tag method.
1734Its full semantics is explained in \See{tag-method}.
1735
1736\subsubsection*{\ff{\tt rawgetglobal (name)}}\Deffunc{rawgetglobal}
1385This function retrieves the value of a global variable. 1737This function retrieves the value of a global variable.
1386The string \verb|name| does not need to be a syntactically valid variable name. 1738The string \verb|name| does not need to be a
1739syntactically valid variable name.
1740
1741\subsubsection*{\ff{\tt getglobal (name)}}\Deffunc{getglobal}
1742This function retrieves the value of a global variable,
1743or calls a tag method.
1744Its full semantics is explained in \See{tag-method}.
1745
1746\subsubsection*{\ff{\tt seterrormethod (newmethod)}}
1747Sets the error handler \see{error}.
1748\verb|newmethod| must be a function or \nil,
1749in which case the error handler does nothing.
1750Returns the old handler.
1751
1752\subsubsection*{\ff{\tt settagmethod (tag, event, newmethod)}}
1753\Deffunc{settagmethod}
1754This function sets a new tag method to the given pair $<tag, event>$.
1755It returns the old method.
1756If \verb|newmethod| is \nil,
1757it restores the default behavior for the given event.
1758
1759\subsubsection*{\ff{\tt gettagmethod (tag, event)}}
1760\Deffunc{gettagmethod}
1761This function returns the current tag method
1762for a given pair $<tag, event>$.
1387 1763
1388\subsubsection*{\ff{\tt setfallback (fallbackname, newfallback)}}
1389\Deffunc{setfallback}
1390This function sets a new fallback function to the given fallback.
1391It returns the old fallback function.
1392\verb|setfallback| is simply an interface to \verb|lua_setfallback|.
1393 1764
1394\subsection{String Manipulation} 1765\subsection{String Manipulation}
1395This library provides generic functions for string manipulation, 1766This library provides generic functions for string manipulation,
@@ -1418,12 +1789,18 @@ Receives a string and returns its length.
1418\subsubsection*{\ff{\tt strsub (s, i [, j])}}\Deffunc{strsub} 1789\subsubsection*{\ff{\tt strsub (s, i [, j])}}\Deffunc{strsub}
1419Returns another string, which is a substring of \verb|s|, 1790Returns another string, which is a substring of \verb|s|,
1420starting at \verb|i| and runing until \verb|j|. 1791starting at \verb|i| and runing until \verb|j|.
1421If \verb|j| is absent, 1792If \verb|i| or \verb|j| are negative,
1422it is assumed to be equal to the length of \verb|s|. 1793they are replaced by the length of the string minus their
1423In particular, the call \verb|strsub(s,1,j)| returns a prefix of \verb|s| 1794absolute value plus 1.
1795Therefore, -1 points to the last character of \verb|s|
1796and -2 to the previous one.
1797If \verb|j| is absent, it is assumed to be equal to -1
1798(which is the same as the string length).
1799In particular,
1800the call \verb|strsub(s,1,j)| returns a prefix of \verb|s|
1424with length \verb|j|, 1801with length \verb|j|,
1425whereas the call \verb|strsub(s,i)| returns a suffix of \verb|s|, 1802and the call \verb|strsub(s, -i)| returns a suffix of \verb|s|
1426starting at \verb|i|. 1803with length \verb|i|.
1427 1804
1428\subsubsection*{\ff{\tt strlower (s)}}\Deffunc{strlower} 1805\subsubsection*{\ff{\tt strlower (s)}}\Deffunc{strlower}
1429Receives a string and returns a copy of that string with all 1806Receives a string and returns a copy of that string with all
@@ -1612,7 +1989,7 @@ and the part matching \verb|%s*| has number 3.
1612\subsection{Mathematical Functions} \label{mathlib} 1989\subsection{Mathematical Functions} \label{mathlib}
1613 1990
1614This library is an interface to some functions of the standard C math library. 1991This library is an interface to some functions of the standard C math library.
1615In addition, it registers a fallback for the binary operator \verb|^| that, 1992In addition, it registers a tag method for the binary operator \verb|^| that
1616returns \M{x^y} when applied to numbers \verb|x^y|. 1993returns \M{x^y} when applied to numbers \verb|x^y|.
1617 1994
1618The library provides the following functions: 1995The library provides the following functions:
@@ -1892,13 +2269,13 @@ they do not have a fixed name:
1892Some functions may be the value of many global variables, 2269Some functions may be the value of many global variables,
1893while others may be stored only in a table field. 2270while others may be stored only in a table field.
1894Function \verb|lua_getobjname| first checks whether the given 2271Function \verb|lua_getobjname| first checks whether the given
1895function is a fallback. 2272function is a tag method.
1896If so, it returns the string \verb|"fallback"|, 2273If so, it returns the string \verb|"tag-method"|,
1897and \verb|name| is set to point to the fallback name. 2274and \verb|name| is set to point to the event name.
1898Otherwise, if the given function is the value of a global variable, 2275Otherwise, if the given function is the value of a global variable,
1899then \verb|lua_getobjname| returns the string \verb|"global"|, 2276then \verb|lua_getobjname| returns the string \verb|"global"|,
1900and \verb|name| points to the variable name. 2277and \verb|name| points to the variable name.
1901If the given function is neither a fallback nor a global variable, 2278If the given function is neither a tag method nor a global variable,
1902then \verb|lua_getobjname| returns the empty string, 2279then \verb|lua_getobjname| returns the empty string,
1903and \verb|name| is set to \verb|NULL|. 2280and \verb|name| is set to \verb|NULL|.
1904 2281
@@ -2131,3 +2508,9 @@ Special care should be taken with macros like
2131 2508
2132 2509
2133\end{document} 2510\end{document}
2511
2512
2513
2514
2515
2516