This patch changes LuaNil PMC, it used to be a singleton, this patch makes it a normal PMC. some other methods are implemented for morphing into other Lua types (LuaString, LuaNumber, LuaBoolean).

regards,
klaas-jan
--- languages/lua/classes/luanil.pmc    2006-01-13 16:34:42.000000000 +0100
+++ languages/lua/classes/newluanil.pmc 2006-01-13 16:37:36.000000000 +0100
@@ -1,130 +1,184 @@
-/*
-Copyright: 2005 The Perl Foundation.  All Rights Reserved.
-$Id: luanil.pmc 10933 2006-01-06 01:43:24Z particle $
-
-=head1 NAME
-
-classes/luanil.pmc - Lua Nil
-
-=head1 DESCRIPTION
-
-C<LuaNil> extends C<Python None> to provide a class with the behaviour of
-the Lua C<Nil> value.
-
-=head2 Overloaded Methods
-
-=over 4
-
-=cut
-
-*/
-
-#include "parrot/parrot.h"
-
-static STRING *string_representation;
-static PMC *Lua_Nil;
-
-pmclass LuaNil
-    singleton
-    extends None
-    dynpmc
-    group lua_group
-    hll Lua {
-
-/* Class initialization. Caches constant strings that will be used later.
-*/
-    void class_init() {
-        if (pass) {
-            string_representation = const_string(INTERP, "nil");
-        }
-    }
-
-/*
-
-=item C<STRING* name()>
-
-Return the string "nil".
-
-=cut
-
-*/
-    STRING* name () {
-        return string_representation;
-    }
-
-/*
-
-=item C<STRING* get_string ()>
-
-Return the string "nil".
-
-=cut
-
-*/
-    STRING* get_string () {
-        return string_representation;
-    }
-
-/*
-
-=item C<STRING* get_repr ()>
-
-Return the string "nil".
-
-=cut
-
-*/
-    STRING* get_repr () {
-        return string_representation;
-    }
-
-/*
-
-=item C<void* get_pointer()>
-
-=item C<void set_pointer(void *ptr)>
-
-These two functions are part of the singleton creation interface. For more
-information see F<src/pmc.c>.
-
-=cut
-
-*/
-    void* get_pointer() {
-        return Lua_Nil;
-    }
-
-    void set_pointer(void* ptr) {
-        Lua_Nil = (PMC*)ptr;
-    }
-
-/*
-
-=item C<PMC logical_not(PMC *dest)>
-
-Return always true.
-
-=cut
-
-*/
-    PMC* logical_not (PMC* dest) {
-        if (!dest)
-            dest = pmc_new(INTERP, Parrot_PMC_typenum(INTERP, "LuaBoolean"));
-        VTABLE_set_integer_native(INTERP, dest, 1);
-        return dest;
-    }
-
-}
-
-/*
-
-=back
-
-=head1 AUTHORS
-
-Original code by Klaas-Jan Stol.
-
-=cut
-
-*/
-
+/*
+Copyright: 2005 The Perl Foundation.  All Rights Reserved.
+$Id: luanil.pmc 10933 2006-01-06 01:43:24Z particle $
+
+=head1 NAME
+
+classes/luanil.pmc - Lua Nil
+
+=head1 DESCRIPTION
+
+C<LuaNil> provides a class with the behaviour of the Lua C<Nil> value.
+Implementation is based on the Undef PMC. LuaNil is no longer a singleton;
+this would be a problem, as all uninitialized values in Lua are LuaNil.
+If some value is assigned, the LuaNil should morph into the correct type,
+and so a new PMC is constructed anyway. Therefore, we may as well create
+a new PMC right away. Also, creating a new PMC from a singleton class is
+troublesome (if not possible?).
+
+
+
+=head2 Overloaded Methods
+
+=over 4
+
+=cut
+
+*/
+
+#include "parrot/parrot.h"
+
+static STRING *string_representation;
+static INTVAL dynclass_LuaNumber;
+static INTVAL dynclass_LuaString;
+static INTVAL dynclass_LuaBoolean;
+
+pmclass LuaNil   
+    dynpmc
+    group lua_group
+    hll Lua {
+
+/* 
+* Class initialization. Caches constant strings that will be used later.
+*/
+    void class_init() {
+        if (pass) {
+            string_representation = const_string(INTERP, "nil");
+           dynclass_LuaNumber = Parrot_PMC_typenum(INTERP, "LuaNumber");
+           dynclass_LuaString = Parrot_PMC_typenum(INTERP, "LuaString");
+           dynclass_LuaBoolean = Parrot_PMC_typenum(INTERP, "LuaBoolean");
+        }
+    }
+
+/*
+
+=item C<STRING* name()>
+
+Return the string "nil".
+
+=cut
+
+*/
+    STRING* name () {
+        return string_representation;
+    }
+
+/*
+
+=item C<STRING* get_string ()>
+
+Return the string "nil".
+
+=cut
+
+*/
+    STRING* get_string () {
+        return string_representation;
+    }
+
+
+
+       
+
+
+/*
+
+=item C<STRING* get_repr ()>
+
+Return the string "nil".
+
+=cut
+
+*/
+    STRING* get_repr () {
+        return string_representation;
+    }
+
+
+    PMC* clone () {
+        PMC* dest = pmc_new(INTERP, SELF->vtable->base_type);
+        memcpy(&PMC_union(dest), &PMC_union(SELF), sizeof(UnionVal));
+        return dest;
+    }
+
+
+
+/*
+
+=item C<PMC logical_not(PMC *dest)>
+
+Return always true.
+
+=cut
+
+*/
+    PMC* logical_not (PMC* dest) {
+        if (!dest)
+            dest = pmc_new(INTERP, dynclass_LuaBoolean);
+        VTABLE_set_integer_native(INTERP, dest, 1);
+        return dest;
+    }
+
+/*
+
+=item C<INTVAL defined()>
+
+"nil" in Lua is always undefined.
+
+=cut
+
+*/
+    INTVAL defined() {
+        return 0;
+    }
+
+/*
+
+=item C<void set_number_native(FLOATVAL)>
+
+=item C<void set_string_native(STRING *)>
+
+=item C<void set_bool(INTVAL)>
+
+Methods to set a new value to this LuaNil PMC. First,
+this PMC is morph'ed into the correct Lua type.
+
+=item C<void morph(INTVAL)>
+
+Morph to another Lua type.
+
+=cut
+
+*/
+    void set_number_native(FLOATVAL value) {
+        VTABLE_morph(INTERP, SELF, dynclass_LuaNumber);
+        VTABLE_set_number_native(INTERP, SELF, value);
+    }
+
+    void set_string_native(STRING *value) {
+        VTABLE_morph(INTERP, SELF, dynclass_LuaString);
+        VTABLE_set_string_native(INTERP, SELF, value);
+    }
+
+    void set_bool (INTVAL value) {
+        VTABLE_morph(INTERP, SELF, dynclass_LuaBoolean);
+        VTABLE_set_bool(INTERP, SELF, value);
+    }
+
+    void morph(INTVAL new_type) {
+        pmc_reuse(INTERP, SELF, new_type, 0);
+    }
+
+
+
+
+}
+
+
+
+
+  
+
+
+
+

Reply via email to