Author: fperrad
Date: Mon Apr 2 23:04:44 2007
New Revision: 17953
Modified:
trunk/languages/lua/lib/luaio.pir
trunk/languages/lua/pmc/luaany.pmc
trunk/languages/lua/pmc/luanil.pmc
trunk/languages/lua/pmc/luatable.pmc
trunk/languages/lua/pmc/luauserdata.pmc
trunk/languages/lua/t/io.t
Log:
[Lua]
- implement metatable with PMC_metadata
- fix finalizer
- and add test
Modified: trunk/languages/lua/lib/luaio.pir
==============================================================================
--- trunk/languages/lua/lib/luaio.pir (original)
+++ trunk/languages/lua/lib/luaio.pir Mon Apr 2 23:04:44 2007
@@ -1024,11 +1024,14 @@
# ignore closed files and standard files
if null f goto L1
$P0 = getstdin
- if $P0 == f goto L1
+ $I0 = issame $P0, f
+ if $I0 goto L1
$P0 = getstdout
- if $P0 == f goto L1
+ $I0 = issame $P0, f
+ if $I0 goto L1
$P0 = getstderr
- if $P0 == f goto L1
+ $I0 = issame $P0, f
+ if $I0 goto L1
print "closing file for you.\n"
aux_close(self)
L1:
Modified: trunk/languages/lua/pmc/luaany.pmc
==============================================================================
--- trunk/languages/lua/pmc/luaany.pmc (original)
+++ trunk/languages/lua/pmc/luaany.pmc Mon Apr 2 23:04:44 2007
@@ -20,7 +20,6 @@
#include "parrot/parrot.h"
-extern STRING *lua_metatable;
extern INTVAL dynpmc_LuaBoolean;
extern INTVAL dynpmc_LuaClosure;
extern INTVAL dynpmc_LuaFunction;
@@ -33,19 +32,22 @@
PMC *
find_meth(Interp* interp, PMC *obj, const char *name) {
PMC *meta = NULL;
+ INTVAL type = obj->vtable->base_type;
- if (dynpmc_LuaString == obj->vtable->base_type) {
+ if (dynpmc_LuaString == type) {
meta = Parrot_find_global_cur(interp,
const_string(interp, "mt_string"));
}
- else {
- meta = VTABLE_getprop(interp, obj, lua_metatable);
- if (meta != NULL && meta->pmc_ext != NULL
- && dynpmc_LuaTable != meta->vtable->base_type) {
+ if (dynpmc_LuaClosure != type && dynpmc_LuaFunction != type) {
+ if (obj->pmc_ext != NULL) {
+ meta = PMC_metadata(obj);
+ }
+ if (meta != NULL && dynpmc_LuaTable != meta->vtable->base_type) {
return meta;
}
}
- if (meta != NULL && dynpmc_LuaTable == meta->vtable->base_type) {
+
+ if (meta != NULL) {
PMC *method;
PMC *key = pmc_new(interp, dynpmc_LuaString);
PMC_str_val(key) = const_string(interp, name);
Modified: trunk/languages/lua/pmc/luanil.pmc
==============================================================================
--- trunk/languages/lua/pmc/luanil.pmc (original)
+++ trunk/languages/lua/pmc/luanil.pmc Mon Apr 2 23:04:44 2007
@@ -31,7 +31,6 @@
#include "parrot/parrot.h"
static STRING *string_representation;
-STRING *lua_metatable;
INTVAL dynpmc_LuaBoolean;
INTVAL dynpmc_LuaClosure;
INTVAL dynpmc_LuaFunction;
@@ -67,7 +66,6 @@
const_string(INTERP, "LuaString"));
dynpmc_LuaTable = pmc_type(INTERP,
const_string(INTERP, "LuaTable"));
- lua_metatable = const_string(INTERP, "__metatable");
}
}
Modified: trunk/languages/lua/pmc/luatable.pmc
==============================================================================
--- trunk/languages/lua/pmc/luatable.pmc (original)
+++ trunk/languages/lua/pmc/luatable.pmc Mon Apr 2 23:04:44 2007
@@ -25,7 +25,6 @@
#include "parrot/parrot.h"
static STRING *luatable_name;
-extern STRING *lua_metatable;
extern INTVAL dynpmc_LuaBoolean;
extern INTVAL dynpmc_LuaClosure;
extern INTVAL dynpmc_LuaFunction;
@@ -91,6 +90,7 @@
extends LuaAny
does hash
dynpmc
+ need_ext
group lua_group
hll Lua {
@@ -113,6 +113,7 @@
*/
void init () {
PMC_struct_val(SELF) = NULL;
+ PMC_metadata(SELF) = NULL;
PObj_custom_mark_destroy_SETALL(SELF);
parrot_new_pmc_hash(INTERP, SELF);
}
@@ -413,8 +414,8 @@
*/
METHOD PMC* get_metatable() {
- PMC* retval = VTABLE_getprop(INTERP, SELF, lua_metatable);
- if (retval != NULL && retval->vtable->base_type == dynpmc_LuaTable)
+ PMC* retval = PMC_metadata(SELF);
+ if (retval != NULL)
return retval;
else
return pmc_new(INTERP, dynpmc_LuaNil);
@@ -542,10 +543,10 @@
*/
METHOD void set_metatable(PMC *meta) {
if (dynpmc_LuaNil == meta->vtable->base_type) {
- VTABLE_delprop(INTERP, SELF, lua_metatable);
+ PMC_metadata(SELF) = NULL;
}
else {
- VTABLE_setprop(INTERP, SELF, lua_metatable, meta);
+ PMC_metadata(SELF) = meta;
}
}
Modified: trunk/languages/lua/pmc/luauserdata.pmc
==============================================================================
--- trunk/languages/lua/pmc/luauserdata.pmc (original)
+++ trunk/languages/lua/pmc/luauserdata.pmc Mon Apr 2 23:04:44 2007
@@ -22,7 +22,6 @@
#include "parrot/parrot.h"
static STRING *luauserdata_name;
-extern STRING *lua_metatable;
extern INTVAL dynpmc_LuaBoolean;
extern INTVAL dynpmc_LuaNil;
extern INTVAL dynpmc_LuaString;
@@ -35,6 +34,7 @@
extends LuaAny
does scalar
dynpmc
+ need_ext
group lua_group
hll Lua {
@@ -57,6 +57,7 @@
*/
void init () {
PMC_pmc_val(SELF) = NULL;
+ PMC_metadata(SELF) = NULL;
PObj_custom_mark_destroy_SETALL(SELF);
}
@@ -84,14 +85,10 @@
*/
void destroy () {
- /* printf("userdata destroy\n"); */
-#if 0
- /* FIXME : crash because metatable is already freed */
PMC* meth = find_meth(INTERP, SELF, "__gc");
if (meth != NULL) {
(void)Parrot_runops_fromc_args(INTERP, meth, "vP", SELF);
}
-#endif
if (PMC_pmc_val(SELF)) {
PMC_pmc_val(SELF) = NULL;
}
@@ -225,8 +222,8 @@
*/
METHOD PMC* get_metatable() {
- PMC *retval = VTABLE_getprop(INTERP, SELF, lua_metatable);
- if (retval != NULL && retval->vtable->base_type == dynpmc_LuaTable)
+ PMC *retval = PMC_metadata(SELF);
+ if (retval != NULL)
return retval;
else
return pmc_new(INTERP, dynpmc_LuaNil);
@@ -258,10 +255,10 @@
*/
METHOD void set_metatable(PMC *meta) {
if (dynpmc_LuaNil == meta->vtable->base_type) {
- VTABLE_delprop(INTERP, SELF, lua_metatable);
+ PMC_metadata(SELF) = NULL;
}
else {
- VTABLE_setprop(INTERP, SELF, lua_metatable, meta);
+ PMC_metadata(SELF) = meta;
}
}
Modified: trunk/languages/lua/t/io.t
==============================================================================
--- trunk/languages/lua/t/io.t (original)
+++ trunk/languages/lua/t/io.t Mon Apr 2 23:04:44 2007
@@ -27,7 +27,7 @@
use FindBin;
use lib "$FindBin::Bin";
-use Parrot::Test tests => 37;
+use Parrot::Test tests => 38;
use Test::More;
language_output_like( 'lua', << 'CODE', << 'OUTPUT', 'io.stdin' );
@@ -362,6 +362,23 @@
true
OUTPUT
+SKIP:
+{
+skip('only with Parrot', 1) if (exists $ENV{PARROT_LUA_TEST_PROG});
+
+language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'file:__gc' );
+function inner ()
+ local f = io.open("file.out")
+end
+
+inner()
+print("end")
+CODE
+end
+closing file for you.
+OUTPUT
+}
+
# Local Variables:
# mode: cperl
# cperl-indent-level: 4