2 Copyright (C) 2004-2010, Parrot Foundation.
7 src/pmc/undef.pmc - Generic undefined value
11 This is Parrot's generic undef type. This PMC has no defined value.
12 It returns a numeric value of 0, a boolean of false, and an empty string.
13 When assigned a number, integer, or string it morphs to a Number,
14 Integer, or String PMC respectively, and when assigned a generic PMC
15 it morphs into a PMC of the passed-in type and does a same-type assignment
26 #include "pmc/pmc_object.h"
27 #define UNDEF_STRING_CLASS enum_class_String
29 /* HEADERIZER HFILE: none */
30 /* HEADERIZER BEGIN: static */
31 /* HEADERIZER END: static */
37 =item C<void set_pmc(PMC *other)>
39 Sets the current PMC to C<*other> by first changing the current PMC to the
46 VTABLE void set_pmc(PMC *other) {
47 if (!PObj_is_object_TEST(other)) {
48 Parrot_pmc_reuse(INTERP, SELF, other->vtable->base_type, 0);
49 VTABLE_set_pmc(INTERP, SELF, other);
52 PMC * const class_ = PARROT_OBJECT(other)->_class;
53 PMC *clone = VTABLE_clone(INTERP, other);
54 void *attrs = PMC_data(clone);
55 PMC *meta = PMC_metadata(clone);
57 /* now swap memory without leaking it */
58 PMC_data(clone) = PMC_data(SELF);
59 PMC_data(SELF) = attrs;
60 SELF->vtable = clone->vtable;
62 /* Restore metadata. */
63 if (!PMC_IS_NULL(meta)) {
64 PMC * const iter = VTABLE_get_iter(INTERP, meta);
65 while (VTABLE_get_bool(INTERP, iter)) {
66 STRING * const key = VTABLE_shift_string(INTERP, iter);
67 PMC * const value = VTABLE_get_pmc_keyed_str(INTERP, meta, key);
68 VTABLE_setprop(INTERP, SELF, key, value);
71 PMC_data(clone) = NULL;
73 PObj_is_object_SET(SELF);
79 =item C<void assign_pmc(PMC *other)>
81 Assigns the PMC to the value of C<*other> by first changing the PMC to the
88 VTABLE void assign_pmc(PMC *other) {
89 /* don't want to call set_pmc if we're assigning an Undef to an Undef */
90 if (other->vtable->base_type != enum_class_Undef)
91 VTABLE_set_pmc(INTERP, SELF, other);
97 =item C<INTVAL get_integer()>
105 VTABLE INTVAL get_integer() {
111 =item C<INTVAL defined()>
119 VTABLE INTVAL defined() {
125 =item C<void set_integer_native(INTVAL value)>
127 Morphs the current PMC to an C<Integer> and sets the value from C<value>.
133 VTABLE void set_integer_native(INTVAL value) {
134 Parrot_pmc_reuse(INTERP, SELF, enum_class_Integer, 0);
135 VTABLE_set_integer_native(INTERP, SELF, value);
140 =item C<FLOATVAL get_number()>
148 VTABLE FLOATVAL get_number() {
154 =item C<void set_number_native(FLOATVAL value)>
156 Morphs the current PMC to a C<Float> and sets the value from C<value>.
162 VTABLE void set_number_native(FLOATVAL value) {
163 Parrot_pmc_reuse(INTERP, SELF, enum_class_Float, 0);
164 VTABLE_set_number_native(INTERP, SELF, value);
169 =item C<STRING *get_string()>
171 Returns an empty string.
177 VTABLE STRING *get_string() {
178 Parrot_warn(INTERP, PARROT_WARNINGS_UNDEF_FLAG,
179 "Stringifying an Undef PMC");
181 return CONST_STRING(INTERP, "");
186 =item C<void set_string_native(STRING *value)>
188 Morphs the current PMC to a C<String> and sets the value from C<value>.
194 VTABLE void set_string_native(STRING *value) {
195 Parrot_pmc_reuse(INTERP, SELF, enum_class_String, 0);
196 VTABLE_set_string_native(INTERP, SELF, value);
201 =item C<INTVAL get_bool()>
209 VTABLE INTVAL get_bool() {
215 =item C<void share()>
223 VTABLE void share() {
224 /* see src/pmc/integer.pmc */
229 =item C<PMC *share_ro()>
237 VTABLE PMC *share_ro() {
238 if (PObj_is_PMC_shared_TEST(SELF))
241 return pt_shared_fixup(INTERP, SELF);
246 =item C<PMC *clone()>
248 Clones the current Undef PMC.
254 VTABLE PMC *clone() {
255 return Parrot_pmc_new(INTERP, SELF->vtable->base_type);
260 =item C<INTVAL is_equal(PMC *value)>
262 Returns 1 if the C<*value> is an Undef PMC, 0 otherwise.
268 MULTI INTVAL is_equal(Undef value) {
272 MULTI INTVAL is_equal(DEFAULT value) {
288 * c-file-style: "parrot"
290 * vim: expandtab shiftwidth=4: