Ticket #1349: arrayiterator.pmc.patch
File arrayiterator.pmc.patch, 11.6 KB (added by jimmy, 12 years ago) |
---|
-
src/pmc/arrayiterator.pmc
85 85 */ 86 86 87 87 VTABLE void init_pmc(PMC *array) { 88 Parrot_ArrayIterator_attributes * const attrs =89 (Parrot_ArrayIterator_attributes *) PMC_data(SELF);90 88 91 attrs->array = array;89 SET_ATTR_array(interp, SELF, array); 92 90 93 91 PObj_custom_mark_SET(SELF); 94 92 95 93 /* by default, iterate from start */ 96 S ELF.set_integer_native(ITERATE_FROM_START);94 STATICSELF.set_integer_native(ITERATE_FROM_START); 97 95 } 98 96 99 97 /* … … 108 106 109 107 VTABLE void mark() { 110 108 PMC *array; 109 111 110 GET_ATTR_array(INTERP, SELF, array); 111 112 112 Parrot_gc_mark_PMC_alive(INTERP, array); 113 113 } 114 114 … … 120 120 121 121 */ 122 122 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; 129 126 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 132 136 return clone; 133 137 } 134 138 … … 143 147 */ 144 148 145 149 VTABLE INTVAL get_bool() { 146 return S ELF.elements() > 0;150 return STATICSELF.elements() > 0; 147 151 } 148 152 149 153 /* … … 157 161 */ 158 162 159 163 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 } 166 177 } 167 178 168 179 VTABLE INTVAL get_integer() { 169 return S ELF.elements();180 return STATICSELF.elements(); 170 181 } 171 182 172 183 /* … … 183 194 */ 184 195 185 196 VTABLE void set_integer_native(INTVAL value) { 186 P arrot_ArrayIterator_attributes * const attrs =187 PARROT_ARRAYITERATOR(SELF);197 PMC *array; 198 188 199 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)); 192 204 } 193 205 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); 197 212 } 198 213 else 199 214 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION, … … 228 243 */ 229 244 230 245 VTABLE INTVAL shift_integer() { 231 Parrot_ArrayIterator_attributes * const attrs =232 PARROT_ARRAYITERATOR(SELF);246 INTVAL pos, length; 247 PMC *array; 233 248 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) 235 253 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 236 254 "StopIteration"); 237 255 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); 239 260 } 240 261 241 262 /* … … 247 268 */ 248 269 249 270 VTABLE FLOATVAL shift_float() { 250 Parrot_ArrayIterator_attributes * const attrs =251 PARROT_ARRAYITERATOR(SELF);271 INTVAL pos; 272 PMC *array; 252 273 274 GET_ATTR_pos(INTERP, SELF, pos); 275 253 276 if (!STATICSELF.get_bool()) 254 277 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 255 278 "StopIteration"); 256 279 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); 258 284 } 259 285 260 286 … … 267 293 */ 268 294 269 295 VTABLE STRING *shift_string() { 270 Parrot_ArrayIterator_attributes * const attrs =271 PARROT_ARRAYITERATOR(SELF);296 INTVAL pos; 297 PMC *array; 272 298 299 GET_ATTR_pos(INTERP, SELF, pos); 300 273 301 if (!STATICSELF.get_bool()) 274 302 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 275 303 "StopIteration"); 276 304 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); 278 309 } 279 310 280 311 /* … … 289 320 */ 290 321 291 322 VTABLE PMC *shift_pmc() { 292 Parrot_ArrayIterator_attributes * const attrs =293 PARROT_ARRAYITERATOR(SELF);323 INTVAL pos; 324 PMC *array; 294 325 326 GET_ATTR_pos(INTERP, SELF, pos); 327 328 295 329 if (!STATICSELF.get_bool()) 296 330 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 297 331 "StopIteration"); 298 332 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); 300 337 } 301 338 302 339 … … 312 349 */ 313 350 314 351 VTABLE INTVAL pop_integer() { 315 Parrot_ArrayIterator_attributes * const attrs =316 PARROT_ARRAYITERATOR(SELF);352 INTVAL pos; 353 PMC *array; 317 354 355 GET_ATTR_pos(INTERP, SELF, pos); 356 357 318 358 if (!STATICSELF.get_bool()) 319 359 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 320 360 "StopIteration"); 321 361 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); 323 366 } 324 367 325 368 /* … … 331 374 */ 332 375 333 376 VTABLE FLOATVAL pop_float() { 334 Parrot_ArrayIterator_attributes * const attrs =335 PARROT_ARRAYITERATOR(SELF);377 INTVAL pos; 378 PMC *array; 336 379 380 GET_ATTR_pos(INTERP, SELF, pos); 381 337 382 if (!STATICSELF.get_bool()) 338 383 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 339 384 "StopIteration"); 340 385 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); 342 390 } 343 391 344 392 … … 351 399 */ 352 400 353 401 VTABLE STRING *pop_string() { 354 Parrot_ArrayIterator_attributes * const attrs =355 PARROT_ARRAYITERATOR(SELF);402 INTVAL pos; 403 PMC *array; 356 404 405 GET_ATTR_pos(INTERP, SELF, pos); 406 357 407 if (!STATICSELF.get_bool()) 358 408 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 359 409 "StopIteration"); 360 410 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); 362 415 } 363 416 364 417 /* … … 373 426 */ 374 427 375 428 VTABLE PMC *pop_pmc() { 376 Parrot_ArrayIterator_attributes * const attrs =377 PARROT_ARRAYITERATOR(SELF);429 INTVAL pos; 430 PMC *array; 378 431 432 GET_ATTR_pos(INTERP, SELF, pos); 433 379 434 if (!STATICSELF.get_bool()) 380 435 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, 381 436 "StopIteration"); 382 437 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); 384 442 } 385 443 386 444 /* … … 407 465 */ 408 466 409 467 VTABLE PMC *get_pmc_keyed_int(INTVAL idx) { 468 INTVAL pos; 469 470 GET_ATTR_pos(INTERP, SELF, pos); 471 410 472 return VTABLE_get_pmc_keyed_int(INTERP, STATICSELF.get_pmc(), 411 PARROT_ARRAYITERATOR(SELF)->pos + idx);473 pos + idx); 412 474 } 413 475 /* 414 476 … … 433 495 */ 434 496 435 497 VTABLE INTVAL get_integer_keyed_int(INTVAL idx) { 498 INTVAL pos; 499 500 GET_ATTR_pos(INTERP, SELF, pos); 501 436 502 return VTABLE_get_integer_keyed_int(INTERP, STATICSELF.get_pmc(), 437 PARROT_ARRAYITERATOR(SELF)->pos + idx);503 pos + idx); 438 504 } 439 505 440 506 /* … … 460 526 */ 461 527 462 528 VTABLE FLOATVAL get_number_keyed_int(INTVAL idx) { 529 INTVAL pos; 530 531 GET_ATTR_pos(INTERP, SELF, pos); 532 463 533 return VTABLE_get_number_keyed_int(INTERP, STATICSELF.get_pmc(), 464 PARROT_ARRAYITERATOR(SELF)->pos + idx);534 pos + idx); 465 535 } 466 536 467 537 … … 489 559 */ 490 560 491 561 VTABLE STRING *get_string_keyed_int(INTVAL idx) { 562 INTVAL pos; 563 564 GET_ATTR_pos(INTERP, SELF, pos); 565 492 566 return VTABLE_get_string_keyed_int(INTERP, STATICSELF.get_pmc(), 493 PARROT_ARRAYITERATOR(SELF)->pos + idx);567 pos + idx); 494 568 } 495 569 496 570 /* … … 518 592 */ 519 593 520 594 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 523 602 /* Cheat! */ 524 const INTVAL final_pos = attrs->pos + idx - attrs->reverse;603 final_pos = pos + idx - reverse; 525 604 526 return VTABLE_exists_keyed_int(INTERP, a ttrs->array, final_pos);605 return VTABLE_exists_keyed_int(INTERP, array, final_pos); 527 606 } 528 607 529 608 /* … … 549 628 */ 550 629 551 630 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 554 638 /* Cheat! */ 555 const INTVAL final_pos = attrs->pos + idx - attrs->reverse;639 final_pos = pos + idx - reverse; 556 640 557 return VTABLE_defined_keyed_int(INTERP, a ttrs->array, final_pos);641 return VTABLE_defined_keyed_int(INTERP, array, final_pos); 558 642 } 559 643 } 560 644