lua-users home
lua-l archive

Re: Read-Only Tables Patch

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Doh! So it is.
Here is the right way 'round.
--- CUT ---
diff -ruN lua-5.1.1/src/ltm.c lua-rot-5.1.1/src/ltm.c
--- lua-5.1.1/src/ltm.c	2006年01月10日 06:50:00.000000000 -0600
+++ lua-rot-5.1.1/src/ltm.c	2006年11月10日 15:39:56.000000000 -0600
@@ -30,8 +30,11 @@
void luaT_init (lua_State *L) {
 static const char *const luaT_eventname[] = { /* ORDER TM */
 "__index", "__newindex",
- "__gc", "__mode", "__eq",
- "__add", "__sub", "__mul", "__div", "__mod",
+ "__gc", "__mode",
+#ifdef LUA_READONLY_TABLES
+	"__readonly",
+#endif
+	"__eq", "__add", "__sub", "__mul", "__div", "__mod",
 "__pow", "__unm", "__len", "__lt", "__le",
 "__concat", "__call"
 };
diff -ruN lua-5.1.1/src/ltm.h lua-rot-5.1.1/src/ltm.h
--- lua-5.1.1/src/ltm.h	2005年06月06日 08:30:25.000000000 -0500
+++ lua-rot-5.1.1/src/ltm.h	2006年11月10日 15:39:59.000000000 -0600
@@ -20,6 +20,9 @@
 TM_NEWINDEX,
 TM_GC,
 TM_MODE,
+#ifdef LUA_READONLY_TABLES
+ TM_READONLY,
+#endif
 TM_EQ, /* last tag method with `fast' access */
 TM_ADD,
 TM_SUB,
diff -ruN lua-5.1.1/src/luaconf.h lua-rot-5.1.1/src/luaconf.h
--- lua-5.1.1/src/luaconf.h	2006年04月10日 13:27:23.000000000 -0500
+++ lua-rot-5.1.1/src/luaconf.h	2006年11月10日 15:39:47.000000000 -0600
@@ -747,6 +747,12 @@
#endif
+/*
+@@ LUA_READONLY_TABLES controls whether to enable read-only table
+support by way of the __readonly metatable entry. CHANGE it (define it)
+to enable Read-Only support.
+*/
+#define LUA_READONLY_TABLES
/* =================================================================== */
diff -ruN lua-5.1.1/src/lvm.c lua-rot-5.1.1/src/lvm.c
--- lua-5.1.1/src/lvm.c	2006年06月05日 10:58:59.000000000 -0500
+++ lua-rot-5.1.1/src/lvm.c	2006年11月10日 14:26:17.000000000 -0600
@@ -140,11 +140,22 @@
 if (ttistable(t)) { /* `t' is a table? */
 Table *h = hvalue(t);
 TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
+#ifdef LUA_READONLY_TABLES
+	 if ( (tm = fasttm(L, h->metatable, TM_READONLY)) != NULL )
+	 {
+		 if (ttisfunction(tm)) {
+			 callTM(L, tm, t, key, val);
+			 return;
+		 }
+		 else
+			 luaG_runerror(L, "Attempt to modify a Read-Only Table!");
+	 }
+#endif
 if (!ttisnil(oldval) || /* result is no nil? */
(tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
- setobj2t(L, oldval, val);
- luaC_barriert(L, h, val);
- return;
+ 	setobj2t(L, oldval, val);
+ 	luaC_barriert(L, h, val);
+ 	return;
 }
 /* else will try the tag method */
 }
--- CUT ---
On Nov 13, 2006, at 9:48 AM, Alex Queiroz wrote:
Hallo,
On 11/13/06, Michael Grubb <lua@dailyvoid.com> wrote:
The patch is below:
 I guess your patch is backwards.
--
-alex
http://www.ventonegro.org/

AltStyle によって変換されたページ (->オリジナル) /