Author: fperrad
Date: Fri Mar 10 00:39:33 2006
New Revision: 11848
Modified:
trunk/languages/lua/lib/luabasic.pir
trunk/languages/lua/lib/luastring.pir
trunk/languages/lua/pmc/luabase.pmc
trunk/languages/lua/pmc/luatable.pmc
Log:
Lua 5.1 :
- metatables for all types
Modified: trunk/languages/lua/lib/luabasic.pir
==============================================================================
--- trunk/languages/lua/lib/luabasic.pir (original)
+++ trunk/languages/lua/lib/luabasic.pir Fri Mar 10 00:39:33 2006
@@ -304,9 +304,8 @@
.param pmc obj :optional
.local pmc ret
checkany(obj)
- ret = getprop "__metatable", obj
+ ret = obj."get_metatable"()
if ret goto L1
- new ret, .LuaNil
.return (ret)
L1:
.local pmc prot
@@ -695,7 +694,7 @@
argerror("nil or table expected")
L1:
.local pmc meta
- meta = getprop "__metatable", table
+ meta = table."get_metatable"()
unless meta goto L3
.local pmc prot
.const .LuaString mt = "__metatable"
@@ -703,11 +702,7 @@
unless prot goto L3
error("cannot change a protected metatable")
L3:
- if $S0 == "table" goto L4
- delprop table, "__metatable"
- .return ()
-L4:
- setprop table, "__metatable", metatable
+ table."set_metatable"(metatable)
.return ()
.end
Modified: trunk/languages/lua/lib/luastring.pir
==============================================================================
--- trunk/languages/lua/lib/luastring.pir (original)
+++ trunk/languages/lua/lib/luastring.pir Fri Mar 10 00:39:33 2006
@@ -104,6 +104,13 @@
$P1 = "gsub"
_string[$P1] = $P0
+
+ .local pmc _lua_mt_string
+ _lua_mt_string = new .LuaTable
+ global "mt_string" = _lua_mt_string
+ $P1 = "__index"
+ _lua_mt_string[$P1] = _string
+
.end
Modified: trunk/languages/lua/pmc/luabase.pmc
==============================================================================
--- trunk/languages/lua/pmc/luabase.pmc (original)
+++ trunk/languages/lua/pmc/luabase.pmc Fri Mar 10 00:39:33 2006
@@ -23,6 +23,7 @@
static INTVAL dynpmc_LuaNil;
static INTVAL dynpmc_LuaString;
static INTVAL dynpmc_LuaTable;
+static INTVAL dynpmc_LuaUserdata;
pmclass LuaBase
@@ -529,8 +530,7 @@
METHOD PMC* find_metamethod(STRING* method_name) {
PMC *method;
- PMC *metatable = VTABLE_getprop(INTERP, SELF,
- string_from_const_cstring(INTERP, "__metatable", 0));
+ PMC *metatable = Parrot_LuaBase_get_metatable(INTERP, SELF);
if (dynpmc_LuaTable == metatable->vtable->base_type) {
PMC *key;
@@ -544,6 +544,24 @@
return NULL;
}
+/*
+
+=item C<PMC *get_metatable()>
+
+=cut
+
+*/
+ METHOD PMC* get_metatable() {
+ if (dynpmc_LuaTable == SELF->vtable->base_type
+ || dynpmc_LuaUserdata == SELF->vtable->base_type) {
+ PMC *retval = VTABLE_getprop(INTERP, SELF,
+ string_from_const_cstring(INTERP, "__metatable", 0));
+ if (dynpmc_LuaTable == retval->vtable->base_type)
+ return retval;
+ }
+ return pmc_new(INTERP, dynpmc_LuaNil);
+ }
+
METHOD void super_init () {
dynpmc_LuaNil = pmc_type(INTERP,
string_from_const_cstring(INTERP, "LuaNil", 0));
@@ -551,6 +569,8 @@
string_from_const_cstring(INTERP, "LuaString", 0));
dynpmc_LuaTable = pmc_type(INTERP,
string_from_const_cstring(INTERP, "LuaTable", 0));
+ dynpmc_LuaUserdata = pmc_type(INTERP,
+ string_from_const_cstring(INTERP, "LuaUserdata", 0));
}
}
Modified: trunk/languages/lua/pmc/luatable.pmc
==============================================================================
--- trunk/languages/lua/pmc/luatable.pmc (original)
+++ trunk/languages/lua/pmc/luatable.pmc Fri Mar 10 00:39:33 2006
@@ -100,6 +100,13 @@
const_string(INTERP, "table"),
const_string(INTERP, "rawset"),
meth);
+ meth = Parrot_find_global(INTERP,
+ const_string(INTERP, "LuaTable"),
+ const_string(INTERP, "set_metatable"));
+ Parrot_store_global(INTERP,
+ const_string(INTERP, "table"),
+ const_string(INTERP, "set_metatable"),
+ meth);
}
}
@@ -522,6 +529,24 @@
}
}
+/*
+
+=item C<void set_metatable(PMC *meta)>
+
+=cut
+
+*/
+ METHOD void set_metatable(PMC *meta) {
+ if (dynpmc_LuaNil == meta->vtable->base_type) {
+ VTABLE_delprop(INTERP, SELF,
+ string_from_const_cstring(INTERP, "__metatable", 0));
+ } else {
+ VTABLE_setprop(INTERP, SELF,
+ string_from_const_cstring(INTERP, "__metatable", 0),
+ meta);
+ }
+ }
+
}
/*