summaryrefslogtreecommitdiff
path: root/manual.tex
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-06-02 17:37:04 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-06-02 17:37:04 -0300
commit02a6891939895129bc968364a5beda73331005e7 (patch)
tree61417ec2d6e1eb38542e04fba31c482c6284280b /manual.tex
parent741c6f50067bfb0f351967c321da56805191f302 (diff)
downloadlua-02a6891939895129bc968364a5beda73331005e7.tar.gz
lua-02a6891939895129bc968364a5beda73331005e7.tar.bz2
lua-02a6891939895129bc968364a5beda73331005e7.zip
API for functions to manipulate global state.
Diffstat (limited to 'manual.tex')
-rw-r--r--manual.tex82
1 files changed, 66 insertions, 16 deletions
diff --git a/manual.tex b/manual.tex
index 041590c4..27f3df12 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.10 1998/05/20 22:21:35 roberto Exp roberto $ 1% $Id: manual.tex,v 1.11 1998/05/27 19:09:39 roberto Exp roberto $
2 2
3\documentstyle[fullpage,11pt,bnf]{article} 3\documentstyle[fullpage,11pt,bnf]{article}
4 4
@@ -38,7 +38,7 @@ Waldemar Celes
38\tecgraf\ --- Computer Science Department --- PUC-Rio 38\tecgraf\ --- Computer Science Department --- PUC-Rio
39} 39}
40 40
41\date{\small \verb$Date: 1998/05/20 22:21:35 $} 41\date{\small \verb$Date: 1998/05/27 19:09:39 $}
42 42
43\maketitle 43\maketitle
44 44
@@ -1291,6 +1291,7 @@ the set of C functions available to the host program to communicate
1291with the Lua library. 1291with the Lua library.
1292The API functions can be classified in the following categories: 1292The API functions can be classified in the following categories:
1293\begin{enumerate} 1293\begin{enumerate}
1294\item managing states;
1294\item exchanging values between C and Lua; 1295\item exchanging values between C and Lua;
1295\item executing Lua code; 1296\item executing Lua code;
1296\item manipulating (reading and writing) Lua objects; 1297\item manipulating (reading and writing) Lua objects;
@@ -1301,30 +1302,76 @@ The API functions can be classified in the following categories:
1301All API functions and related types and constants 1302All API functions and related types and constants
1302are declared in the header file \verb|lua.h|. 1303are declared in the header file \verb|lua.h|.
1303 1304
1305\subsection{Managing States}
1306The whole state of the Lua interpreter
1307(global variables, stack, tag methods, etc)
1308is stored in a dynamic structure pointed by\Deffunc{lua_state}
1309\begin{verbatim}
1310typedef struct lua_State lua_State;
1311extern lua_State *lua_state;
1312\end{verbatim}
1313
1304Before calling any API function, 1314Before calling any API function,
1305the library must be initalizated. 1315this state must be initialized.
1306This is done by calling:\Deffunc{lua_open} 1316This is done by calling\Deffunc{lua_open}
1307\begin{verbatim} 1317\begin{verbatim}
1308void lua_open (void); 1318void lua_open (void);
1309\end{verbatim} 1319\end{verbatim}
1310This function allocates and initializes some internal structures, 1320This function allocates and initializes some internal structures,
1311and defines all pre-defined functions of Lua. 1321and defines all pre-defined functions of Lua.
1312If the library is already opened, 1322If \verb|lua_state| is already different from \verb|NULL|,
1313this function has no effect. 1323this function has no effect;
1324therefore, it is safe to call this function multiple times.
1314All standard libraries call \verb|lua_open| when they are opened. 1325All standard libraries call \verb|lua_open| when they are opened.
1315 1326
1316If necessary, the library may be closed:\Deffunc{lua_close} 1327Function \verb|lua_setstate| is used to change the current state
1328of Lua:\Deffunc{lua_setstate}
1329\begin{verbatim}
1330lua_State *lua_setstate (lua_State *st);
1331\end{verbatim}
1332It sets \verb|lua_state| to \verb|st| and returns the old state.
1333
1334Multiple, independent, states may be created.
1335For that, you must set \verb|lua_state| back to \verb|NULL| before
1336calling \verb|lua_open|.
1337An easy way to do that is defining an auxiliary function:
1338\begin{verbatim}
1339lua_State *lua_newstate (void) {
1340 lua_State *old = lua_setstate(NULL);
1341 lua_open();
1342 return lua_setstate(old);
1343}
1344\end{verbatim}
1345This function creates a new state without changing the current state
1346of the interpreter.
1347Notice that any new state is built with all predefined functions;
1348any additional library (such as the standard libraries) must be
1349explicitly open in the new state, if needed.
1350
1351If necessary, a state may be released:\Deffunc{lua_close}
1317\begin{verbatim} 1352\begin{verbatim}
1318void lua_close (void); 1353void lua_close (void);
1319\end{verbatim} 1354\end{verbatim}
1320This function destroys all objects in the Lua environment 1355This function destroys all objects in the current Lua environment
1321(calling the correspondent garbage collector tag methods), 1356(calling the correspondent garbage collector tag methods),
1322and then frees all dynamic memory used by the library. 1357frees all dynamic memory used by the state,
1358and then sets \verb|lua_state| to \verb|NULL|.
1323Usually, there is no need to call this function, 1359Usually, there is no need to call this function,
1324since these resources are naturally released when the program ends. 1360since these resources are naturally released when the program ends.
1325If the library is already closed, 1361If \verb|lua_state| is already \verb|NULL|,
1326this function has no effect. 1362this function has no effect.
1327 1363
1364If you are using multiple states,
1365you may find useful the following function,
1366which releases a given state:
1367\begin{verbatim}
1368void lua_freestate (lua_State *st) {
1369 lua_State *old = lua_setstate(st);
1370 lua_close();
1371 if (old != st) lua_setstate(old);
1372}
1373\end{verbatim}
1374
1328\subsection{Exchanging Values between C and Lua} \label{valuesCLua} 1375\subsection{Exchanging Values between C and Lua} \label{valuesCLua}
1329Because Lua has no static type system, 1376Because Lua has no static type system,
1330all values passed between Lua and C have type 1377all values passed between Lua and C have type
@@ -1522,12 +1569,13 @@ The use of explicit nested blocks is strongly encouraged.
1522\subsection{Executing Lua Code} 1569\subsection{Executing Lua Code}
1523A host program can execute Lua chunks written in a file or in a string 1570A host program can execute Lua chunks written in a file or in a string
1524using the following functions: 1571using the following functions:
1525\Deffunc{lua_dofile}\Deffunc{lua_dostring} 1572\Deffunc{lua_dofile}\Deffunc{lua_dostring}\Deffunc{lua_dobuffer}
1526\begin{verbatim} 1573\begin{verbatim}
1527int lua_dofile (char *filename); 1574int lua_dofile (char *filename);
1528int lua_dostring (char *string); 1575int lua_dostring (char *string);
1576int lua_dobuffer (char *buff, int size);
1529\end{verbatim} 1577\end{verbatim}
1530Both functions return an error code: 1578All these functions return an error code:
15310, in case of success; non zero, in case of errors. 15790, in case of success; non zero, in case of errors.
1532More specifically, \verb|lua_dofile| returns 2 if for any reason 1580More specifically, \verb|lua_dofile| returns 2 if for any reason
1533it could not open the file. 1581it could not open the file.
@@ -1536,6 +1584,8 @@ executes the \verb|stdin| stream.
1536Function \verb|lua_dofile| is also able to execute pre-compiled chunks. 1584Function \verb|lua_dofile| is also able to execute pre-compiled chunks.
1537It automatically detects whether the file is text or binary, 1585It automatically detects whether the file is text or binary,
1538and loads it accordingly (see program \IndexVerb{luac}). 1586and loads it accordingly (see program \IndexVerb{luac}).
1587Function \verb|lua_dostring| executes only source code,
1588and function \verb|lua_dobuffer| executes only pre-compiled chunks.
1539 1589
1540These functions return, in structure lua2C, 1590These functions return, in structure lua2C,
1541any values eventually returned by the chunks. 1591any values eventually returned by the chunks.
@@ -1975,7 +2025,7 @@ converts it to a string in a reasonable format.
1975 2025
1976\subsubsection*{\ff \T{print (e1, e2, ...)}}\Deffunc{print} 2026\subsubsection*{\ff \T{print (e1, e2, ...)}}\Deffunc{print}
1977This function receives any number of arguments, 2027This function receives any number of arguments,
1978and prints their values in a reasonable format. 2028and prints their values using the strings returned by \verb|tostring|.
1979This function is not intended for formatted output, 2029This function is not intended for formatted output,
1980but as a quick way to show a value, 2030but as a quick way to show a value,
1981for instance for error messages or debugging. 2031for instance for error messages or debugging.
@@ -2177,7 +2227,7 @@ letter depends on the current locale.
2177Returns a string that is the concatenation of \verb|n| copies of 2227Returns a string that is the concatenation of \verb|n| copies of
2178the string \verb|s|. 2228the string \verb|s|.
2179 2229
2180\subsubsection*{\ff \T{ascii (s [, i])}}\Deffunc{ascii} 2230\subsubsection*{\ff \T{strbyte (s [, i])}}\Deffunc{strbyte}
2181Returns the internal numerical code of the character \verb|s[i]|. 2231Returns the internal numerical code of the character \verb|s[i]|.
2182If \verb|i| is absent, then it is assumed to be 1. 2232If \verb|i| is absent, then it is assumed to be 1.
2183If \verb|i| is negative, 2233If \verb|i| is negative,
@@ -2185,10 +2235,10 @@ it is replaced by the length of the string minus its
2185absolute value plus 1. 2235absolute value plus 1.
2186Therefore, \M{-1} points to the last character of \verb|s|. 2236Therefore, \M{-1} points to the last character of \verb|s|.
2187 2237
2188\subsubsection*{\ff \T{int2str (i1, i2, \ldots)}}\Deffunc{int2str} 2238\subsubsection*{\ff \T{strchar (i1, i2, \ldots)}}\Deffunc{strchar}
2189Receives 0 or more integers. 2239Receives 0 or more integers.
2190Returns a string with length equal to the number of arguments, 2240Returns a string with length equal to the number of arguments,
2191wherein each character has ascii value equal 2241wherein each character has the internal numerical code equal
2192to its correspondent argument. 2242to its correspondent argument.
2193 2243
2194\subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format} 2244\subsubsection*{\ff \T{format (formatstring, e1, e2, \ldots)}}\Deffunc{format}