Skip to content
This repository
tree: de6a984c5e
Fetching contributors…

Cannot retrieve contributors at this time

file 289 lines (188 sloc) 5.067 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
/*
Copyright (C) 2004-2012, Parrot Foundation.

=head1 NAME

src/pmc/undef.pmc - Generic undefined value

=head1 DESCRIPTION

This is Parrot's generic undef type. This PMC has no defined value.
It returns a numeric value of 0, a boolean of false, and an empty string.
When assigned a number, integer, or string it morphs to a Number,
Integer, or String PMC respectively, and when assigned a generic PMC
it morphs into a PMC of the passed-in type and does a same-type assignment
from there.

=head2 Methods

=over 4

=cut

*/

#include "pmc/pmc_object.h"
#define UNDEF_STRING_CLASS enum_class_String

/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */

pmclass Undef no_ro {

/*

=item C<void set_pmc(PMC *other)>

Sets the current PMC to C<*other> by first changing the current PMC to the
appropriate type.

=cut

*/

    VTABLE void set_pmc(PMC *other) {
        if (!PObj_is_object_TEST(other)) {
            Parrot_pmc_reuse(INTERP, SELF, other->vtable->base_type, 0);
            VTABLE_set_pmc(INTERP, SELF, other);
        }
        else {
            PMC * const clone = VTABLE_clone(INTERP, other);
            void * const attrs = PMC_data(clone);
            PMC * const meta = PMC_metadata(clone);

            /* now swap memory without leaking it */
            PMC_data(clone) = PMC_data(SELF);
            PMC_data(SELF) = attrs;
            SELF->vtable = clone->vtable;

            /* Restore metadata. */
            if (!PMC_IS_NULL(meta)) {
                PMC * const iter = VTABLE_get_iter(INTERP, meta);
                while (VTABLE_get_bool(INTERP, iter)) {
                    STRING * const key = VTABLE_shift_string(INTERP, iter);
                    PMC * const value = VTABLE_get_pmc_keyed_str(INTERP, meta, key);
                    Parrot_pmc_setprop(INTERP, SELF, key, value);
                }
            }
            PMC_data(clone) = NULL;

            PObj_is_object_SET(SELF);
        }
    }

/*

=item C<void assign_pmc(PMC *other)>

Assigns the PMC to the value of C<*other> by first changing the PMC to the
appropriate type.

=cut

*/

    VTABLE void assign_pmc(PMC *other) {
        /* don't want to call set_pmc if we're assigning an Undef to an Undef */
        if (other->vtable->base_type != enum_class_Undef)
            VTABLE_set_pmc(INTERP, SELF, other);
    }


/*

=item C<INTVAL get_integer()>

Returns 0.

=cut

*/

    VTABLE INTVAL get_integer() {
        UNUSED(SELF)
        UNUSED(INTERP)
        return 0;
    }

/*

=item C<INTVAL defined()>

Returns 0.

=cut

*/

    VTABLE INTVAL defined() {
        UNUSED(SELF)
        UNUSED(INTERP)
        return 0;
    }

/*

=item C<void set_integer_native(INTVAL value)>

Morphs the current PMC to an C<Integer> and sets the value from C<value>.

=cut

*/

    VTABLE void set_integer_native(INTVAL value) {
        Parrot_pmc_reuse(INTERP, SELF, enum_class_Integer, 0);
        VTABLE_set_integer_native(INTERP, SELF, value);
    }

/*

=item C<FLOATVAL get_number()>

Returns 0.0.

=cut

*/

    VTABLE FLOATVAL get_number() {
        UNUSED(SELF)
        UNUSED(INTERP)
        return 0.0;
    }

/*

=item C<void set_number_native(FLOATVAL value)>

Morphs the current PMC to a C<Float> and sets the value from C<value>.

=cut

*/

    VTABLE void set_number_native(FLOATVAL value) {
        Parrot_pmc_reuse(INTERP, SELF, enum_class_Float, 0);
        VTABLE_set_number_native(INTERP, SELF, value);
    }

/*

=item C<STRING *get_string()>

Returns an empty string.

=cut

*/

    VTABLE STRING *get_string() {
        UNUSED(SELF)
        Parrot_warn(INTERP, PARROT_WARNINGS_UNDEF_FLAG,
            "Stringifying an Undef PMC");

        return CONST_STRING(INTERP, "");
    }

/*

=item C<void set_string_native(STRING *value)>

Morphs the current PMC to a C<String> and sets the value from C<value>.

=cut

*/

    VTABLE void set_string_native(STRING *value) {
        Parrot_pmc_reuse(INTERP, SELF, enum_class_String, 0);
        VTABLE_set_string_native(INTERP, SELF, value);
    }

/*

=item C<INTVAL get_bool()>

Returns 0.

=cut

*/

    VTABLE INTVAL get_bool() {
        UNUSED(SELF)
        UNUSED(INTERP)
        return 0;
    }

/*

=item C<void share()>

Unknown. (TODO)

=cut

*/

    VTABLE void share() {
        UNUSED(SELF)
        UNUSED(INTERP)
        /* see src/pmc/integer.pmc */
    }

/*

=item C<PMC *clone()>

Clones the current Undef PMC.

=cut

*/

    VTABLE PMC *clone() {
        return Parrot_pmc_new(INTERP, SELF->vtable->base_type);
    }

/*

=item C<INTVAL is_equal(PMC *value)>

Returns 1 if the C<*value> is an Undef PMC, 0 otherwise.

=cut

*/

    MULTI INTVAL is_equal(Undef value) {
        UNUSED(SELF)
        UNUSED(INTERP)
        UNUSED(value)
        return 1;
    }

    MULTI INTVAL is_equal(DEFAULT value) {
        UNUSED(SELF)
        UNUSED(INTERP)
        UNUSED(value)
        return 0;
    }
}

/*

=back

=cut

*/


/*
 * Local variables:
 * c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
 */
Something went wrong with that request. Please try again.