Ticket #1349: arrayiterator.pmc.patch

File arrayiterator.pmc.patch, 11.6 KB (added by jimmy, 12 years ago)
  • src/pmc/arrayiterator.pmc

     
    8585*/ 
    8686 
    8787    VTABLE void init_pmc(PMC *array) { 
    88         Parrot_ArrayIterator_attributes * const attrs = 
    89             (Parrot_ArrayIterator_attributes *) PMC_data(SELF); 
    9088 
    91         attrs->array     = array; 
     89        SET_ATTR_array(interp, SELF, array); 
    9290 
    9391        PObj_custom_mark_SET(SELF); 
    9492 
    9593        /* by default, iterate from start */ 
    96         SELF.set_integer_native(ITERATE_FROM_START); 
     94        STATICSELF.set_integer_native(ITERATE_FROM_START); 
    9795    } 
    9896 
    9997/* 
     
    108106 
    109107    VTABLE void mark() { 
    110108        PMC *array; 
     109 
    111110        GET_ATTR_array(INTERP, SELF, array); 
     111 
    112112        Parrot_gc_mark_PMC_alive(INTERP, array); 
    113113    } 
    114114 
     
    120120 
    121121*/ 
    122122    VTABLE PMC* clone() { 
    123         Parrot_ArrayIterator_attributes * const attrs = 
    124                 PARROT_ARRAYITERATOR(SELF); 
    125         PMC                             * const clone = 
    126                 pmc_new_init(INTERP, enum_class_ArrayIterator, attrs->array); 
    127         Parrot_ArrayIterator_attributes * const clone_attrs = 
    128                 PARROT_ARRAYITERATOR(clone); 
     123        INTVAL pos, reverse; 
     124        PMC   *array; 
     125        PMC   *clone; 
    129126 
    130         clone_attrs->pos     = attrs->pos; 
    131         clone_attrs->reverse = attrs->reverse; 
     127        GET_ATTR_array(interp, SELF, array); 
     128        GET_ATTR_pos(interp, SELF, pos); 
     129        GET_ATTR_reverse(interp, SELF, reverse); 
     130 
     131        clone = pmc_new_init(INTERP, enum_class_ArrayIterator, array); 
     132 
     133        SET_ATTR_pos(interp, clone, pos); 
     134        SET_ATTR_reverse(interp, clone, reverse); 
     135 
    132136        return clone; 
    133137    } 
    134138 
     
    143147*/ 
    144148 
    145149    VTABLE INTVAL get_bool() { 
    146         return SELF.elements() > 0; 
     150        return STATICSELF.elements() > 0; 
    147151    } 
    148152 
    149153/* 
     
    157161*/ 
    158162 
    159163    VTABLE INTVAL elements() { 
    160         Parrot_ArrayIterator_attributes * const attrs = 
    161                 PARROT_ARRAYITERATOR(SELF); 
    162         if (attrs->reverse) 
    163             return attrs->pos; 
    164         else 
    165             return attrs->length - attrs->pos; 
     164        INTVAL reverse, pos, length; 
     165 
     166        GET_ATTR_reverse(interp, SELF, reverse); 
     167 
     168        if (reverse) { 
     169            GET_ATTR_pos(interp, SELF, pos); 
     170            return pos; 
     171        } 
     172        else { 
     173            GET_ATTR_length(interp, SELF, length); 
     174            GET_ATTR_pos(interp, SELF, pos); 
     175            return length - pos; 
     176        } 
    166177    } 
    167178 
    168179    VTABLE INTVAL get_integer() { 
    169         return SELF.elements(); 
     180        return STATICSELF.elements(); 
    170181    } 
    171182 
    172183/* 
     
    183194*/ 
    184195 
    185196    VTABLE void set_integer_native(INTVAL value) { 
    186         Parrot_ArrayIterator_attributes * const attrs = 
    187                 PARROT_ARRAYITERATOR(SELF); 
     197        PMC *array; 
     198         
    188199        if (value == ITERATE_FROM_START) { 
    189             attrs->reverse   = 0; 
    190             attrs->pos       = 0; 
    191             attrs->length    = VTABLE_elements(INTERP, attrs->array); 
     200            GET_ATTR_array(interp, SELF, array); 
     201            SET_ATTR_reverse(interp, SELF, 0); 
     202            SET_ATTR_pos(interp, SELF, 0); 
     203            SET_ATTR_length(interp, SELF, VTABLE_elements(INTERP, array)); 
    192204        } 
    193205        else if (value == ITERATE_FROM_END) { 
    194             attrs->reverse   = 1; 
    195             attrs->pos       = attrs->length 
    196                              = VTABLE_elements(INTERP, attrs->array); 
     206            INTVAL element; 
     207            GET_ATTR_array(interp, SELF, array); 
     208            element = VTABLE_elements(INTERP, array); 
     209            SET_ATTR_reverse(interp, SELF, 1); 
     210            SET_ATTR_length(interp, SELF, element); 
     211            SET_ATTR_pos(interp, SELF, element); 
    197212        } 
    198213        else 
    199214            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION, 
     
    228243*/ 
    229244 
    230245    VTABLE INTVAL shift_integer() { 
    231         Parrot_ArrayIterator_attributes * const attrs = 
    232                 PARROT_ARRAYITERATOR(SELF); 
     246        INTVAL pos, length; 
     247        PMC   *array; 
    233248 
    234         if (attrs->pos >= attrs->length) 
     249        GET_ATTR_pos(INTERP, SELF, pos); 
     250        GET_ATTR_length(INTERP, SELF, length); 
     251 
     252        if (pos >= length) 
    235253            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 
    236254                "StopIteration"); 
    237255 
    238         return VTABLE_get_integer_keyed_int(INTERP, attrs->array, attrs->pos++); 
     256        GET_ATTR_array(INTERP, SELF, array); 
     257        SET_ATTR_pos(INTERP, SELF, pos+1); 
     258 
     259        return VTABLE_get_integer_keyed_int(INTERP, array, pos); 
    239260    } 
    240261 
    241262/* 
     
    247268*/ 
    248269 
    249270    VTABLE FLOATVAL shift_float() { 
    250         Parrot_ArrayIterator_attributes * const attrs = 
    251                 PARROT_ARRAYITERATOR(SELF); 
     271        INTVAL pos; 
     272        PMC   *array; 
    252273 
     274        GET_ATTR_pos(INTERP, SELF, pos); 
     275 
    253276        if (!STATICSELF.get_bool()) 
    254277            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 
    255278                "StopIteration"); 
    256279 
    257         return VTABLE_get_number_keyed_int(INTERP, attrs->array, attrs->pos++); 
     280        GET_ATTR_array(INTERP, SELF, array); 
     281        SET_ATTR_pos(INTERP, SELF, pos+1); 
     282 
     283        return VTABLE_get_number_keyed_int(INTERP, array, pos); 
    258284    } 
    259285 
    260286 
     
    267293*/ 
    268294 
    269295    VTABLE STRING *shift_string() { 
    270         Parrot_ArrayIterator_attributes * const attrs = 
    271                 PARROT_ARRAYITERATOR(SELF); 
     296        INTVAL pos; 
     297        PMC   *array; 
    272298 
     299        GET_ATTR_pos(INTERP, SELF, pos); 
     300 
    273301        if (!STATICSELF.get_bool()) 
    274302            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 
    275303                "StopIteration"); 
    276304 
    277         return VTABLE_get_string_keyed_int(INTERP, attrs->array, attrs->pos++); 
     305        GET_ATTR_array(INTERP, SELF, array); 
     306        SET_ATTR_pos(INTERP, SELF, pos+1); 
     307 
     308        return VTABLE_get_string_keyed_int(INTERP, array, pos); 
    278309    } 
    279310 
    280311/* 
     
    289320*/ 
    290321 
    291322    VTABLE PMC *shift_pmc() { 
    292         Parrot_ArrayIterator_attributes * const attrs = 
    293                 PARROT_ARRAYITERATOR(SELF); 
     323        INTVAL pos; 
     324        PMC   *array; 
    294325 
     326        GET_ATTR_pos(INTERP, SELF, pos); 
     327 
     328 
    295329        if (!STATICSELF.get_bool()) 
    296330            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 
    297331                "StopIteration"); 
    298332 
    299         return VTABLE_get_pmc_keyed_int(INTERP, attrs->array, attrs->pos++); 
     333        GET_ATTR_array(INTERP, SELF, array); 
     334        SET_ATTR_pos(INTERP, SELF, pos+1); 
     335 
     336        return VTABLE_get_pmc_keyed_int(INTERP, array, pos); 
    300337    } 
    301338 
    302339 
     
    312349*/ 
    313350 
    314351    VTABLE INTVAL pop_integer() { 
    315         Parrot_ArrayIterator_attributes * const attrs = 
    316                 PARROT_ARRAYITERATOR(SELF); 
     352        INTVAL pos; 
     353        PMC   *array; 
    317354 
     355        GET_ATTR_pos(INTERP, SELF, pos); 
     356 
     357 
    318358        if (!STATICSELF.get_bool()) 
    319359            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 
    320360                "StopIteration"); 
    321361 
    322         return VTABLE_get_integer_keyed_int(INTERP, attrs->array, --attrs->pos); 
     362        GET_ATTR_array(INTERP, SELF, array); 
     363        SET_ATTR_pos(INTERP, SELF, --pos); 
     364 
     365        return VTABLE_get_integer_keyed_int(INTERP, array, pos); 
    323366    } 
    324367 
    325368/* 
     
    331374*/ 
    332375 
    333376    VTABLE FLOATVAL pop_float() { 
    334         Parrot_ArrayIterator_attributes * const attrs = 
    335                 PARROT_ARRAYITERATOR(SELF); 
     377        INTVAL pos; 
     378        PMC   *array; 
    336379 
     380        GET_ATTR_pos(INTERP, SELF, pos); 
     381 
    337382        if (!STATICSELF.get_bool()) 
    338383            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 
    339384                "StopIteration"); 
    340385 
    341         return VTABLE_get_number_keyed_int(INTERP, attrs->array, --attrs->pos); 
     386        GET_ATTR_array(INTERP, SELF, array); 
     387        SET_ATTR_pos(INTERP, SELF, --pos); 
     388 
     389        return VTABLE_get_number_keyed_int(INTERP, array, pos); 
    342390    } 
    343391 
    344392 
     
    351399*/ 
    352400 
    353401    VTABLE STRING *pop_string() { 
    354         Parrot_ArrayIterator_attributes * const attrs = 
    355                 PARROT_ARRAYITERATOR(SELF); 
     402        INTVAL pos; 
     403        PMC   *array; 
    356404 
     405        GET_ATTR_pos(INTERP, SELF, pos); 
     406 
    357407        if (!STATICSELF.get_bool()) 
    358408            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 
    359409                "StopIteration"); 
    360410 
    361         return VTABLE_get_string_keyed_int(INTERP, attrs->array, --attrs->pos); 
     411        GET_ATTR_array(INTERP, SELF, array); 
     412        SET_ATTR_pos(INTERP, SELF, --pos); 
     413 
     414        return VTABLE_get_string_keyed_int(INTERP, array, pos); 
    362415    } 
    363416 
    364417/* 
     
    373426*/ 
    374427 
    375428    VTABLE PMC *pop_pmc() { 
    376         Parrot_ArrayIterator_attributes * const attrs = 
    377                 PARROT_ARRAYITERATOR(SELF); 
     429        INTVAL pos; 
     430        PMC   *array; 
    378431 
     432        GET_ATTR_pos(INTERP, SELF, pos); 
     433 
    379434        if (!STATICSELF.get_bool()) 
    380435            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 
    381436                "StopIteration"); 
    382437 
    383         return VTABLE_get_pmc_keyed_int(INTERP, attrs->array, --attrs->pos); 
     438        GET_ATTR_array(INTERP, SELF, array); 
     439        SET_ATTR_pos(INTERP, SELF, --pos); 
     440 
     441        return VTABLE_get_pmc_keyed_int(INTERP, array, pos); 
    384442    } 
    385443 
    386444/* 
     
    407465*/ 
    408466 
    409467    VTABLE PMC *get_pmc_keyed_int(INTVAL idx) { 
     468        INTVAL pos; 
     469 
     470        GET_ATTR_pos(INTERP, SELF, pos); 
     471 
    410472        return VTABLE_get_pmc_keyed_int(INTERP, STATICSELF.get_pmc(), 
    411                 PARROT_ARRAYITERATOR(SELF)->pos + idx); 
     473                pos + idx); 
    412474    } 
    413475/* 
    414476 
     
    433495*/ 
    434496 
    435497    VTABLE INTVAL get_integer_keyed_int(INTVAL idx) { 
     498        INTVAL pos; 
     499 
     500        GET_ATTR_pos(INTERP, SELF, pos); 
     501 
    436502        return VTABLE_get_integer_keyed_int(INTERP, STATICSELF.get_pmc(), 
    437                 PARROT_ARRAYITERATOR(SELF)->pos + idx); 
     503                pos + idx); 
    438504    } 
    439505 
    440506/* 
     
    460526*/ 
    461527 
    462528    VTABLE FLOATVAL get_number_keyed_int(INTVAL idx) { 
     529        INTVAL pos; 
     530 
     531        GET_ATTR_pos(INTERP, SELF, pos); 
     532 
    463533        return VTABLE_get_number_keyed_int(INTERP, STATICSELF.get_pmc(), 
    464                 PARROT_ARRAYITERATOR(SELF)->pos + idx); 
     534                pos + idx); 
    465535    } 
    466536 
    467537 
     
    489559*/ 
    490560 
    491561    VTABLE STRING *get_string_keyed_int(INTVAL idx) { 
     562        INTVAL pos; 
     563 
     564        GET_ATTR_pos(INTERP, SELF, pos); 
     565 
    492566        return VTABLE_get_string_keyed_int(INTERP, STATICSELF.get_pmc(), 
    493                 PARROT_ARRAYITERATOR(SELF)->pos + idx); 
     567                pos + idx); 
    494568    } 
    495569 
    496570/* 
     
    518592*/ 
    519593 
    520594    VTABLE INTVAL exists_keyed_int(INTVAL idx) { 
    521         Parrot_ArrayIterator_attributes * const attrs = 
    522                 PARROT_ARRAYITERATOR(SELF); 
     595        INTVAL pos, reverse, final_pos; 
     596        PMC   *array; 
     597 
     598        GET_ATTR_pos(INTERP, SELF, pos); 
     599        GET_ATTR_reverse(INTERP, SELF, reverse); 
     600        GET_ATTR_array(INTERP, SELF, array); 
     601 
    523602        /* Cheat! */ 
    524         const INTVAL final_pos = attrs->pos + idx - attrs->reverse; 
     603        final_pos = pos + idx - reverse; 
    525604 
    526         return VTABLE_exists_keyed_int(INTERP, attrs->array, final_pos); 
     605        return VTABLE_exists_keyed_int(INTERP, array, final_pos); 
    527606    } 
    528607 
    529608/* 
     
    549628*/ 
    550629 
    551630    VTABLE INTVAL defined_keyed_int(INTVAL idx) { 
    552         Parrot_ArrayIterator_attributes * const attrs = 
    553                 PARROT_ARRAYITERATOR(SELF); 
     631        INTVAL pos, reverse, final_pos; 
     632        PMC   *array; 
     633 
     634        GET_ATTR_pos(INTERP, SELF, pos); 
     635        GET_ATTR_reverse(INTERP, SELF, reverse); 
     636        GET_ATTR_array(INTERP, SELF, array); 
     637 
    554638        /* Cheat! */ 
    555         const INTVAL final_pos = attrs->pos + idx - attrs->reverse; 
     639        final_pos = pos + idx - reverse; 
    556640 
    557         return VTABLE_defined_keyed_int(INTERP, attrs->array, final_pos); 
     641        return VTABLE_defined_keyed_int(INTERP, array, final_pos); 
    558642    } 
    559643} 
    560644