tagged release 0.6.4
[parrot.git] / src / dynpmc / dynlexpad.pmc
blob62e6ed82b2a3c5bb98f8fdc365cafe67df04f106
1 /*
2 Copyright (C) 2005-2007, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/dynpmc/dynlexpad.pmc - DynLexPad PMC
9 =head1 DESCRIPTION
11 DynLexPad provides a more dynamic lexpad that allows the addition of
12 lexicals at runtime.
14 =head2 Functions
16 =over 4
18 =cut
22 pmclass DynLexPad dynpmc provides lexpad need_ext {
24     VTABLE void init() {
25         real_exception(interp, NULL, INVALID_OPERATION,
26                 "don't create me like this");
27     }
31 =item C<init_pmc(PMC *lexinfo)>
33 Initialize the LexPad PMC and remember the associate
34 lexinfo.
36 =item C<void set_pointer(void *)>
38 Initialize the LexPad PMC and remember the associate context.
40 =item C<INTVAL elements()>
42 Returns the number of elements in the hash.
44 =item C<INTVAL exists_keyed(PMC *name)>
46 =item C<INTVAL exists_keyed_str(STRING *name)>
48 Returns whether a lexical C<name> exists in the hash.
50 =item C<PMC *get_pmc_keyed_str(STRING *name)>
52 =item C<PMC *get_pmc_keyed(PMC *name)>
54 Return the lexical with the given name, or NULL (not PMCNULL), if the
55 lexical doesn't exist.
57 =item C<void set_pmc_keyed(PMC *name, PMC *value)>
59 =item C<void set_pmc_keyed_str(STRING *name, PMC *value)>
61 Set the lexical with the given name to value. If the lexical name
62 doesn't exist, it is created.
64 =item C<METHOD PMC* get_lexinfo()>
66 Return the LexInfo PMC, if any or a Null PMC.
68 =cut
71     void init_pmc(PMC* lexinfo) {
72         if (VTABLE_elements(interp, lexinfo)) {
73             PMC_pmc_val(SELF) =
74                 pmc_new_init(interp, enum_class_LexPad, lexinfo);
75         }
76         else
77             PMC_pmc_val(SELF) = NULL;
78         parrot_new_pmc_hash(interp, SELF);
79         PObj_custom_mark_destroy_SETALL(SELF);
80     }
82     void set_pointer(void* ctx) {
83         PMC *std_pad = PMC_pmc_val(SELF);
84         if (std_pad)
85             VTABLE_set_pointer(interp, std_pad, ctx);
86     }
88     VTABLE void destroy() {
89         if (PMC_struct_val(SELF)) {
90             parrot_hash_destroy(interp, (Hash*) PMC_struct_val(SELF));
91             PMC_struct_val(SELF) = NULL;
92         }
93     }
96 =item C<void mark()>
98 Marks the lexpad hash as live.
100 =cut
104     VTABLE void mark() {
105         PMC *std_pad = PMC_pmc_val(SELF);
106         if (std_pad)
107             pobject_lives(interp, (PObj *)std_pad);
108         if (PMC_struct_val(SELF))
109             parrot_mark_hash(interp, (Hash *)PMC_struct_val(SELF));
110     }
112     PMC* get_pmc_keyed_str(STRING* name) {
113         HashBucket *b = parrot_hash_get_bucket(interp,
114             (Hash *)PMC_struct_val(SELF), name);
116         if (!b) {
117             PMC *std_pad = PMC_pmc_val(SELF);
119             if (std_pad)
120                 return VTABLE_get_pmc_keyed_str(interp, std_pad, name);
122             return PMCNULL;
123         }
125         return (PMC *)b->value;
126     }
128     INTVAL exists_keyed_str(STRING* name) {
129         PMC *std_pad;
130         if (parrot_hash_exists(interp, (Hash*) PMC_struct_val(SELF), name))
131             return 1;
132         std_pad = PMC_pmc_val(SELF);
133         if (std_pad)
134             return VTABLE_exists_keyed_str(interp, std_pad, name);
135         return 0;
136     }
138     INTVAL exists_keyed(PMC* name) {
139         STRING *s = key_string(interp, name);
140         return SELF.exists_keyed_str(s);
141     }
143     PMC* get_pmc_keyed(PMC* name) {
144         STRING *s = key_string(interp, name);
145         return SELF.get_pmc_keyed_str(s);
146     }
148     void set_pmc_keyed_str(STRING* name, PMC* value) {
149         PMC *std_pad = PMC_pmc_val(SELF);
151         if (std_pad && VTABLE_exists_keyed_str(interp, std_pad, name))
152             VTABLE_set_pmc_keyed_str(interp, std_pad, name, value);
154         parrot_hash_put(interp, (Hash *)PMC_struct_val(SELF), name, value);
155     }
157     void set_pmc_keyed(PMC* name, PMC* value) {
158         STRING *s = key_string(interp, name);
159         SELF.set_pmc_keyed_str(s, value);
160     }
167 =back
169 =head1 SEE ALSO
171 F<docs/pdds/pdd20_lexical_vars.pod>, F<src/pmc/lexpad.pmc>,
172 F<src/pmc/lexinfo.pmc>.
174 =cut
179  * Local variables:
180  *   c-file-style: "parrot"
181  * End:
182  * vim: expandtab shiftwidth=4:
183  */