[svn:parrot] r36350 - trunk/languages/lua/src/pmc

fperrad at svn.parrot.org fperrad at svn.parrot.org
Wed Feb 4 13:59:43 UTC 2009


Author: fperrad
Date: Wed Feb  4 13:59:38 2009
New Revision: 36350
URL: https://trac.parrot.org/parrot/changeset/36350

Log:
[Lua] refactor LuaBoolean & LuaNumber PMC with ATTR

Modified:
   trunk/languages/lua/src/pmc/luaboolean.pmc
   trunk/languages/lua/src/pmc/luanumber.pmc
   trunk/languages/lua/src/pmc/luatable.pmc

Modified: trunk/languages/lua/src/pmc/luaboolean.pmc
==============================================================================
--- trunk/languages/lua/src/pmc/luaboolean.pmc	Wed Feb  4 12:44:29 2009	(r36349)
+++ trunk/languages/lua/src/pmc/luaboolean.pmc	Wed Feb  4 13:59:38 2009	(r36350)
@@ -34,6 +34,8 @@
     hll      lua
     maps     Boolean {
 
+    ATTR INTVAL iv;
+
 /*
 
 =item C<void init()>
@@ -44,7 +46,24 @@
 
 */
     VTABLE void init() {
-        PMC_int_val(SELF) = 0;
+        Parrot_LuaBoolean_attributes* attrs =
+            mem_allocate_zeroed_typed(Parrot_LuaBoolean_attributes);
+        attrs->iv = 0;
+        PMC_data(SELF) = attrs;
+        PObj_active_destroy_SET(SELF);
+    }
+
+/*
+
+=item C<void destroy()>
+
+Destroy this PMC.
+
+=cut
+
+*/
+    VTABLE void destroy() {
+        mem_sys_free(PMC_data(SELF));
     }
 
 /*
@@ -57,13 +76,13 @@
 
 */
     VTABLE PMC* instantiate_str(STRING *rep, INTVAL flags) {
-        PMC *res;
-        INTVAL type = PMC_type(SELF);
-        if (flags & PObj_constant_FLAG)
-            res = constant_pmc_new(INTERP, type);
-        else
-            res = pmc_new(INTERP, type);
-        PMC_int_val(res) = (Parrot_str_to_int(INTERP, rep) != 0);
+        const INTVAL type = SELF->vtable->base_type;
+        PMC * const res =
+            (flags & PObj_constant_FLAG)
+                ? constant_pmc_new(INTERP, type)
+                : pmc_new(INTERP, type);
+
+        SET_ATTR_iv(INTERP, res, Parrot_str_to_int(INTERP, rep) != 0);
         return res;
     }
 
@@ -90,8 +109,10 @@
 
 */
     VTABLE PMC* clone() {
-        PMC *dest = pmc_new(INTERP, PMC_type(SELF));
-        STRUCT_COPY(&PMC_union(dest), &PMC_union(SELF));
+        INTVAL iv;
+        PMC *dest = pmc_new(INTERP, SELF->vtable->base_type);
+        GET_ATTR_iv(INTERP, SELF, iv);
+        SET_ATTR_iv(INTERP, dest, iv);
         return dest;
     }
 
@@ -105,7 +126,9 @@
 
 */
     VTABLE INTVAL get_integer() {
-        return PMC_int_val(SELF);
+        INTVAL iv;
+        GET_ATTR_iv(INTERP, SELF, iv);
+        return iv;
     }
 
 /*
@@ -118,7 +141,9 @@
 
 */
     VTABLE STRING* get_string() {
-        if (PMC_int_val(SELF))
+        INTVAL iv;
+        GET_ATTR_iv(INTERP, SELF, iv);
+        if (iv)
             return Parrot_str_new_constant(INTERP, "true");
         else
             return Parrot_str_new_constant(INTERP, "false");
@@ -135,7 +160,9 @@
 
 */
     VTABLE INTVAL get_bool() {
-        return PMC_int_val(SELF) ? 1 : 0;
+        INTVAL iv;
+        GET_ATTR_iv(INTERP, SELF, iv);
+        return iv ? 1 : 0;
     }
 
 /*
@@ -148,11 +175,11 @@
 
 */
     VTABLE void set_integer_native(INTVAL value) {
-        PMC_int_val(SELF) = (value != 0);
+        SET_ATTR_iv(INTERP, SELF, value != 0);
     }
 
     VTABLE void set_bool(INTVAL value) {
-        PMC_int_val(SELF) = (value != 0);
+        SET_ATTR_iv(INTERP, SELF, value != 0);
     }
 
 /*
@@ -165,7 +192,7 @@
 
 */
     VTABLE void set_pmc(PMC *value) {
-        PMC_int_val(SELF) = VTABLE_get_bool(INTERP, value);
+        SELF.set_integer_native(VTABLE_get_bool(INTERP, value));
     }
 
 /*
@@ -178,9 +205,9 @@
 
 */
     VTABLE void freeze(visit_info *info) {
-        IMAGE_IO *io = info->image_io;
+        IMAGE_IO * const io = info->image_io;
         SUPER(info);
-        VTABLE_push_integer(INTERP, io, PMC_int_val(SELF));
+        VTABLE_push_integer(INTERP, io, SELF.get_integer());
     }
 
 /*
@@ -193,10 +220,10 @@
 
 */
     VTABLE void thaw(visit_info *info) {
-        IMAGE_IO *io = info->image_io;
+        IMAGE_IO * const io = info->image_io;
         SUPER(info);
         if (info->extra_flags == EXTRA_IS_NULL)
-            PMC_int_val(SELF) = VTABLE_shift_integer(INTERP, io);
+            SELF.set_integer_native(VTABLE_shift_integer(INTERP, io));
     }
 
 /*
@@ -213,7 +240,8 @@
 
 */
     MULTI INTVAL is_equal(LuaBoolean value) {
-        return (INTVAL)(PMC_int_val(SELF) == PMC_int_val(value));
+        return (VTABLE_get_integer(INTERP, SELF)
+            ==  VTABLE_get_integer(INTERP, value));
     }
 
     MULTI INTVAL is_equal(DEFAULT value) {
@@ -237,7 +265,7 @@
         PMC *retval = pmc_new(INTERP, dynpmc_LuaBoolean);
 
         if (PMC_type(SELF)    == PMC_type(value)
-        &&  PMC_int_val(SELF) == VTABLE_get_integer(INTERP, value))
+        &&  VTABLE_get_integer(INTERP, SELF) == VTABLE_get_integer(INTERP, value))
             VTABLE_set_integer_native(INTERP, retval, 1);
         else
             VTABLE_set_integer_native(INTERP, retval, 0);

Modified: trunk/languages/lua/src/pmc/luanumber.pmc
==============================================================================
--- trunk/languages/lua/src/pmc/luanumber.pmc	Wed Feb  4 12:44:29 2009	(r36349)
+++ trunk/languages/lua/src/pmc/luanumber.pmc	Wed Feb  4 13:59:38 2009	(r36350)
@@ -35,6 +35,8 @@
     maps     Integer
     maps     Float {
 
+    ATTR FLOATVAL fv;
+
 /*
 
 =item C<void init()>
@@ -45,7 +47,23 @@
 
 */
     VTABLE void init() {
-        PMC_num_val(SELF) = 0.0;
+        Parrot_LuaNumber_attributes *fattr =
+            mem_allocate_zeroed_typed(Parrot_LuaNumber_attributes);
+        fattr->fv = 0.0;
+        PMC_data(SELF) = fattr;
+    }
+
+/*
+
+=item C<void destroy()>
+
+Destroy this PMC.
+
+=cut
+
+*/
+    VTABLE void destroy() {
+        mem_sys_free(PMC_data(SELF));
     }
 
 /*
@@ -63,13 +81,13 @@
 
 */
     VTABLE PMC *instantiate_str(STRING *rep, INTVAL flags) {
-        PMC *res;
-        INTVAL type = PMC_type(SELF);
-        if (flags & PObj_constant_FLAG)
-            res = constant_pmc_new(INTERP, type);
-        else
-            res = pmc_new(INTERP, type);
-        PMC_num_val(res) = Parrot_str_to_num(INTERP, rep);
+        const INTVAL type = SELF->vtable->base_type;
+        PMC * const res =
+            (flags & PObj_constant_FLAG)
+                ? constant_pmc_new(INTERP, type)
+                : pmc_new(INTERP, type);
+
+        SET_ATTR_fv(INTERP, res, Parrot_str_to_num(INTERP, rep));
         return res;
     }
 
@@ -96,8 +114,10 @@
 
 */
     VTABLE PMC *clone() {
-        PMC *dest = pmc_new(INTERP, PMC_type(SELF));
-        STRUCT_COPY(&PMC_union(dest), &PMC_union(SELF));
+        FLOATVAL fv;
+        PMC *dest = pmc_new(INTERP, SELF->vtable->base_type);
+        GET_ATTR_fv(INTERP, SELF, fv);
+        SET_ATTR_fv(INTERP, dest, fv);
         return dest;
     }
 
@@ -110,9 +130,11 @@
 =cut
 
 */
-   VTABLE INTVAL get_integer() {
-        return (INTVAL)PMC_num_val(SELF);
-   }
+    VTABLE INTVAL get_integer() {
+        /* two steps avoid casting warnings */
+        FLOATVAL n = SELF.get_number();
+        return (INTVAL) n;
+    }
 
 /*
 
@@ -124,7 +146,9 @@
 
 */
     VTABLE FLOATVAL get_number() {
-        return PMC_num_val(SELF);
+        FLOATVAL fv;
+        GET_ATTR_fv(INTERP, SELF, fv);
+        return fv;
     }
 
 /*
@@ -137,8 +161,8 @@
 
 */
     VTABLE STRING* get_string() {
-        FLOATVAL f = PMC_num_val(SELF);
-        return Parrot_sprintf_c(INTERP, LUA_NUMBER_FMT, f);
+        const FLOATVAL fv = SELF.get_number();
+        return Parrot_sprintf_c(INTERP, LUA_NUMBER_FMT, fv);
     }
 
 /*
@@ -149,7 +173,7 @@
 
 */
     VTABLE void set_integer_native(INTVAL value) {
-        PMC_num_val(SELF) = (FLOATVAL)value;
+        SET_ATTR_fv(INTERP, SELF, (FLOATVAL)value);
     }
 
 /*
@@ -160,7 +184,7 @@
 
 */
     VTABLE void set_number_native(FLOATVAL value) {
-        PMC_num_val(SELF) = value;
+        SET_ATTR_fv(INTERP, SELF, value);
     }
 
 /*
@@ -185,7 +209,7 @@
 
 */
     VTABLE void set_pmc(PMC *value) {
-        PMC_num_val(SELF) = VTABLE_get_number(INTERP, value);
+        SET_ATTR_fv(INTERP, SELF, VTABLE_get_number(INTERP, value));
     }
 
 /*
@@ -198,7 +222,10 @@
 
 */
     VTABLE void increment() {
-        PMC_num_val(SELF)++;
+        FLOATVAL fv;
+        GET_ATTR_fv(INTERP, SELF, fv);
+        fv++;
+        SET_ATTR_fv(INTERP, SELF, fv);
     }
 
 /*
@@ -211,7 +238,10 @@
 
 */
     VTABLE void decrement() {
-        PMC_num_val(SELF)--;
+        FLOATVAL fv;
+        GET_ATTR_fv(INTERP, SELF, fv);
+        fv--;
+        SET_ATTR_fv(INTERP, SELF, fv);
     }
 
 /*
@@ -242,6 +272,38 @@
 
 /*
 
+=item C<void freeze(visit_info *info)>
+
+Used to archive the number.
+
+=cut
+
+*/
+    VTABLE void freeze(visit_info *info) {
+        IMAGE_IO * const io = info->image_io;
+        SUPER(info);
+        VTABLE_push_float(INTERP, io, SELF.get_number());
+    }
+
+/*
+
+=item C<void thaw(visit_info *info)>
+
+Used to unarchive the number.
+
+=cut
+
+*/
+    VTABLE void thaw(visit_info *info) {
+        IMAGE_IO * const io = info->image_io;
+        SUPER(info);
+        if (info->extra_flags == EXTRA_IS_NULL)
+            SET_ATTR_fv(INTERP, SELF, VTABLE_shift_float(INTERP, io));
+    }
+/*
+
+/*
+
 =back
 
 =head2 non-Vtable Methods
@@ -760,7 +822,7 @@
 
 */
     MULTI INTVAL is_equal(LuaNumber value) {
-        return (INTVAL)(PMC_num_val(SELF) == PMC_num_val(value));
+        return (INTVAL)(SELF.get_number() == VTABLE_get_number(INTERP, value));
     }
 
     MULTI INTVAL is_equal(DEFAULT value) {
@@ -775,10 +837,8 @@
 
 */
     MULTI INTVAL cmp(LuaNumber value) {
-        FLOATVAL diff = PMC_num_val(SELF)
-            - VTABLE_get_number(INTERP, value);
-
-        return diff > 0 ? (INTVAL)1 : diff < 0 ? (INTVAL)-1 : (INTVAL)0;
+        const FLOATVAL diff = SELF.get_number() - VTABLE_get_number(INTERP, value);
+        return diff > 0 ? 1 : diff < 0 ? -1 : 0;
     }
 
     MULTI INTVAL cmp(DEFAULT value) {
@@ -1341,7 +1401,7 @@
         PMC *retval = pmc_new(INTERP, dynpmc_LuaBoolean);
 
         if (PMC_type(SELF)    == PMC_type(value)
-        &&  PMC_num_val(SELF) == PMC_num_val(value))
+        &&  VTABLE_get_number(INTERP, SELF) == VTABLE_get_number(INTERP, value))
             VTABLE_set_integer_native(INTERP, retval, 1);
         else
             VTABLE_set_integer_native(INTERP, retval, 0);

Modified: trunk/languages/lua/src/pmc/luatable.pmc
==============================================================================
--- trunk/languages/lua/src/pmc/luatable.pmc	Wed Feb  4 12:44:29 2009	(r36349)
+++ trunk/languages/lua/src/pmc/luatable.pmc	Wed Feb  4 13:59:38 2009	(r36350)
@@ -63,7 +63,7 @@
 
 /******************************************************************************/
 
-static int lua_equalObj(PARROT_INTERP, const PMC *t1, const PMC *t2)
+static int lua_equalObj(PARROT_INTERP, PMC * const t1, PMC * const t2)
 {
     if (!t2)
         return 0;
@@ -72,13 +72,14 @@
         return 0;
 
     if (PMC_type(t1) == dynpmc_LuaNumber)
-        return PMC_num_val(t1) == PMC_num_val(t2);
+        return VTABLE_get_number(interp, t1) == VTABLE_get_number(interp, t2);
 
     if (PMC_type(t1) == dynpmc_LuaBoolean)
-        return PMC_int_val(t1) == PMC_int_val(t2);
+        return VTABLE_get_integer(interp, t1) == VTABLE_get_integer(interp, t2);
 
     if (PMC_type(t1) == dynpmc_LuaString)
-        return 0 == Parrot_str_compare(interp, PMC_str_val(t1), PMC_str_val(t2));
+        return 0 == Parrot_str_compare(interp, VTABLE_get_string(interp, t1),
+                                               VTABLE_get_string(interp, t2));
 
     return PMC_struct_val(t1) == PMC_struct_val(t2);
 }
@@ -95,10 +96,10 @@
         return NULL;
 
     if (PMC_type(key) == dynpmc_LuaNumber) {
-        h = (unsigned long)(long)PMC_num_val(key);
+        h = (unsigned long)(long)VTABLE_get_number(interp, key);
     }
     else if (PMC_type(key) == dynpmc_LuaBoolean) {
-        h = PMC_int_val(key);
+        h = VTABLE_get_integer(interp, key);
     }
     else if (PMC_type(key) == dynpmc_LuaString) {
         h = Parrot_str_to_hashval(interp, VTABLE_get_string(interp, key));
@@ -122,7 +123,7 @@
 
     do {
         if (PMC_type(n->key) == dynpmc_LuaString
-         && 0 == Parrot_str_compare(interp, PMC_str_val(n->key), key))
+         && 0 == Parrot_str_compare(interp, VTABLE_get_string(interp, n->key), key))
             return &n->val;
 
         n = n->next;
@@ -409,7 +410,7 @@
                     Parrot_str_new_constant(INTERP, "__mode"));
 #endif
             if (m && *m)
-                mode = PMC_str_val(*m);
+                mode = VTABLE_get_string(INTERP, *m);
         }
 
         if (PMC_struct_val(SELF))


More information about the parrot-commits mailing list