This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
missed a file
[perl5.git] / av.c
CommitLineData
a0d0e21e 1/* av.c
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e
LW
8 */
9
10/*
11 * "...for the Entwives desired order, and plenty, and peace (by which they
12 * meant that things should remain where they had set them)." --Treebeard
79072805
LW
13 */
14
15#include "EXTERN.h"
864dbfa3 16#define PERL_IN_AV_C
79072805
LW
17#include "perl.h"
18
fb73857a 19void
864dbfa3 20Perl_av_reify(pTHX_ AV *av)
a0d0e21e
LW
21{
22 I32 key;
23 SV* sv;
fb73857a 24
3c78fafa
GS
25 if (AvREAL(av))
26 return;
93965878 27#ifdef DEBUGGING
33c27489 28 if (SvTIED_mg((SV*)av, 'P'))
93965878
NIS
29 warn("av_reify called on tied array");
30#endif
a0d0e21e 31 key = AvMAX(av) + 1;
93965878 32 while (key > AvFILLp(av) + 1)
3280af22 33 AvARRAY(av)[--key] = &PL_sv_undef;
a0d0e21e
LW
34 while (key) {
35 sv = AvARRAY(av)[--key];
36 assert(sv);
3280af22 37 if (sv != &PL_sv_undef) {
11343788 38 dTHR;
a0d0e21e 39 (void)SvREFCNT_inc(sv);
11343788 40 }
a0d0e21e 41 }
29de640a
CS
42 key = AvARRAY(av) - AvALLOC(av);
43 while (key)
3280af22 44 AvALLOC(av)[--key] = &PL_sv_undef;
62b1ebc2 45 AvREIFY_off(av);
a0d0e21e
LW
46 AvREAL_on(av);
47}
48
49void
864dbfa3 50Perl_av_extend(pTHX_ AV *av, I32 key)
a0d0e21e 51{
11343788 52 dTHR; /* only necessary if we have to extend stack */
93965878 53 MAGIC *mg;
33c27489 54 if (mg = SvTIED_mg((SV*)av, 'P')) {
93965878
NIS
55 dSP;
56 ENTER;
57 SAVETMPS;
e788e7d3 58 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
59 PUSHMARK(SP);
60 EXTEND(SP,2);
33c27489 61 PUSHs(SvTIED_obj((SV*)av, mg));
a60c0954 62 PUSHs(sv_2mortal(newSViv(key+1)));
93965878 63 PUTBACK;
864dbfa3 64 call_method("EXTEND", G_SCALAR|G_DISCARD);
d3acc0f7 65 POPSTACK;
93965878
NIS
66 FREETMPS;
67 LEAVE;
68 return;
69 }
a0d0e21e
LW
70 if (key > AvMAX(av)) {
71 SV** ary;
72 I32 tmp;
73 I32 newmax;
74
75 if (AvALLOC(av) != AvARRAY(av)) {
93965878 76 ary = AvALLOC(av) + AvFILLp(av) + 1;
a0d0e21e 77 tmp = AvARRAY(av) - AvALLOC(av);
93965878 78 Move(AvARRAY(av), AvALLOC(av), AvFILLp(av)+1, SV*);
a0d0e21e
LW
79 AvMAX(av) += tmp;
80 SvPVX(av) = (char*)AvALLOC(av);
81 if (AvREAL(av)) {
82 while (tmp)
3280af22 83 ary[--tmp] = &PL_sv_undef;
a0d0e21e
LW
84 }
85
86 if (key > AvMAX(av) - 10) {
87 newmax = key + AvMAX(av);
88 goto resize;
89 }
90 }
91 else {
92 if (AvALLOC(av)) {
c07a80fd 93#ifndef STRANGE_MALLOC
4633a7c4 94 U32 bytes;
c07a80fd 95#endif
4633a7c4 96
1fe09876 97#if defined(MYMALLOC) && !defined(PURIFY) && !defined(LEAKTEST)
8d6dde3e
IZ
98 newmax = malloced_size((void*)AvALLOC(av))/sizeof(SV*) - 1;
99
100 if (key <= newmax)
101 goto resized;
102#endif
a0d0e21e
LW
103 newmax = key + AvMAX(av) / 5;
104 resize:
8d6dde3e 105#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
a0d0e21e 106 Renew(AvALLOC(av),newmax+1, SV*);
4633a7c4
LW
107#else
108 bytes = (newmax + 1) * sizeof(SV*);
109#define MALLOC_OVERHEAD 16
110 tmp = MALLOC_OVERHEAD;
111 while (tmp - MALLOC_OVERHEAD < bytes)
112 tmp += tmp;
113 tmp -= MALLOC_OVERHEAD;
114 tmp /= sizeof(SV*);
115 assert(tmp > newmax);
116 newmax = tmp - 1;
117 New(2,ary, newmax+1, SV*);
118 Copy(AvALLOC(av), ary, AvMAX(av)+1, SV*);
fba3b22e
MB
119 if (AvMAX(av) > 64)
120 offer_nice_chunk(AvALLOC(av), (AvMAX(av)+1) * sizeof(SV*));
4633a7c4
LW
121 else
122 Safefree(AvALLOC(av));
123 AvALLOC(av) = ary;
124#endif
8d6dde3e 125 resized:
a0d0e21e
LW
126 ary = AvALLOC(av) + AvMAX(av) + 1;
127 tmp = newmax - AvMAX(av);
3280af22
NIS
128 if (av == PL_curstack) { /* Oops, grew stack (via av_store()?) */
129 PL_stack_sp = AvALLOC(av) + (PL_stack_sp - PL_stack_base);
130 PL_stack_base = AvALLOC(av);
131 PL_stack_max = PL_stack_base + newmax;
a0d0e21e
LW
132 }
133 }
134 else {
8d6dde3e 135 newmax = key < 3 ? 3 : key;
a0d0e21e
LW
136 New(2,AvALLOC(av), newmax+1, SV*);
137 ary = AvALLOC(av) + 1;
138 tmp = newmax;
3280af22 139 AvALLOC(av)[0] = &PL_sv_undef; /* For the stacks */
a0d0e21e
LW
140 }
141 if (AvREAL(av)) {
142 while (tmp)
3280af22 143 ary[--tmp] = &PL_sv_undef;
a0d0e21e
LW
144 }
145
146 SvPVX(av) = (char*)AvALLOC(av);
147 AvMAX(av) = newmax;
148 }
149 }
150}
151
79072805 152SV**
864dbfa3 153Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
79072805
LW
154{
155 SV *sv;
156
a0d0e21e
LW
157 if (!av)
158 return 0;
159
93965878
NIS
160 if (key < 0) {
161 key += AvFILL(av) + 1;
162 if (key < 0)
163 return 0;
164 }
165
8990e307 166 if (SvRMAGICAL(av)) {
6cef1e77 167 if (mg_find((SV*)av,'P') || mg_find((SV*)av,'D')) {
11343788 168 dTHR;
8990e307 169 sv = sv_newmortal();
463ee0b2 170 mg_copy((SV*)av, sv, 0, key);
3280af22
NIS
171 PL_av_fetch_sv = sv;
172 return &PL_av_fetch_sv;
463ee0b2
LW
173 }
174 }
175
93965878 176 if (key > AvFILLp(av)) {
a0d0e21e
LW
177 if (!lval)
178 return 0;
352edd90 179 sv = NEWSV(5,0);
a0d0e21e 180 return av_store(av,key,sv);
79072805 181 }
3280af22 182 if (AvARRAY(av)[key] == &PL_sv_undef) {
4dbf4341 183 emptyness:
79072805
LW
184 if (lval) {
185 sv = NEWSV(6,0);
463ee0b2 186 return av_store(av,key,sv);
79072805
LW
187 }
188 return 0;
189 }
4dbf4341
PP
190 else if (AvREIFY(av)
191 && (!AvARRAY(av)[key] /* eg. @_ could have freed elts */
192 || SvTYPE(AvARRAY(av)[key]) == SVTYPEMASK)) {
3280af22 193 AvARRAY(av)[key] = &PL_sv_undef; /* 1/2 reify */
4dbf4341
PP
194 goto emptyness;
195 }
463ee0b2 196 return &AvARRAY(av)[key];
79072805
LW
197}
198
199SV**
864dbfa3 200Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
79072805 201{
79072805 202 SV** ary;
93965878
NIS
203 U32 fill;
204
79072805 205
a0d0e21e
LW
206 if (!av)
207 return 0;
43fcc5d2 208 if (!val)
3280af22 209 val = &PL_sv_undef;
463ee0b2 210
a0d0e21e
LW
211 if (key < 0) {
212 key += AvFILL(av) + 1;
213 if (key < 0)
214 return 0;
79072805 215 }
93965878 216
43fcc5d2 217 if (SvREADONLY(av) && key >= AvFILL(av))
22c35a8c 218 croak(PL_no_modify);
93965878
NIS
219
220 if (SvRMAGICAL(av)) {
221 if (mg_find((SV*)av,'P')) {
3280af22 222 if (val != &PL_sv_undef) {
93965878
NIS
223 mg_copy((SV*)av, val, 0, key);
224 }
225 return 0;
226 }
227 }
228
49beac48 229 if (!AvREAL(av) && AvREIFY(av))
a0d0e21e 230 av_reify(av);
a0d0e21e
LW
231 if (key > AvMAX(av))
232 av_extend(av,key);
463ee0b2 233 ary = AvARRAY(av);
93965878 234 if (AvFILLp(av) < key) {
a0d0e21e 235 if (!AvREAL(av)) {
11343788 236 dTHR;
3280af22
NIS
237 if (av == PL_curstack && key > PL_stack_sp - PL_stack_base)
238 PL_stack_sp = PL_stack_base + key; /* XPUSH in disguise */
a0d0e21e 239 do
3280af22 240 ary[++AvFILLp(av)] = &PL_sv_undef;
93965878 241 while (AvFILLp(av) < key);
79072805 242 }
93965878 243 AvFILLp(av) = key;
79072805 244 }
a0d0e21e
LW
245 else if (AvREAL(av))
246 SvREFCNT_dec(ary[key]);
79072805 247 ary[key] = val;
8990e307 248 if (SvSMAGICAL(av)) {
3280af22 249 if (val != &PL_sv_undef) {
a0d0e21e
LW
250 MAGIC* mg = SvMAGIC(av);
251 sv_magic(val, (SV*)av, toLOWER(mg->mg_type), 0, key);
252 }
463ee0b2
LW
253 mg_set((SV*)av);
254 }
79072805
LW
255 return &ary[key];
256}
257
258AV *
864dbfa3 259Perl_newAV(pTHX)
79072805 260{
463ee0b2 261 register AV *av;
79072805 262
a0d0e21e
LW
263 av = (AV*)NEWSV(3,0);
264 sv_upgrade((SV *)av, SVt_PVAV);
463ee0b2
LW
265 AvREAL_on(av);
266 AvALLOC(av) = 0;
267 SvPVX(av) = 0;
93965878 268 AvMAX(av) = AvFILLp(av) = -1;
463ee0b2 269 return av;
79072805
LW
270}
271
272AV *
864dbfa3 273Perl_av_make(pTHX_ register I32 size, register SV **strp)
79072805 274{
463ee0b2 275 register AV *av;
79072805
LW
276 register I32 i;
277 register SV** ary;
278
a0d0e21e
LW
279 av = (AV*)NEWSV(8,0);
280 sv_upgrade((SV *) av,SVt_PVAV);
a0d0e21e 281 AvFLAGS(av) = AVf_REAL;
573fa4ea
TB
282 if (size) { /* `defined' was returning undef for size==0 anyway. */
283 New(4,ary,size,SV*);
284 AvALLOC(av) = ary;
285 SvPVX(av) = (char*)ary;
93965878 286 AvFILLp(av) = size - 1;
573fa4ea
TB
287 AvMAX(av) = size - 1;
288 for (i = 0; i < size; i++) {
289 assert (*strp);
290 ary[i] = NEWSV(7,0);
291 sv_setsv(ary[i], *strp);
292 strp++;
293 }
79072805 294 }
463ee0b2 295 return av;
79072805
LW
296}
297
298AV *
864dbfa3 299Perl_av_fake(pTHX_ register I32 size, register SV **strp)
79072805 300{
463ee0b2 301 register AV *av;
79072805
LW
302 register SV** ary;
303
a0d0e21e
LW
304 av = (AV*)NEWSV(9,0);
305 sv_upgrade((SV *)av, SVt_PVAV);
79072805 306 New(4,ary,size+1,SV*);
463ee0b2 307 AvALLOC(av) = ary;
79072805 308 Copy(strp,ary,size,SV*);
a0d0e21e 309 AvFLAGS(av) = AVf_REIFY;
463ee0b2 310 SvPVX(av) = (char*)ary;
93965878 311 AvFILLp(av) = size - 1;
463ee0b2 312 AvMAX(av) = size - 1;
79072805 313 while (size--) {
a0d0e21e
LW
314 assert (*strp);
315 SvTEMP_off(*strp);
79072805
LW
316 strp++;
317 }
463ee0b2 318 return av;
79072805
LW
319}
320
321void
864dbfa3 322Perl_av_clear(pTHX_ register AV *av)
79072805
LW
323{
324 register I32 key;
a0d0e21e 325 SV** ary;
79072805 326
7d55f622
PP
327#ifdef DEBUGGING
328 if (SvREFCNT(av) <= 0) {
329 warn("Attempt to clear deleted array");
330 }
331#endif
a60c0954 332 if (!av)
79072805
LW
333 return;
334 /*SUPPRESS 560*/
a0d0e21e 335
39caa665 336 if (SvREADONLY(av))
22c35a8c 337 croak(PL_no_modify);
39caa665 338
93965878
NIS
339 /* Give any tie a chance to cleanup first */
340 if (SvRMAGICAL(av))
341 mg_clear((SV*)av);
342
a60c0954
NIS
343 if (AvMAX(av) < 0)
344 return;
345
a0d0e21e
LW
346 if (AvREAL(av)) {
347 ary = AvARRAY(av);
93965878 348 key = AvFILLp(av) + 1;
a0d0e21e
LW
349 while (key) {
350 SvREFCNT_dec(ary[--key]);
3280af22 351 ary[key] = &PL_sv_undef;
a0d0e21e
LW
352 }
353 }
463ee0b2
LW
354 if (key = AvARRAY(av) - AvALLOC(av)) {
355 AvMAX(av) += key;
a0d0e21e 356 SvPVX(av) = (char*)AvALLOC(av);
79072805 357 }
93965878 358 AvFILLp(av) = -1;
fb73857a 359
79072805
LW
360}
361
362void
864dbfa3 363Perl_av_undef(pTHX_ register AV *av)
79072805
LW
364{
365 register I32 key;
366
463ee0b2 367 if (!av)
79072805
LW
368 return;
369 /*SUPPRESS 560*/
93965878
NIS
370
371 /* Give any tie a chance to cleanup first */
33c27489 372 if (SvTIED_mg((SV*)av, 'P'))
93965878
NIS
373 av_fill(av, -1); /* mg_clear() ? */
374
a0d0e21e 375 if (AvREAL(av)) {
93965878 376 key = AvFILLp(av) + 1;
a0d0e21e
LW
377 while (key)
378 SvREFCNT_dec(AvARRAY(av)[--key]);
379 }
463ee0b2
LW
380 Safefree(AvALLOC(av));
381 AvALLOC(av) = 0;
382 SvPVX(av) = 0;
93965878 383 AvMAX(av) = AvFILLp(av) = -1;
748a9306
LW
384 if (AvARYLEN(av)) {
385 SvREFCNT_dec(AvARYLEN(av));
386 AvARYLEN(av) = 0;
387 }
79072805
LW
388}
389
a0d0e21e 390void
864dbfa3 391Perl_av_push(pTHX_ register AV *av, SV *val)
93965878
NIS
392{
393 MAGIC *mg;
a0d0e21e
LW
394 if (!av)
395 return;
93965878 396 if (SvREADONLY(av))
22c35a8c 397 croak(PL_no_modify);
93965878 398
33c27489 399 if (mg = SvTIED_mg((SV*)av, 'P')) {
93965878 400 dSP;
e788e7d3 401 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
402 PUSHMARK(SP);
403 EXTEND(SP,2);
33c27489 404 PUSHs(SvTIED_obj((SV*)av, mg));
93965878 405 PUSHs(val);
a60c0954
NIS
406 PUTBACK;
407 ENTER;
864dbfa3 408 call_method("PUSH", G_SCALAR|G_DISCARD);
a60c0954 409 LEAVE;
d3acc0f7 410 POPSTACK;
93965878
NIS
411 return;
412 }
413 av_store(av,AvFILLp(av)+1,val);
79072805
LW
414}
415
416SV *
864dbfa3 417Perl_av_pop(pTHX_ register AV *av)
79072805
LW
418{
419 SV *retval;
93965878 420 MAGIC* mg;
79072805 421
a0d0e21e 422 if (!av || AvFILL(av) < 0)
3280af22 423 return &PL_sv_undef;
43fcc5d2 424 if (SvREADONLY(av))
22c35a8c 425 croak(PL_no_modify);
33c27489 426 if (mg = SvTIED_mg((SV*)av, 'P')) {
93965878 427 dSP;
e788e7d3 428 PUSHSTACKi(PERLSI_MAGIC);
924508f0 429 PUSHMARK(SP);
33c27489 430 XPUSHs(SvTIED_obj((SV*)av, mg));
a60c0954
NIS
431 PUTBACK;
432 ENTER;
864dbfa3 433 if (call_method("POP", G_SCALAR)) {
3280af22 434 retval = newSVsv(*PL_stack_sp--);
93965878 435 } else {
3280af22 436 retval = &PL_sv_undef;
93965878 437 }
a60c0954 438 LEAVE;
d3acc0f7 439 POPSTACK;
93965878
NIS
440 return retval;
441 }
442 retval = AvARRAY(av)[AvFILLp(av)];
3280af22 443 AvARRAY(av)[AvFILLp(av)--] = &PL_sv_undef;
8990e307 444 if (SvSMAGICAL(av))
463ee0b2 445 mg_set((SV*)av);
79072805
LW
446 return retval;
447}
448
449void
864dbfa3 450Perl_av_unshift(pTHX_ register AV *av, register I32 num)
79072805
LW
451{
452 register I32 i;
67a38de0 453 register SV **ary;
93965878 454 MAGIC* mg;
79072805 455
a0d0e21e 456 if (!av || num <= 0)
79072805 457 return;
43fcc5d2 458 if (SvREADONLY(av))
22c35a8c 459 croak(PL_no_modify);
93965878 460
33c27489 461 if (mg = SvTIED_mg((SV*)av, 'P')) {
93965878 462 dSP;
e788e7d3 463 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
464 PUSHMARK(SP);
465 EXTEND(SP,1+num);
33c27489 466 PUSHs(SvTIED_obj((SV*)av, mg));
93965878 467 while (num-- > 0) {
3280af22 468 PUSHs(&PL_sv_undef);
93965878
NIS
469 }
470 PUTBACK;
a60c0954 471 ENTER;
864dbfa3 472 call_method("UNSHIFT", G_SCALAR|G_DISCARD);
a60c0954 473 LEAVE;
d3acc0f7 474 POPSTACK;
93965878
NIS
475 return;
476 }
477
49beac48
CS
478 if (!AvREAL(av) && AvREIFY(av))
479 av_reify(av);
a0d0e21e
LW
480 i = AvARRAY(av) - AvALLOC(av);
481 if (i) {
482 if (i > num)
483 i = num;
484 num -= i;
485
486 AvMAX(av) += i;
93965878 487 AvFILLp(av) += i;
a0d0e21e
LW
488 SvPVX(av) = (char*)(AvARRAY(av) - i);
489 }
d2719217 490 if (num) {
67a38de0
NIS
491 i = AvFILLp(av);
492 av_extend(av, i + num);
93965878 493 AvFILLp(av) += num;
67a38de0
NIS
494 ary = AvARRAY(av);
495 Move(ary, ary + num, i + 1, SV*);
496 do {
3280af22 497 ary[--num] = &PL_sv_undef;
67a38de0 498 } while (num);
79072805
LW
499 }
500}
501
502SV *
864dbfa3 503Perl_av_shift(pTHX_ register AV *av)
79072805
LW
504{
505 SV *retval;
93965878 506 MAGIC* mg;
79072805 507
a0d0e21e 508 if (!av || AvFILL(av) < 0)
3280af22 509 return &PL_sv_undef;
43fcc5d2 510 if (SvREADONLY(av))
22c35a8c 511 croak(PL_no_modify);
33c27489 512 if (mg = SvTIED_mg((SV*)av, 'P')) {
93965878 513 dSP;
e788e7d3 514 PUSHSTACKi(PERLSI_MAGIC);
924508f0 515 PUSHMARK(SP);
33c27489 516 XPUSHs(SvTIED_obj((SV*)av, mg));
a60c0954
NIS
517 PUTBACK;
518 ENTER;
864dbfa3 519 if (call_method("SHIFT", G_SCALAR)) {
3280af22 520 retval = newSVsv(*PL_stack_sp--);
93965878 521 } else {
3280af22 522 retval = &PL_sv_undef;
a60c0954
NIS
523 }
524 LEAVE;
d3acc0f7 525 POPSTACK;
93965878
NIS
526 return retval;
527 }
463ee0b2 528 retval = *AvARRAY(av);
a0d0e21e 529 if (AvREAL(av))
3280af22 530 *AvARRAY(av) = &PL_sv_undef;
463ee0b2
LW
531 SvPVX(av) = (char*)(AvARRAY(av) + 1);
532 AvMAX(av)--;
93965878 533 AvFILLp(av)--;
8990e307 534 if (SvSMAGICAL(av))
463ee0b2 535 mg_set((SV*)av);
79072805
LW
536 return retval;
537}
538
539I32
864dbfa3 540Perl_av_len(pTHX_ register AV *av)
79072805 541{
463ee0b2 542 return AvFILL(av);
79072805
LW
543}
544
545void
864dbfa3 546Perl_av_fill(pTHX_ register AV *av, I32 fill)
79072805 547{
93965878 548 MAGIC *mg;
a0d0e21e
LW
549 if (!av)
550 croak("panic: null array");
79072805
LW
551 if (fill < 0)
552 fill = -1;
33c27489 553 if (mg = SvTIED_mg((SV*)av, 'P')) {
93965878
NIS
554 dSP;
555 ENTER;
556 SAVETMPS;
e788e7d3 557 PUSHSTACKi(PERLSI_MAGIC);
924508f0
GS
558 PUSHMARK(SP);
559 EXTEND(SP,2);
33c27489 560 PUSHs(SvTIED_obj((SV*)av, mg));
a60c0954 561 PUSHs(sv_2mortal(newSViv(fill+1)));
93965878 562 PUTBACK;
864dbfa3 563 call_method("STORESIZE", G_SCALAR|G_DISCARD);
d3acc0f7 564 POPSTACK;
93965878
NIS
565 FREETMPS;
566 LEAVE;
567 return;
568 }
463ee0b2 569 if (fill <= AvMAX(av)) {
93965878 570 I32 key = AvFILLp(av);
a0d0e21e
LW
571 SV** ary = AvARRAY(av);
572
573 if (AvREAL(av)) {
574 while (key > fill) {
575 SvREFCNT_dec(ary[key]);
3280af22 576 ary[key--] = &PL_sv_undef;
a0d0e21e
LW
577 }
578 }
579 else {
580 while (key < fill)
3280af22 581 ary[++key] = &PL_sv_undef;
a0d0e21e
LW
582 }
583
93965878 584 AvFILLp(av) = fill;
8990e307 585 if (SvSMAGICAL(av))
463ee0b2
LW
586 mg_set((SV*)av);
587 }
a0d0e21e 588 else
3280af22 589 (void)av_store(av,fill,&PL_sv_undef);
79072805 590}
c750a3ec 591
57079c46
GA
592
593/* AVHV: Support for treating arrays as if they were hashes. The
594 * first element of the array should be a hash reference that maps
595 * hash keys to array indices.
596 */
597
72311751 598STATIC I32
864dbfa3 599avhv_index_sv(pTHX_ SV* sv)
57079c46
GA
600{
601 I32 index = SvIV(sv);
602 if (index < 1)
603 croak("Bad index while coercing array into hash");
604 return index;
605}
606
5d5aaa5e 607HV*
864dbfa3 608Perl_avhv_keys(pTHX_ AV *av)
5d5aaa5e 609{
57079c46 610 SV **keysp = av_fetch(av, 0, FALSE);
5d5aaa5e 611 if (keysp) {
d627ae4e
MB
612 SV *sv = *keysp;
613 if (SvGMAGICAL(sv))
614 mg_get(sv);
615 if (SvROK(sv)) {
616 sv = SvRV(sv);
617 if (SvTYPE(sv) == SVt_PVHV)
57079c46 618 return (HV*)sv;
5d5aaa5e
JP
619 }
620 }
57079c46 621 croak("Can't coerce array into hash");
72311751 622 return Nullhv;
c750a3ec
MB
623}
624
625SV**
864dbfa3 626Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash)
97fcbf96 627{
5d5aaa5e
JP
628 SV **indsvp;
629 HV *keys = avhv_keys(av);
97fcbf96 630 HE *he;
5bc6513d 631
5d5aaa5e 632 he = hv_fetch_ent(keys, keysv, FALSE, hash);
57079c46
GA
633 if (!he)
634 croak("No such array field");
635 return av_fetch(av, avhv_index_sv(HeVAL(he)), lval);
5bc6513d
MB
636}
637
c750a3ec 638bool
864dbfa3 639Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash)
97fcbf96 640{
5d5aaa5e
JP
641 HV *keys = avhv_keys(av);
642 return hv_exists_ent(keys, keysv, hash);
97fcbf96
MB
643}
644
c750a3ec 645HE *
864dbfa3 646Perl_avhv_iternext(pTHX_ AV *av)
c750a3ec 647{
5d5aaa5e
JP
648 HV *keys = avhv_keys(av);
649 return hv_iternext(keys);
c750a3ec
MB
650}
651
652SV *
864dbfa3 653Perl_avhv_iterval(pTHX_ AV *av, register HE *entry)
c750a3ec 654{
57079c46
GA
655 SV *sv = hv_iterval(avhv_keys(av), entry);
656 return *av_fetch(av, avhv_index_sv(sv), TRUE);
c750a3ec 657}