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 | |
parent | 741c6f50067bfb0f351967c321da56805191f302 (diff) | |
download | lua-02a6891939895129bc968364a5beda73331005e7.tar.gz lua-02a6891939895129bc968364a5beda73331005e7.tar.bz2 lua-02a6891939895129bc968364a5beda73331005e7.zip |
API for functions to manipulate global state.
-rw-r--r-- | lbuffer.c | 4 | ||||
-rw-r--r-- | lstate.c | 14 | ||||
-rw-r--r-- | lstate.h | 23 | ||||
-rw-r--r-- | lua.h | 7 | ||||
-rw-r--r-- | manual.tex | 82 |
5 files changed, 98 insertions, 32 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuffer.c,v 1.1 1997/12/23 19:24:36 roberto Exp roberto $ | 2 | ** $Id: lbuffer.c,v 1.2 1998/03/06 16:54:42 roberto Exp roberto $ |
3 | ** Auxiliar functions for building Lua libraries | 3 | ** Auxiliar functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -22,7 +22,7 @@ | |||
22 | 22 | ||
23 | static void Openspace (int size) | 23 | static void Openspace (int size) |
24 | { | 24 | { |
25 | LState *l = L; /* to optimize */ | 25 | lua_State *l = L; /* to optimize */ |
26 | int base = l->Mbuffbase-l->Mbuffer; | 26 | int base = l->Mbuffbase-l->Mbuffer; |
27 | l->Mbuffsize *= 2; | 27 | l->Mbuffsize *= 2; |
28 | if (l->Mbuffnext+size > l->Mbuffsize) /* still not big enough? */ | 28 | if (l->Mbuffnext+size > l->Mbuffsize) /* still not big enough? */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 1.4 1997/12/11 14:48:46 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 1.5 1997/12/17 20:48:58 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -17,13 +17,13 @@ | |||
17 | #include "ltm.h" | 17 | #include "ltm.h" |
18 | 18 | ||
19 | 19 | ||
20 | LState *lua_state = NULL; | 20 | lua_State *lua_state = NULL; |
21 | 21 | ||
22 | 22 | ||
23 | void lua_open (void) | 23 | void lua_open (void) |
24 | { | 24 | { |
25 | if (lua_state) return; | 25 | if (lua_state) return; |
26 | lua_state = luaM_new(LState); | 26 | lua_state = luaM_new(lua_State); |
27 | L->numCblocks = 0; | 27 | L->numCblocks = 0; |
28 | L->Cstack.base = 0; | 28 | L->Cstack.base = 0; |
29 | L->Cstack.lua2C = 0; | 29 | L->Cstack.lua2C = 0; |
@@ -76,3 +76,11 @@ void lua_close (void) | |||
76 | printf("total de memoria: %ld\n", totalmem); | 76 | printf("total de memoria: %ld\n", totalmem); |
77 | #endif | 77 | #endif |
78 | } | 78 | } |
79 | |||
80 | |||
81 | lua_State *lua_setstate (lua_State *st) { | ||
82 | lua_State *old = lua_state; | ||
83 | lua_state = st; | ||
84 | return old; | ||
85 | } | ||
86 | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.h,v 1.7 1998/01/09 14:57:43 roberto Exp $ | 2 | ** $Id: lstate.h,v 1.8 1998/05/27 13:03:40 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -8,6 +8,7 @@ | |||
8 | #define lstate_h | 8 | #define lstate_h |
9 | 9 | ||
10 | #include "lobject.h" | 10 | #include "lobject.h" |
11 | #include "lua.h" | ||
11 | 12 | ||
12 | 13 | ||
13 | #define MAX_C_BLOCKS 10 | 14 | #define MAX_C_BLOCKS 10 |
@@ -44,10 +45,18 @@ struct ref { | |||
44 | }; | 45 | }; |
45 | 46 | ||
46 | 47 | ||
47 | typedef struct LState { | 48 | struct lua_State { |
49 | /* trhead-specific state */ | ||
48 | struct Stack stack; /* Lua stack */ | 50 | struct Stack stack; /* Lua stack */ |
49 | struct C_Lua_Stack Cstack; /* C2lua struct */ | 51 | struct C_Lua_Stack Cstack; /* C2lua struct */ |
50 | void *errorJmp; /* current error recover point */ | 52 | void *errorJmp; /* current error recover point */ |
53 | char *Mbuffer; /* global buffer */ | ||
54 | char *Mbuffbase; /* current first position of Mbuffer */ | ||
55 | int Mbuffsize; /* size of Mbuffer */ | ||
56 | int Mbuffnext; /* next position to fill in Mbuffer */ | ||
57 | struct C_Lua_Stack Cblocks[MAX_C_BLOCKS]; | ||
58 | int numCblocks; /* number of nested Cblocks */ | ||
59 | /* global state */ | ||
51 | TObject errorim; /* error tag method */ | 60 | TObject errorim; /* error tag method */ |
52 | GCnode rootproto; /* list of all prototypes */ | 61 | GCnode rootproto; /* list of all prototypes */ |
53 | GCnode rootcl; /* list of all closures */ | 62 | GCnode rootcl; /* list of all closures */ |
@@ -61,16 +70,10 @@ typedef struct LState { | |||
61 | int refSize; /* size of refArray */ | 70 | int refSize; /* size of refArray */ |
62 | unsigned long GCthreshold; | 71 | unsigned long GCthreshold; |
63 | unsigned long nblocks; /* number of 'blocks' currently allocated */ | 72 | unsigned long nblocks; /* number of 'blocks' currently allocated */ |
64 | char *Mbuffer; /* global buffer */ | 73 | }; |
65 | char *Mbuffbase; /* current first position of Mbuffer */ | ||
66 | int Mbuffsize; /* size of Mbuffer */ | ||
67 | int Mbuffnext; /* next position to fill in Mbuffer */ | ||
68 | struct C_Lua_Stack Cblocks[MAX_C_BLOCKS]; | ||
69 | int numCblocks; /* number of nested Cblocks */ | ||
70 | } LState; | ||
71 | 74 | ||
72 | 75 | ||
73 | extern LState *lua_state; | 76 | extern lua_State *lua_state; |
74 | 77 | ||
75 | 78 | ||
76 | #define L lua_state | 79 | #define L lua_state |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.h,v 1.17 1998/03/06 18:47:42 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.18 1998/05/18 22:26:03 roberto Exp roberto $ |
3 | ** Lua - An Extensible Extension Language | 3 | ** Lua - An Extensible Extension Language |
4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil | 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil |
5 | ** e-mail: lua@tecgraf.puc-rio.br | 5 | ** e-mail: lua@tecgraf.puc-rio.br |
@@ -55,9 +55,12 @@ | |||
55 | typedef void (*lua_CFunction) (void); | 55 | typedef void (*lua_CFunction) (void); |
56 | typedef unsigned int lua_Object; | 56 | typedef unsigned int lua_Object; |
57 | 57 | ||
58 | typedef struct lua_State lua_State; | ||
59 | extern lua_State *lua_state; | ||
58 | 60 | ||
59 | void lua_open (void); | 61 | void lua_open (void); |
60 | void lua_close (void); | 62 | void lua_close (void); |
63 | lua_State *lua_setstate (lua_State *st); | ||
61 | 64 | ||
62 | lua_Object lua_settagmethod (int tag, char *event); /* In: new method */ | 65 | lua_Object lua_settagmethod (int tag, char *event); /* In: new method */ |
63 | lua_Object lua_gettagmethod (int tag, char *event); | 66 | lua_Object lua_gettagmethod (int tag, char *event); |
@@ -70,6 +73,8 @@ void lua_settag (int tag); /* In: object */ | |||
70 | void lua_error (char *s); | 73 | void lua_error (char *s); |
71 | int lua_dofile (char *filename); /* Out: returns */ | 74 | int lua_dofile (char *filename); /* Out: returns */ |
72 | int lua_dostring (char *string); /* Out: returns */ | 75 | int lua_dostring (char *string); /* Out: returns */ |
76 | int lua_dobuffer (char *buff, int size); | ||
77 | /* Out: returns */ | ||
73 | int lua_callfunction (lua_Object f); | 78 | int lua_callfunction (lua_Object f); |
74 | /* In: parameters; Out: returns */ | 79 | /* In: parameters; Out: returns */ |
75 | 80 | ||
@@ -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} |