diff options
Diffstat (limited to 'manual.tex')
-rw-r--r-- | manual.tex | 75 |
1 files changed, 46 insertions, 29 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.14 1996/03/20 18:44:02 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.15 1996/04/01 14:36:35 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentstyle[fullpage,11pt,bnf]{article} | 3 | \documentstyle[fullpage,11pt,bnf]{article} |
4 | 4 | ||
@@ -34,7 +34,7 @@ Waldemar Celes Filho | |||
34 | \tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio | 34 | \tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio |
35 | } | 35 | } |
36 | 36 | ||
37 | \date{\small \verb$Date: 1996/03/20 18:44:02 $} | 37 | \date{\small \verb$Date: 1996/04/01 14:36:35 $} |
38 | 38 | ||
39 | \maketitle | 39 | \maketitle |
40 | 40 | ||
@@ -739,7 +739,7 @@ The API functions can be classified in the following categories: | |||
739 | \item manipulating (reading and writing) Lua objects; | 739 | \item manipulating (reading and writing) Lua objects; |
740 | \item calling Lua functions; | 740 | \item calling Lua functions; |
741 | \item C functions to be called by Lua; | 741 | \item C functions to be called by Lua; |
742 | \item locking Lua Objects. | 742 | \item references to Lua Objects. |
743 | \end{enumerate} | 743 | \end{enumerate} |
744 | All API functions are declared in the file \verb'lua.h'. | 744 | All API functions are declared in the file \verb'lua.h'. |
745 | 745 | ||
@@ -1069,30 +1069,39 @@ many results. | |||
1069 | Section~\ref{exCFunction} presents an example of a CFunction. | 1069 | Section~\ref{exCFunction} presents an example of a CFunction. |
1070 | 1070 | ||
1071 | 1071 | ||
1072 | \subsection{Locking Lua Objects} | 1072 | \subsection{References to Lua Objects} |
1073 | 1073 | ||
1074 | As already noted, \verb'lua_Object's are volatile. | 1074 | As already noted, \verb'lua_Object's are volatile. |
1075 | If the C code needs to keep a \verb'lua_Object' | 1075 | If the C code needs to keep a \verb'lua_Object' |
1076 | outside block boundaries, | 1076 | outside block boundaries, |
1077 | it has to {\em lock} the object. | 1077 | it must create a \Def{reference} to the object. |
1078 | The routines to manipulate locking are the following: | 1078 | The routines to manipulate references are the following: |
1079 | \Deffunc{lua_lock}\Deffunc{lua_getlocked} | 1079 | \Deffunc{lua_ref}\Deffunc{lua_getref} |
1080 | \Deffunc{lua_pushlocked}\Deffunc{lua_unlock} | 1080 | \Deffunc{lua_pushref}\Deffunc{lua_unref} |
1081 | \begin{verbatim} | 1081 | \begin{verbatim} |
1082 | int lua_lock (void); | 1082 | typedef int lua_Reference; |
1083 | lua_Object lua_getlocked (int ref); | 1083 | |
1084 | void lua_pushlocked (int ref); | 1084 | lua_Reference lua_ref (int lock); |
1085 | void lua_unlock (int ref); | 1085 | lua_Object lua_getref (lua_Reference ref); |
1086 | \end{verbatim} | 1086 | void lua_pushref (lua_Reference ref); |
1087 | The function \verb'lua_lock' locks the object | 1087 | void lua_unref (lua_Reference ref); |
1088 | which is on the top of the stack, | 1088 | \end{verbatim} |
1089 | and returns a reference to it. | 1089 | The function \verb'lua_ref' creates a reference |
1090 | Whenever the locked object is needed, | 1090 | to the object which is on the top of the stack, |
1091 | a call to \verb'lua_getlocked' | 1091 | and returns this reference. |
1092 | If \verb'lock' is true, the object is {\em locked}: | ||
1093 | that means the object will not be garbage collected. | ||
1094 | Notice that an unlocked reference may be garbage collected. | ||
1095 | Whenever the referenced object is needed, | ||
1096 | a call to \verb'lua_getref' | ||
1092 | returns a handle to it, | 1097 | returns a handle to it, |
1093 | while \verb'lua_pushlocked' pushes the handle on the stack. | 1098 | while \verb'lua_pushref' pushes the object on the stack. |
1094 | When a locked object is no longer needed, | 1099 | If the object has been collected, |
1095 | it can be unlocked with a call to \verb'lua_unlock'. | 1100 | \verb'lua_getref' returns \verb'LUA_NOOBJECT', |
1101 | and \verb'lua_pushobject' issues an error. | ||
1102 | |||
1103 | When a reference is no longer needed, | ||
1104 | it can be freed with a call to \verb'lua_unref'. | ||
1096 | 1105 | ||
1097 | 1106 | ||
1098 | 1107 | ||
@@ -1839,12 +1848,14 @@ as illustrated in Figure~\ref{Cinher}. | |||
1839 | \begin{figure} | 1848 | \begin{figure} |
1840 | \Line | 1849 | \Line |
1841 | \begin{verbatim} | 1850 | \begin{verbatim} |
1842 | int lockedParentName; /* stores the lock index for the string "parent" */ | 1851 | #include "lua.h" |
1843 | int lockedOldIndex; /* previous fallback function */ | 1852 | |
1853 | lua_Reference lockedParentName; /* lock index for the string "parent" */ | ||
1854 | lua_Reference lockedOldIndex; /* previous fallback function */ | ||
1844 | 1855 | ||
1845 | void callOldFallback (lua_Object table, lua_Object index) | 1856 | void callOldFallback (lua_Object table, lua_Object index) |
1846 | { | 1857 | { |
1847 | lua_Object oldIndex = lua_getlocked(lockedOldIndex); | 1858 | lua_Object oldIndex = lua_getref(lockedOldIndex); |
1848 | lua_pushobject(table); | 1859 | lua_pushobject(table); |
1849 | lua_pushobject(index); | 1860 | lua_pushobject(index); |
1850 | lua_callfunction(oldIndex); | 1861 | lua_callfunction(oldIndex); |
@@ -1861,7 +1872,7 @@ void Index (void) | |||
1861 | return; | 1872 | return; |
1862 | } | 1873 | } |
1863 | lua_pushobject(table); | 1874 | lua_pushobject(table); |
1864 | lua_pushlocked(lockedParentName); | 1875 | lua_pushref(lockedParentName); |
1865 | parent = lua_getsubscript(); | 1876 | parent = lua_getsubscript(); |
1866 | if (lua_istable(parent)) | 1877 | if (lua_istable(parent)) |
1867 | { | 1878 | { |
@@ -1880,9 +1891,9 @@ void Index (void) | |||
1880 | This code must be registered with: | 1891 | This code must be registered with: |
1881 | \begin{verbatim} | 1892 | \begin{verbatim} |
1882 | lua_pushstring("parent"); | 1893 | lua_pushstring("parent"); |
1883 | lockedParentName = lua_lock(); | 1894 | lockedParentName = lua_ref(1); |
1884 | lua_pushobject(lua_setfallback("index", Index)); | 1895 | lua_pushobject(lua_setfallback("index", Index)); |
1885 | lockedOldIndex = lua_lock(); | 1896 | lockedOldIndex = lua_ref(1); |
1886 | \end{verbatim} | 1897 | \end{verbatim} |
1887 | Notice how the string \verb'"parent"' is kept | 1898 | Notice how the string \verb'"parent"' is kept |
1888 | locked in Lua for optimal performance. | 1899 | locked in Lua for optimal performance. |
@@ -1892,6 +1903,9 @@ There are many different ways to do object-oriented programming in Lua. | |||
1892 | This section presents one possible way to | 1903 | This section presents one possible way to |
1893 | implement classes, | 1904 | implement classes, |
1894 | using the inheritance mechanism presented above. | 1905 | using the inheritance mechanism presented above. |
1906 | {\em Please notice: the following examples only work | ||
1907 | with the index fallback redefined according to | ||
1908 | Section~\ref{exfallback}}. | ||
1895 | 1909 | ||
1896 | As one could expect, a good way to represent a class is | 1910 | As one could expect, a good way to represent a class is |
1897 | as a table. | 1911 | as a table. |
@@ -2079,9 +2093,12 @@ have been superseded by the new version of function \verb'date'. | |||
2079 | Function \verb'int2str' (from \verb'strlib') has been superseded by new | 2093 | Function \verb'int2str' (from \verb'strlib') has been superseded by new |
2080 | function \verb'format', with parameter \verb'"%c"'. | 2094 | function \verb'format', with parameter \verb'"%c"'. |
2081 | \item | 2095 | \item |
2096 | The lock mechanism has been superseded by the reference mechanism. | ||
2097 | However, \verb-lua.h- provides compatibility macros, | ||
2098 | so there is no need to change programs. | ||
2099 | \item | ||
2082 | API function \verb'lua_pushliteral' now is just a macro to | 2100 | API function \verb'lua_pushliteral' now is just a macro to |
2083 | \verb'lua_pushstring'. | 2101 | \verb'lua_pushstring'. |
2084 | Programmers are encouraged not to use this macro. | ||
2085 | \end{itemize} | 2102 | \end{itemize} |
2086 | 2103 | ||
2087 | \subsection*{Incompatibilities with \Index{version 2.1}} | 2104 | \subsection*{Incompatibilities with \Index{version 2.1}} |