diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-04-22 15:00:37 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-04-22 15:00:37 -0300 |
commit | 0ef5cf22891c9d34a88ccc5d89eb0ed82b004471 (patch) | |
tree | 1a095acefa978d5a41f32ff7d0fef58a642aa66c | |
parent | fed9408ab51a4be5ff84450ad47d1e0cdaed97bc (diff) | |
download | lua-0ef5cf22891c9d34a88ccc5d89eb0ed82b004471.tar.gz lua-0ef5cf22891c9d34a88ccc5d89eb0ed82b004471.tar.bz2 lua-0ef5cf22891c9d34a88ccc5d89eb0ed82b004471.zip |
lock mechanism seperseded by the REFERENCE mechanism.
-rw-r--r-- | fallback.c | 70 | ||||
-rw-r--r-- | fallback.h | 8 | ||||
-rw-r--r-- | lua.h | 21 | ||||
-rw-r--r-- | luamem.h | 4 | ||||
-rw-r--r-- | manual.tex | 75 | ||||
-rw-r--r-- | mathlib.c | 8 | ||||
-rw-r--r-- | opcode.c | 46 | ||||
-rw-r--r-- | table.c | 21 | ||||
-rw-r--r-- | table.h | 3 |
9 files changed, 163 insertions, 93 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_fallback="$Id: fallback.c,v 1.22 1996/03/19 22:28:37 roberto Exp roberto $"; | 6 | char *rcs_fallback="$Id: fallback.c,v 1.23 1996/03/21 16:31:32 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <string.h> | 9 | #include <string.h> |
@@ -12,6 +12,7 @@ char *rcs_fallback="$Id: fallback.c,v 1.22 1996/03/19 22:28:37 roberto Exp rober | |||
12 | #include "fallback.h" | 12 | #include "fallback.h" |
13 | #include "opcode.h" | 13 | #include "opcode.h" |
14 | #include "lua.h" | 14 | #include "lua.h" |
15 | #include "table.h" | ||
15 | 16 | ||
16 | 17 | ||
17 | static void errorFB (void); | 18 | static void errorFB (void); |
@@ -112,59 +113,74 @@ static void funcFB (void) | |||
112 | 113 | ||
113 | 114 | ||
114 | /* | 115 | /* |
115 | ** Lock routines | 116 | ** Reference routines |
116 | */ | 117 | */ |
117 | 118 | ||
118 | static Object *lockArray = NULL; | 119 | static struct ref { |
119 | static int lockSize = 0; | 120 | Object o; |
121 | enum {LOCK, HOLD, FREE, COLLECTED} status; | ||
122 | } *refArray = NULL; | ||
123 | static int refSize = 0; | ||
120 | 124 | ||
121 | int luaI_lock (Object *object) | 125 | lua_Reference luaI_ref (Object *object, int lock) |
122 | { | 126 | { |
123 | int i; | 127 | int i; |
124 | int oldSize; | 128 | int oldSize; |
125 | if (tag(object) == LUA_T_NIL) | 129 | if (tag(object) == LUA_T_NIL) |
126 | return -1; /* special lock ref for nil */ | 130 | return -1; /* special ref for nil */ |
127 | for (i=0; i<lockSize; i++) | 131 | for (i=0; i<refSize; i++) |
128 | if (tag(&lockArray[i]) == LUA_T_NIL) | 132 | if (refArray[i].status == FREE) |
129 | { | 133 | goto found; |
130 | lockArray[i] = *object; | ||
131 | return i; | ||
132 | } | ||
133 | /* no more empty spaces */ | 134 | /* no more empty spaces */ |
134 | oldSize = lockSize; | 135 | oldSize = refSize; |
135 | lockSize = growvector(&lockArray, lockSize, Object, lockEM, MAX_WORD); | 136 | refSize = growvector(&refArray, refSize, struct ref, refEM, MAX_WORD); |
136 | for (i=oldSize; i<lockSize; i++) | 137 | for (i=oldSize; i<refSize; i++) |
137 | tag(&lockArray[i]) = LUA_T_NIL; | 138 | refArray[i].status = FREE; |
138 | lockArray[oldSize] = *object; | 139 | i = oldSize; |
139 | return oldSize; | 140 | found: |
141 | refArray[i].o = *object; | ||
142 | refArray[i].status = lock ? LOCK : HOLD; | ||
143 | return i; | ||
140 | } | 144 | } |
141 | 145 | ||
142 | 146 | ||
143 | void lua_unlock (int ref) | 147 | void lua_unref (lua_Reference ref) |
144 | { | 148 | { |
145 | if (ref >= 0 && ref < lockSize) | 149 | if (ref >= 0 && ref < refSize) |
146 | tag(&lockArray[ref]) = LUA_T_NIL; | 150 | refArray[ref].status = FREE; |
147 | } | 151 | } |
148 | 152 | ||
149 | 153 | ||
150 | Object *luaI_getlocked (int ref) | 154 | Object *luaI_getref (lua_Reference ref) |
151 | { | 155 | { |
152 | static Object nul = {LUA_T_NIL, {0}}; | 156 | static Object nul = {LUA_T_NIL, {0}}; |
153 | if (ref >= 0 && ref < lockSize) | 157 | if (ref == -1) |
154 | return &lockArray[ref]; | ||
155 | else | ||
156 | return &nul; | 158 | return &nul; |
159 | if (ref >= 0 && ref < refSize && | ||
160 | (refArray[ref].status == LOCK || refArray[ref].status == HOLD)) | ||
161 | return &refArray[ref].o; | ||
162 | else | ||
163 | return NULL; | ||
157 | } | 164 | } |
158 | 165 | ||
159 | 166 | ||
160 | void luaI_travlock (int (*fn)(Object *)) | 167 | void luaI_travlock (int (*fn)(Object *)) |
161 | { | 168 | { |
162 | int i; | 169 | int i; |
163 | for (i=0; i<lockSize; i++) | 170 | for (i=0; i<refSize; i++) |
164 | fn(&lockArray[i]); | 171 | if (refArray[i].status == LOCK) |
172 | fn(&refArray[i].o); | ||
165 | } | 173 | } |
166 | 174 | ||
167 | 175 | ||
176 | void luaI_invalidaterefs (void) | ||
177 | { | ||
178 | int i; | ||
179 | for (i=0; i<refSize; i++) | ||
180 | if (refArray[i].status == HOLD && !luaI_ismarked(&refArray[i].o)) | ||
181 | refArray[i].status = COLLECTED; | ||
182 | } | ||
183 | |||
168 | char *luaI_travfallbacks (int (*fn)(Object *)) | 184 | char *luaI_travfallbacks (int (*fn)(Object *)) |
169 | { | 185 | { |
170 | int i; | 186 | int i; |
@@ -1,10 +1,11 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: fallback.h,v 1.10 1995/10/17 11:52:38 roberto Exp roberto $ | 2 | ** $Id: fallback.h,v 1.11 1996/01/30 15:25:23 roberto Exp roberto $ |
3 | */ | 3 | */ |
4 | 4 | ||
5 | #ifndef fallback_h | 5 | #ifndef fallback_h |
6 | #define fallback_h | 6 | #define fallback_h |
7 | 7 | ||
8 | #include "lua.h" | ||
8 | #include "opcode.h" | 9 | #include "opcode.h" |
9 | 10 | ||
10 | extern struct FB { | 11 | extern struct FB { |
@@ -26,9 +27,10 @@ extern struct FB { | |||
26 | #define FB_GETGLOBAL 9 | 27 | #define FB_GETGLOBAL 9 |
27 | 28 | ||
28 | void luaI_setfallback (void); | 29 | void luaI_setfallback (void); |
29 | int luaI_lock (Object *object); | 30 | lua_Reference luaI_ref (Object *object, int lock); |
30 | Object *luaI_getlocked (int ref); | 31 | Object *luaI_getref (lua_Reference ref); |
31 | void luaI_travlock (int (*fn)(Object *)); | 32 | void luaI_travlock (int (*fn)(Object *)); |
33 | void luaI_invalidaterefs (void); | ||
32 | char *luaI_travfallbacks (int (*fn)(Object *)); | 34 | char *luaI_travfallbacks (int (*fn)(Object *)); |
33 | 35 | ||
34 | #endif | 36 | #endif |
@@ -2,7 +2,7 @@ | |||
2 | ** LUA - Linguagem para Usuarios de Aplicacao | 2 | ** LUA - Linguagem para Usuarios de Aplicacao |
3 | ** Grupo de Tecnologia em Computacao Grafica | 3 | ** Grupo de Tecnologia em Computacao Grafica |
4 | ** TeCGraf - PUC-Rio | 4 | ** TeCGraf - PUC-Rio |
5 | ** $Id: lua.h,v 3.24 1996/03/19 22:28:37 roberto Exp roberto $ | 5 | ** $Id: lua.h,v 3.25 1996/03/21 21:30:29 roberto Exp roberto $ |
6 | */ | 6 | */ |
7 | 7 | ||
8 | 8 | ||
@@ -80,17 +80,20 @@ lua_Object lua_getsubscript (void); | |||
80 | 80 | ||
81 | int lua_type (lua_Object object); | 81 | int lua_type (lua_Object object); |
82 | 82 | ||
83 | int lua_lock (void); | 83 | |
84 | lua_Object lua_getlocked (int ref); | 84 | typedef int lua_Reference; |
85 | void lua_pushlocked (int ref); | 85 | |
86 | void lua_unlock (int ref); | 86 | lua_Reference lua_ref (int lock); |
87 | lua_Object lua_getref (lua_Reference ref); | ||
88 | void lua_pushref (lua_Reference ref); | ||
89 | void lua_unref (lua_Reference ref); | ||
87 | 90 | ||
88 | lua_Object lua_createtable (void); | 91 | lua_Object lua_createtable (void); |
89 | 92 | ||
90 | 93 | ||
91 | /* some useful macros */ | 94 | /* some useful macros */ |
92 | 95 | ||
93 | #define lua_lockobject(o) (lua_pushobject(o), lua_lock()) | 96 | #define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l)) |
94 | 97 | ||
95 | #define lua_register(n,f) (lua_pushcfunction(f), lua_storeglobal(n)) | 98 | #define lua_register(n,f) (lua_pushcfunction(f), lua_storeglobal(n)) |
96 | 99 | ||
@@ -99,6 +102,12 @@ lua_Object lua_createtable (void); | |||
99 | 102 | ||
100 | /* for compatibility with old versions. Avoid using these macros */ | 103 | /* for compatibility with old versions. Avoid using these macros */ |
101 | 104 | ||
105 | #define lua_lockobject(o) lua_refobject(o,1) | ||
106 | #define lua_lock() lua_ref(1) | ||
107 | #define lua_getlocked lua_getref | ||
108 | #define lua_pushlocked lua_pushref | ||
109 | #define lua_unlock lua_unref | ||
110 | |||
102 | #define lua_pushliteral(o) lua_pushstring(o) | 111 | #define lua_pushliteral(o) lua_pushstring(o) |
103 | 112 | ||
104 | #define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getsubscript()) | 113 | #define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getsubscript()) |
@@ -1,7 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | ** mem.c | 2 | ** mem.c |
3 | ** memory manager for lua | 3 | ** memory manager for lua |
4 | ** $Id: mem.h,v 1.5 1996/03/21 16:31:32 roberto Exp roberto $ | 4 | ** $Id: mem.h,v 1.6 1996/03/21 18:54:29 roberto Exp roberto $ |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #ifndef mem_h | 7 | #ifndef mem_h |
@@ -18,7 +18,7 @@ | |||
18 | #define constantEM "constant table overflow" | 18 | #define constantEM "constant table overflow" |
19 | #define stackEM "stack size overflow" | 19 | #define stackEM "stack size overflow" |
20 | #define lexEM "lex buffer overflow" | 20 | #define lexEM "lex buffer overflow" |
21 | #define lockEM "lock table overflow" | 21 | #define refEM "reference table overflow" |
22 | #define tableEM "table overflow" | 22 | #define tableEM "table overflow" |
23 | #define memEM "not enough memory" | 23 | #define memEM "not enough memory" |
24 | 24 | ||
@@ -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}} |
@@ -3,7 +3,7 @@ | |||
3 | ** Mathematics library to LUA | 3 | ** Mathematics library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_mathlib="$Id: mathlib.c,v 1.13 1995/11/10 17:54:31 roberto Exp roberto $"; | 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.14 1996/02/09 17:21:27 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
9 | #include <math.h> | 9 | #include <math.h> |
@@ -104,7 +104,7 @@ static void math_sqrt (void) | |||
104 | lua_pushnumber (sqrt(d)); | 104 | lua_pushnumber (sqrt(d)); |
105 | } | 105 | } |
106 | 106 | ||
107 | static int old_pow; | 107 | static lua_Reference old_pow; |
108 | 108 | ||
109 | static void math_pow (void) | 109 | static void math_pow (void) |
110 | { | 110 | { |
@@ -113,7 +113,7 @@ static void math_pow (void) | |||
113 | lua_Object op = lua_getparam(3); | 113 | lua_Object op = lua_getparam(3); |
114 | if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p') | 114 | if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p') |
115 | { | 115 | { |
116 | lua_Object old = lua_getlocked(old_pow); | 116 | lua_Object old = lua_getref(old_pow); |
117 | lua_pushobject(o1); | 117 | lua_pushobject(o1); |
118 | lua_pushobject(o2); | 118 | lua_pushobject(o2); |
119 | lua_pushobject(op); | 119 | lua_pushobject(op); |
@@ -223,5 +223,5 @@ void mathlib_open (void) | |||
223 | lua_register ("random", math_random); | 223 | lua_register ("random", math_random); |
224 | lua_register ("randomseed", math_randomseed); | 224 | lua_register ("randomseed", math_randomseed); |
225 | 225 | ||
226 | old_pow = lua_lockobject(lua_setfallback("arith", math_pow)); | 226 | old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1); |
227 | } | 227 | } |
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_opcode="$Id: opcode.c,v 3.65 1996/03/21 18:55:02 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.66 1996/03/22 19:12:15 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <setjmp.h> | 8 | #include <setjmp.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -717,27 +717,31 @@ void *lua_getuserdata (lua_Object object) | |||
717 | } | 717 | } |
718 | 718 | ||
719 | 719 | ||
720 | lua_Object lua_getlocked (int ref) | 720 | lua_Object lua_getref (lua_Reference ref) |
721 | { | 721 | { |
722 | adjustC(0); | 722 | Object *o = luaI_getref(ref); |
723 | *top = *luaI_getlocked(ref); | 723 | if (o == NULL) |
724 | incr_top; | 724 | return LUA_NOOBJECT; |
725 | CBase++; /* incorporate object in the stack */ | 725 | adjustC(0); |
726 | return Ref(top-1); | 726 | luaI_pushobject(o); |
727 | CBase++; /* incorporate object in the stack */ | ||
728 | return Ref(top-1); | ||
727 | } | 729 | } |
728 | 730 | ||
729 | 731 | ||
730 | void lua_pushlocked (int ref) | 732 | void lua_pushref (lua_Reference ref) |
731 | { | 733 | { |
732 | *top = *luaI_getlocked(ref); | 734 | Object *o = luaI_getref(ref); |
733 | incr_top; | 735 | if (o == NULL) |
736 | lua_error("access to invalid (possibly garbage collected) reference"); | ||
737 | luaI_pushobject(o); | ||
734 | } | 738 | } |
735 | 739 | ||
736 | 740 | ||
737 | int lua_lock (void) | 741 | lua_Reference lua_ref (int lock) |
738 | { | 742 | { |
739 | adjustC(1); | 743 | adjustC(1); |
740 | return luaI_lock(--top); | 744 | return luaI_ref(--top, lock); |
741 | } | 745 | } |
742 | 746 | ||
743 | 747 | ||
@@ -812,27 +816,29 @@ void lua_pushcfunction (lua_CFunction fn) | |||
812 | */ | 816 | */ |
813 | void lua_pushusertag (void *u, int tag) | 817 | void lua_pushusertag (void *u, int tag) |
814 | { | 818 | { |
815 | if (tag < LUA_T_USERDATA) return; | 819 | if (tag < LUA_T_USERDATA) |
820 | lua_error("invalid tag in `lua_pushusertag'"); | ||
816 | tag(top) = tag; uvalue(top) = u; | 821 | tag(top) = tag; uvalue(top) = u; |
817 | incr_top; | 822 | incr_top; |
818 | } | 823 | } |
819 | 824 | ||
820 | /* | 825 | /* |
821 | ** Push a lua_Object to stack. | 826 | ** Push an object on the stack. |
822 | */ | 827 | */ |
823 | void lua_pushobject (lua_Object o) | 828 | void luaI_pushobject (Object *o) |
824 | { | 829 | { |
825 | *top = *Address(o); | 830 | *top = *o; |
826 | incr_top; | 831 | incr_top; |
827 | } | 832 | } |
828 | 833 | ||
829 | /* | 834 | /* |
830 | ** Push an object on the stack. | 835 | ** Push a lua_Object on stack. |
831 | */ | 836 | */ |
832 | void luaI_pushobject (Object *o) | 837 | void lua_pushobject (lua_Object o) |
833 | { | 838 | { |
834 | *top = *o; | 839 | if (o == LUA_NOOBJECT) |
835 | incr_top; | 840 | lua_error("attempt to push a NOOBJECT"); |
841 | luaI_pushobject(Address(o)); | ||
836 | } | 842 | } |
837 | 843 | ||
838 | int lua_type (lua_Object o) | 844 | int lua_type (lua_Object o) |
@@ -3,7 +3,7 @@ | |||
3 | ** Module to control static tables | 3 | ** Module to control static tables |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_table="$Id: table.c,v 2.50 1996/03/21 16:31:32 roberto Exp roberto $"; | 6 | char *rcs_table="$Id: table.c,v 2.51 1996/03/21 18:54:29 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include "mem.h" | 8 | #include "mem.h" |
9 | #include "opcode.h" | 9 | #include "opcode.h" |
@@ -170,6 +170,24 @@ int lua_markobject (Object *o) | |||
170 | return 0; | 170 | return 0; |
171 | } | 171 | } |
172 | 172 | ||
173 | /* | ||
174 | * returns 0 if the object is going to be (garbage) collected | ||
175 | */ | ||
176 | int luaI_ismarked (Object *o) | ||
177 | { | ||
178 | switch (o->tag) | ||
179 | { | ||
180 | case LUA_T_STRING: | ||
181 | return o->value.ts->marked; | ||
182 | case LUA_T_FUNCTION: | ||
183 | return o->value.tf->marked; | ||
184 | case LUA_T_ARRAY: | ||
185 | return o->value.a->mark; | ||
186 | default: /* nil, number, cfunction, or user data */ | ||
187 | return 1; | ||
188 | } | ||
189 | } | ||
190 | |||
173 | 191 | ||
174 | /* | 192 | /* |
175 | ** Garbage collection. | 193 | ** Garbage collection. |
@@ -182,6 +200,7 @@ Long luaI_collectgarbage (void) | |||
182 | lua_travsymbol(lua_markobject); /* mark symbol table objects */ | 200 | lua_travsymbol(lua_markobject); /* mark symbol table objects */ |
183 | luaI_travlock(lua_markobject); /* mark locked objects */ | 201 | luaI_travlock(lua_markobject); /* mark locked objects */ |
184 | luaI_travfallbacks(lua_markobject); /* mark fallbacks */ | 202 | luaI_travfallbacks(lua_markobject); /* mark fallbacks */ |
203 | luaI_invalidaterefs(); | ||
185 | recovered += lua_strcollector(); | 204 | recovered += lua_strcollector(); |
186 | recovered += lua_hashcollector(); | 205 | recovered += lua_hashcollector(); |
187 | recovered += luaI_funccollector(); | 206 | recovered += luaI_funccollector(); |
@@ -1,7 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | ** Module to control static tables | 2 | ** Module to control static tables |
3 | ** TeCGraf - PUC-Rio | 3 | ** TeCGraf - PUC-Rio |
4 | ** $Id: table.h,v 2.19 1996/02/26 21:00:27 roberto Exp roberto $ | 4 | ** $Id: table.h,v 2.20 1996/03/14 15:57:19 roberto Exp roberto $ |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #ifndef table_h | 7 | #ifndef table_h |
@@ -30,6 +30,7 @@ Word luaI_findconstant (TaggedString *t); | |||
30 | Word luaI_findconstantbyname (char *name); | 30 | Word luaI_findconstantbyname (char *name); |
31 | TaggedString *luaI_createfixedstring (char *str); | 31 | TaggedString *luaI_createfixedstring (char *str); |
32 | int lua_markobject (Object *o); | 32 | int lua_markobject (Object *o); |
33 | int luaI_ismarked (Object *o); | ||
33 | Long luaI_collectgarbage (void); | 34 | Long luaI_collectgarbage (void); |
34 | void lua_pack (void); | 35 | void lua_pack (void); |
35 | 36 | ||