|
| 1 | +#include "pmc_nqplexinfo.h" |
| 2 | + |
| 3 | +pmclass NQPLexPad provides hash no_ro auto_attrs dynpmc group nqp { |
| 4 | + ATTR PMC *lexinfo; |
| 5 | + ATTR PMC *ctx; |
| 6 | + |
| 7 | + VTABLE void init() { |
| 8 | + Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION, |
| 9 | + "Cannot create a NQPLexPad PMC without an initializer"); |
| 10 | + } |
| 11 | + |
| 12 | +/* |
| 13 | + |
| 14 | +=item C<init_pmc(PMC *lexinfo)> |
| 15 | + |
| 16 | +Initialize the LexPad PMC and remember the associate |
| 17 | +lexinfo. |
| 18 | + |
| 19 | +=item C<void set_pointer(void *)> |
| 20 | + |
| 21 | +Associate the context, and set into it any static entries. |
| 22 | + |
| 23 | +=item C<INTVAL elements()> |
| 24 | + |
| 25 | +Returns the number of elements in the hash. |
| 26 | + |
| 27 | +=item C<INTVAL exists_keyed(PMC *name)> |
| 28 | + |
| 29 | +=item C<INTVAL exists_keyed_str(STRING *name)> |
| 30 | + |
| 31 | +Returns whether a lexical C<name> exists in the hash. |
| 32 | + |
| 33 | +=item C<PMC *get_pmc_keyed_str(STRING *name)> |
| 34 | + |
| 35 | +=item C<PMC *get_pmc_keyed(PMC *name)> |
| 36 | + |
| 37 | +Return the lexical with the given name, or NULL (not PMCNULL), if the |
| 38 | +lexical doesn't exist. |
| 39 | + |
| 40 | +=item C<void set_pmc_keyed(PMC *name, PMC *value)> |
| 41 | + |
| 42 | +=item C<void set_pmc_keyed_str(STRING *name, PMC *value)> |
| 43 | + |
| 44 | +Set the lexical with the given name to value. If the lexical name |
| 45 | +doesn't exist, it is created. |
| 46 | + |
| 47 | +=item C<PMC *get_lexinfo()> |
| 48 | + |
| 49 | +Return the LexInfo PMC, if any or a Null PMC. |
| 50 | + |
| 51 | +=cut |
| 52 | + |
| 53 | +*/ |
| 54 | + VTABLE void init_pmc(PMC *lexinfo) { |
| 55 | + SET_ATTR_lexinfo(INTERP, SELF, lexinfo); |
| 56 | + } |
| 57 | + |
| 58 | + VTABLE void set_pointer(void *ctx) { |
| 59 | + SET_ATTR_ctx(INTERP, SELF, (PMC *)ctx); |
| 60 | + } |
| 61 | + |
| 62 | + VTABLE INTVAL elements() { |
| 63 | + PMC *info, *name_map; |
| 64 | + GET_ATTR_lexinfo(INTERP, SELF, info); |
| 65 | + GETATTR_NQPLexInfo_name_to_register_map(INTERP, info, name_map); |
| 66 | + return Parrot_hash_size(INTERP, |
| 67 | + (Hash *)VTABLE_get_pointer(INTERP, name_map)); |
| 68 | + } |
| 69 | + |
| 70 | + VTABLE INTVAL exists_keyed_str(STRING *name) { |
| 71 | + PMC *info, *name_map; |
| 72 | + const Hash *hash; |
| 73 | + GET_ATTR_lexinfo(INTERP, SELF, info); |
| 74 | + GETATTR_NQPLexInfo_name_to_register_map(INTERP, info, name_map); |
| 75 | + hash = (const Hash *)VTABLE_get_pointer(INTERP, name_map); |
| 76 | + |
| 77 | + return hash->entries |
| 78 | + ? (Parrot_hash_get_bucket(INTERP, hash, name) != 0) |
| 79 | + : 0; |
| 80 | + } |
| 81 | + |
| 82 | + VTABLE INTVAL exists_keyed(PMC *name) { |
| 83 | + STRING * const s = VTABLE_get_string(INTERP, name); |
| 84 | + return SELF.exists_keyed_str(s); |
| 85 | + } |
| 86 | + |
| 87 | + VTABLE PMC *get_pmc_keyed_str(STRING *name) { |
| 88 | + PMC *info, *name_map; |
| 89 | + const Hash *hash; |
| 90 | + PMC *ctx; |
| 91 | + HashBucket *b; |
| 92 | + |
| 93 | + GET_ATTR_lexinfo(INTERP, SELF, info); |
| 94 | + GETATTR_NQPLexInfo_name_to_register_map(INTERP, info, name_map); |
| 95 | + hash = (const Hash *)VTABLE_get_pointer(INTERP, name_map); |
| 96 | + |
| 97 | + if (!hash->entries) |
| 98 | + return PMCNULL; |
| 99 | + |
| 100 | + b = Parrot_hash_get_bucket(INTERP, hash, name); |
| 101 | + |
| 102 | + if (!b) |
| 103 | + return PMCNULL; |
| 104 | + |
| 105 | + GET_ATTR_ctx(INTERP, SELF, ctx); |
| 106 | + return CTX_REG_PMC(ctx, (INTVAL)b->value); |
| 107 | + } |
| 108 | + |
| 109 | + VTABLE PMC *get_pmc_keyed(PMC *name) { |
| 110 | + STRING * const s = VTABLE_get_string(INTERP, name); |
| 111 | + return SELF.get_pmc_keyed_str(s); |
| 112 | + } |
| 113 | + |
| 114 | + VTABLE void set_pmc_keyed_str(STRING *name, PMC *value) { |
| 115 | + PMC *info, *name_map; |
| 116 | + const Hash *hash; |
| 117 | + PMC * ctx; |
| 118 | + HashBucket * b; |
| 119 | + |
| 120 | + GET_ATTR_lexinfo(INTERP, SELF, info); |
| 121 | + GETATTR_NQPLexInfo_name_to_register_map(INTERP, info, name_map); |
| 122 | + hash = (const Hash *)VTABLE_get_pointer(INTERP, name_map); |
| 123 | + b = Parrot_hash_get_bucket(INTERP, hash, name); |
| 124 | + |
| 125 | + if (!b) |
| 126 | + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_LEX_NOT_FOUND, |
| 127 | + "Lexical '%Ss' not found", name); |
| 128 | + |
| 129 | + GET_ATTR_ctx(INTERP, SELF, ctx); |
| 130 | + CTX_REG_PMC(ctx, (INTVAL)b->value) = value; |
| 131 | + PARROT_GC_WRITE_BARRIER(INTERP, ctx); |
| 132 | + } |
| 133 | + |
| 134 | + VTABLE void set_pmc_keyed(PMC *name, PMC *value) { |
| 135 | + STRING * const s = VTABLE_get_string(INTERP, name); |
| 136 | + SELF.set_pmc_keyed_str(s, value); |
| 137 | + } |
| 138 | + |
| 139 | + METHOD get_lexinfo() { |
| 140 | + PMC *lexinfo; |
| 141 | + GET_ATTR_lexinfo(INTERP, SELF, lexinfo); |
| 142 | + RETURN(PMC *lexinfo); |
| 143 | + } |
| 144 | + |
| 145 | +/* |
| 146 | + |
| 147 | +=item C<PMC *get_iter()> |
| 148 | + |
| 149 | +Get iterator for declared lexicals. |
| 150 | + |
| 151 | +=cut |
| 152 | + |
| 153 | +*/ |
| 154 | + VTABLE PMC *get_iter() { |
| 155 | + PMC *lexinfo; |
| 156 | + GET_ATTR_lexinfo(INTERP, SELF, lexinfo); |
| 157 | + return VTABLE_get_iter(INTERP, lexinfo); |
| 158 | + } |
| 159 | + |
| 160 | +} |
| 161 | + |
| 162 | +/* |
| 163 | + |
| 164 | +=back |
| 165 | + |
| 166 | +=cut |
| 167 | + |
| 168 | +*/ |
0 commit comments