2 Copyright (C) 2005-2010, Parrot Foundation.
7 src/dynpmc/dynlexpad.pmc - DynLexPad PMC
11 DynLexPad provides a more dynamic lexpad that allows the addition of
22 pmclass DynLexPad dynpmc provides lexpad auto_attrs {
24 ATTR PMC *init; /* the PMC used to initialize this DynLexPad */
27 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
28 "don't create me like this");
33 =item C<init_pmc(PMC *lexinfo)>
35 Initialize the LexPad PMC and remember the associate
41 VTABLE void init_pmc(PMC* lexinfo) {
44 Parrot_DynLexPad_attributes *attrs =
45 (Parrot_DynLexPad_attributes *) PMC_data(SELF);
47 if (VTABLE_elements(INTERP, lexinfo)) {
48 attrs->init = Parrot_pmc_new_init(INTERP, enum_class_LexPad, lexinfo);
53 hash = parrot_new_hash(INTERP);
55 PObj_custom_mark_destroy_SETALL(SELF);
60 =item C<void set_pointer(void *)>
62 Initialize the LexPad PMC and remember the associate context.
67 VTABLE void set_pointer(void* ctx) {
68 PMC *std_pad = PARROT_DYNLEXPAD(SELF)->init;
70 VTABLE_set_pointer(INTERP, std_pad, ctx);
75 =item C<INTVAL elements()>
77 Returns the number of elements in the hash.
82 VTABLE INTVAL elements() {
83 return parrot_hash_size(INTERP, PARROT_DYNLEXPAD(SELF)->hash);
88 =item C<INTVAL exists_keyed(PMC *name)>
90 =item C<INTVAL exists_keyed_str(STRING *name)>
92 Returns whether a lexical C<name> exists in the hash.
98 VTABLE INTVAL exists_keyed(PMC* name) {
99 STRING *s = VTABLE_get_string(INTERP, name);
100 return SELF.exists_keyed_str(s);
103 VTABLE INTVAL exists_keyed_str(STRING* name) {
105 if (parrot_hash_exists(INTERP, PARROT_DYNLEXPAD(SELF)->hash, name))
107 std_pad = PARROT_DYNLEXPAD(SELF)->init;
109 return VTABLE_exists_keyed_str(INTERP, std_pad, name);
115 =item C<PMC *get_pmc_keyed_str(STRING *name)>
117 =item C<PMC *get_pmc_keyed(PMC *name)>
119 Return the lexical with the given name, or NULL (not PMCNULL), if the
120 lexical doesn't exist.
125 VTABLE PMC* get_pmc_keyed_str(STRING* name) {
126 HashBucket *b = parrot_hash_get_bucket(INTERP,
127 PARROT_DYNLEXPAD(SELF)->hash, name);
130 PMC *std_pad = PARROT_DYNLEXPAD(SELF)->init;
133 return VTABLE_get_pmc_keyed_str(INTERP, std_pad, name);
138 return (PMC *)b->value;
141 VTABLE PMC* get_pmc_keyed(PMC* name) {
142 STRING *s = VTABLE_get_string(INTERP, name);
143 return SELF.get_pmc_keyed_str(s);
149 =item C<void set_pmc_keyed(PMC *name, PMC *value)>
151 =item C<void set_pmc_keyed_str(STRING *name, PMC *value)>
153 Set the lexical with the given name to value. If the lexical name
154 doesn't exist, it is created.
160 VTABLE void set_pmc_keyed(PMC* name, PMC* value) {
161 STRING *s = VTABLE_get_string(INTERP, name);
162 SELF.set_pmc_keyed_str(s, value);
165 VTABLE void set_pmc_keyed_str(STRING* name, PMC* value) {
166 PMC *std_pad = PARROT_DYNLEXPAD(SELF)->init;
168 if (PObj_constant_TEST(SELF)) {
169 if (!PObj_constant_TEST((PObj *)name))
170 Parrot_ex_throw_from_c_args(INTERP, NULL,
171 EXCEPTION_INVALID_OPERATION,
172 "Used non-constant STRING key in constant hash.");
173 if (!PObj_constant_TEST((PObj *)value))
174 Parrot_ex_throw_from_c_args(INTERP, NULL,
175 EXCEPTION_INVALID_OPERATION,
176 "Used non-constant PMC value in constant hash.");
179 if (std_pad && VTABLE_exists_keyed_str(INTERP, std_pad, name))
180 VTABLE_set_pmc_keyed_str(INTERP, std_pad, name, value);
182 parrot_hash_put(INTERP, PARROT_DYNLEXPAD(SELF)->hash, name, value);
187 =item C<void destroy()>
195 VTABLE void destroy() {
196 if (PARROT_DYNLEXPAD(SELF)->hash) {
197 parrot_hash_destroy(INTERP, PARROT_DYNLEXPAD(SELF)->hash);
198 PARROT_DYNLEXPAD(SELF)->hash = NULL;
205 Marks the lexpad hash as live.
212 PMC *std_pad = PARROT_DYNLEXPAD(SELF)->init;
213 Parrot_gc_mark_PMC_alive(INTERP, std_pad);
214 if (PARROT_DYNLEXPAD(SELF)->hash)
215 parrot_mark_hash(INTERP, PARROT_DYNLEXPAD(SELF)->hash);
228 F<docs/pdds/pdd20_lexical_vars.pod>, F<src/pmc/lexpad.pmc>,
229 F<src/pmc/lexinfo.pmc>.
237 * c-file-style: "parrot"
239 * vim: expandtab shiftwidth=4: