diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-06-02 17:37:04 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-06-02 17:37:04 -0300 |
commit | 02a6891939895129bc968364a5beda73331005e7 (patch) | |
tree | 61417ec2d6e1eb38542e04fba31c482c6284280b /manual.tex | |
parent | 741c6f50067bfb0f351967c321da56805191f302 (diff) | |
download | lua-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.tex | 82 |
1 files changed, 66 insertions, 16 deletions
@@ -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 | |||
1291 | with the Lua library. | 1291 | with the Lua library. |
1292 | The API functions can be classified in the following categories: | 1292 | The 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: | |||
1301 | All API functions and related types and constants | 1302 | All API functions and related types and constants |
1302 | are declared in the header file \verb|lua.h|. | 1303 | are declared in the header file \verb|lua.h|. |
1303 | 1304 | ||
1305 | \subsection{Managing States} | ||
1306 | The whole state of the Lua interpreter | ||
1307 | (global variables, stack, tag methods, etc) | ||
1308 | is stored in a dynamic structure pointed by\Deffunc{lua_state} | ||
1309 | \begin{verbatim} | ||
1310 | typedef struct lua_State lua_State; | ||
1311 | extern lua_State *lua_state; | ||
1312 | \end{verbatim} | ||
1313 | |||
1304 | Before calling any API function, | 1314 | Before calling any API function, |
1305 | the library must be initalizated. | 1315 | this state must be initialized. |
1306 | This is done by calling:\Deffunc{lua_open} | 1316 | This is done by calling\Deffunc{lua_open} |
1307 | \begin{verbatim} | 1317 | \begin{verbatim} |
1308 | void lua_open (void); | 1318 | void lua_open (void); |
1309 | \end{verbatim} | 1319 | \end{verbatim} |
1310 | This function allocates and initializes some internal structures, | 1320 | This function allocates and initializes some internal structures, |
1311 | and defines all pre-defined functions of Lua. | 1321 | and defines all pre-defined functions of Lua. |
1312 | If the library is already opened, | 1322 | If \verb|lua_state| is already different from \verb|NULL|, |
1313 | this function has no effect. | 1323 | this function has no effect; |
1324 | therefore, it is safe to call this function multiple times. | ||
1314 | All standard libraries call \verb|lua_open| when they are opened. | 1325 | All standard libraries call \verb|lua_open| when they are opened. |
1315 | 1326 | ||
1316 | If necessary, the library may be closed:\Deffunc{lua_close} | 1327 | Function \verb|lua_setstate| is used to change the current state |
1328 | of Lua:\Deffunc{lua_setstate} | ||
1329 | \begin{verbatim} | ||
1330 | lua_State *lua_setstate (lua_State *st); | ||
1331 | \end{verbatim} | ||
1332 | It sets \verb|lua_state| to \verb|st| and returns the old state. | ||
1333 | |||
1334 | Multiple, independent, states may be created. | ||
1335 | For that, you must set \verb|lua_state| back to \verb|NULL| before | ||
1336 | calling \verb|lua_open|. | ||
1337 | An easy way to do that is defining an auxiliary function: | ||
1338 | \begin{verbatim} | ||
1339 | lua_State *lua_newstate (void) { | ||
1340 | lua_State *old = lua_setstate(NULL); | ||
1341 | lua_open(); | ||
1342 | return lua_setstate(old); | ||
1343 | } | ||
1344 | \end{verbatim} | ||
1345 | This function creates a new state without changing the current state | ||
1346 | of the interpreter. | ||
1347 | Notice that any new state is built with all predefined functions; | ||
1348 | any additional library (such as the standard libraries) must be | ||
1349 | explicitly open in the new state, if needed. | ||
1350 | |||
1351 | If necessary, a state may be released:\Deffunc{lua_close} | ||
1317 | \begin{verbatim} | 1352 | \begin{verbatim} |
1318 | void lua_close (void); | 1353 | void lua_close (void); |
1319 | \end{verbatim} | 1354 | \end{verbatim} |
1320 | This function destroys all objects in the Lua environment | 1355 | This 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), |
1322 | and then frees all dynamic memory used by the library. | 1357 | frees all dynamic memory used by the state, |
1358 | and then sets \verb|lua_state| to \verb|NULL|. | ||
1323 | Usually, there is no need to call this function, | 1359 | Usually, there is no need to call this function, |
1324 | since these resources are naturally released when the program ends. | 1360 | since these resources are naturally released when the program ends. |
1325 | If the library is already closed, | 1361 | If \verb|lua_state| is already \verb|NULL|, |
1326 | this function has no effect. | 1362 | this function has no effect. |
1327 | 1363 | ||
1364 | If you are using multiple states, | ||
1365 | you may find useful the following function, | ||
1366 | which releases a given state: | ||
1367 | \begin{verbatim} | ||
1368 | void 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} |
1329 | Because Lua has no static type system, | 1376 | Because Lua has no static type system, |
1330 | all values passed between Lua and C have type | 1377 | all 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} |
1523 | A host program can execute Lua chunks written in a file or in a string | 1570 | A host program can execute Lua chunks written in a file or in a string |
1524 | using the following functions: | 1571 | using 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} |
1527 | int lua_dofile (char *filename); | 1574 | int lua_dofile (char *filename); |
1528 | int lua_dostring (char *string); | 1575 | int lua_dostring (char *string); |
1576 | int lua_dobuffer (char *buff, int size); | ||
1529 | \end{verbatim} | 1577 | \end{verbatim} |
1530 | Both functions return an error code: | 1578 | All these functions return an error code: |
1531 | 0, in case of success; non zero, in case of errors. | 1579 | 0, in case of success; non zero, in case of errors. |
1532 | More specifically, \verb|lua_dofile| returns 2 if for any reason | 1580 | More specifically, \verb|lua_dofile| returns 2 if for any reason |
1533 | it could not open the file. | 1581 | it could not open the file. |
@@ -1536,6 +1584,8 @@ executes the \verb|stdin| stream. | |||
1536 | Function \verb|lua_dofile| is also able to execute pre-compiled chunks. | 1584 | Function \verb|lua_dofile| is also able to execute pre-compiled chunks. |
1537 | It automatically detects whether the file is text or binary, | 1585 | It automatically detects whether the file is text or binary, |
1538 | and loads it accordingly (see program \IndexVerb{luac}). | 1586 | and loads it accordingly (see program \IndexVerb{luac}). |
1587 | Function \verb|lua_dostring| executes only source code, | ||
1588 | and function \verb|lua_dobuffer| executes only pre-compiled chunks. | ||
1539 | 1589 | ||
1540 | These functions return, in structure lua2C, | 1590 | These functions return, in structure lua2C, |
1541 | any values eventually returned by the chunks. | 1591 | any 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} |
1977 | This function receives any number of arguments, | 2027 | This function receives any number of arguments, |
1978 | and prints their values in a reasonable format. | 2028 | and prints their values using the strings returned by \verb|tostring|. |
1979 | This function is not intended for formatted output, | 2029 | This function is not intended for formatted output, |
1980 | but as a quick way to show a value, | 2030 | but as a quick way to show a value, |
1981 | for instance for error messages or debugging. | 2031 | for instance for error messages or debugging. |
@@ -2177,7 +2227,7 @@ letter depends on the current locale. | |||
2177 | Returns a string that is the concatenation of \verb|n| copies of | 2227 | Returns a string that is the concatenation of \verb|n| copies of |
2178 | the string \verb|s|. | 2228 | the string \verb|s|. |
2179 | 2229 | ||
2180 | \subsubsection*{\ff \T{ascii (s [, i])}}\Deffunc{ascii} | 2230 | \subsubsection*{\ff \T{strbyte (s [, i])}}\Deffunc{strbyte} |
2181 | Returns the internal numerical code of the character \verb|s[i]|. | 2231 | Returns the internal numerical code of the character \verb|s[i]|. |
2182 | If \verb|i| is absent, then it is assumed to be 1. | 2232 | If \verb|i| is absent, then it is assumed to be 1. |
2183 | If \verb|i| is negative, | 2233 | If \verb|i| is negative, |
@@ -2185,10 +2235,10 @@ it is replaced by the length of the string minus its | |||
2185 | absolute value plus 1. | 2235 | absolute value plus 1. |
2186 | Therefore, \M{-1} points to the last character of \verb|s|. | 2236 | Therefore, \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} |
2189 | Receives 0 or more integers. | 2239 | Receives 0 or more integers. |
2190 | Returns a string with length equal to the number of arguments, | 2240 | Returns a string with length equal to the number of arguments, |
2191 | wherein each character has ascii value equal | 2241 | wherein each character has the internal numerical code equal |
2192 | to its correspondent argument. | 2242 | to 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} |