1 /* Copyright (c) 1997-2000 Graham Barr <gbarr@pobox.com>. All rights reserved.
2 * This program is free software; you can redistribute it and/or
3 * modify it under the same terms as Perl itself.
5 #define PERL_NO_GET_CONTEXT /* we want efficiency */
11 # include <patchlevel.h>
12 # if !(defined(PERL_VERSION) || (SUBVERSION > 0 && defined(PATCHLEVEL)))
13 # include <could_not_find_Perl_patchlevel.h>
15 # define PERL_REVISION 5
16 # define PERL_VERSION PATCHLEVEL
17 # define PERL_SUBVERSION SUBVERSION
21 # include "multicall.h"
28 /* Some platforms have strict exports. And before 5.7.3 cxinc (or Perl_cxinc)
29 was not exported. Therefore platforms like win32, VMS etc have problems
30 so we redefine it here -- GMB
34 # define SvUOK(sv) SvIOK_UV(sv)
38 # define cxinc() my_cxinc(aTHX)
42 cxstack_max = cxstack_max * 3 / 2;
43 Renew(cxstack, cxstack_max + 1, struct context); /* XXX should fix CXINC macro */
44 return cxstack_ix + 1;
53 # define slu_sv_value(sv) (SvIOK(sv)) ? (SvIOK_UV(sv)) ? (NV)(SvUVX(sv)) : (NV)(SvIVX(sv)) : (SvNV(sv))
55 # define slu_sv_value(sv) (SvIOK(sv)) ? (NV)(SvIVX(sv)) : (SvNV(sv))
59 # define Drand01() ((rand() & 0x7FFF) / (double) ((unsigned long)1 << 15))
64 # define gv_stashpvn(n,l,c) gv_stashpv(n,c)
69 sv_tainted(pTHX_ SV *sv)
71 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
72 MAGIC *mg = mg_find(sv, 't');
73 if (mg && ((mg->mg_len & 1) || (mg->mg_len & 2) && mg->mg_obj == sv))
79 # define SvTAINTED_on(sv) sv_magic((sv), Nullsv, 't', Nullch, 0)
80 # define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(aTHX_ sv))
82 # define PL_defgv defgv
84 # define PL_curpad curpad
85 # define CALLRUNOPS runops
86 # define PL_curpm curpm
87 # define PL_sv_undef sv_undef
88 # define PERL_CONTEXT struct context
90 #if (PERL_VERSION < 5) || (PERL_VERSION == 5 && PERL_SUBVERSION <50)
92 # define PL_tainting tainting
94 # ifndef PL_stack_base
95 # define PL_stack_base stack_base
98 # define PL_stack_sp stack_sp
101 # define PL_ppaddr ppaddr
106 # define PTR2UV(ptr) (UV)(ptr)
110 # define SvUV_set(sv, val) (((XPVUV*)SvANY(sv))->xuv_uv = (val))
113 #ifndef PERL_UNUSED_DECL
115 # if (defined(__GNUC__) && defined(__cplusplus)) || defined(__INTEL_COMPILER)
116 # define PERL_UNUSED_DECL
118 # define PERL_UNUSED_DECL __attribute__((unused))
121 # define PERL_UNUSED_DECL
126 #define dNOOP extern int Perl___notused PERL_UNUSED_DECL
133 MODULE=List::Util PACKAGE=List::Util
151 magic = SvAMAGIC(retsv);
153 retval = slu_sv_value(retsv);
155 for(index = 1 ; index < items ; index++) {
156 SV *stacksv = ST(index);
158 if ((magic || SvAMAGIC(stacksv)) && (tmpsv = amagic_call(retsv, stacksv, gt_amg, 0))) {
159 if (SvTRUE(tmpsv) ? !ix : ix) {
161 magic = SvAMAGIC(retsv);
163 retval = slu_sv_value(retsv);
168 NV val = slu_sv_value(stacksv);
170 retval = slu_sv_value(retsv);
173 if(val < retval ? !ix : ix) {
199 retsv = sv_newmortal();
203 retval = slu_sv_value(sv);
205 for(index = 1 ; index < items ; index++) {
207 if (retsv || SvAMAGIC(sv)) {
209 retsv = sv_newmortal();
210 sv_setnv(retsv,retval);
212 if (!amagic_call(retsv, sv, add_amg, AMGf_assign)) {
213 sv_setnv(retsv, SvNV(retsv) + SvNV(sv));
217 retval += slu_sv_value(sv);
221 retsv = sv_newmortal();
222 sv_setnv(retsv,retval);
243 sv_cmp & sv_cmp_locale return 1,0,-1 for gt,eq,lt
244 so we set ix to the value we are looking for
245 xsubpp does not allow -ve values, so we start with 0,2 and subtract 1
250 if(MAXARG & OPpLOCALE) {
251 for(index = 1 ; index < items ; index++) {
252 SV *right = ST(index);
253 if(sv_cmp_locale(left, right) == ix)
259 for(index = 1 ; index < items ; index++) {
260 SV *right = ST(index);
261 if(sv_cmp(left, right) == ix)
282 SV *ret = sv_newmortal();
286 I32 gimme = G_SCALAR;
287 SV **args = &PL_stack_base[ax];
293 cv = sv_2cv(block, &stash, &gv, 0);
295 croak("Not a subroutine reference");
298 agv = gv_fetchpv("a", TRUE, SVt_PV);
299 bgv = gv_fetchpv("b", TRUE, SVt_PV);
303 SvSetSV(ret, args[1]);
304 for(index = 2 ; index < items ; index++) {
305 GvSV(bgv) = args[index];
307 SvSetSV(ret, *PL_stack_sp);
324 I32 gimme = G_SCALAR;
325 SV **args = &PL_stack_base[ax];
331 cv = sv_2cv(block, &stash, &gv, 0);
333 croak("Not a subroutine reference");
336 SAVESPTR(GvSV(PL_defgv));
338 for(index = 1 ; index < items ; index++) {
339 GvSV(PL_defgv) = args[index];
341 if (SvTRUE(*PL_stack_sp)) {
359 #if (PERL_VERSION < 9)
361 struct op *old_op = PL_op;
363 /* We call pp_rand here so that Drand01 get initialized if rand()
364 or srand() has not already been called
366 memzero((char*)(&dmy_op), sizeof(struct op));
367 /* we let pp_rand() borrow the TARG allocated for this XS sub */
368 dmy_op.op_targ = PL_op->op_targ;
370 (void)*(PL_ppaddr[OP_RAND])(aTHX);
373 /* Initialize Drand01 if rand() or srand() has
374 not already been called
376 if (!PL_srand_called) {
377 (void)seedDrand01((Rand_seed_t)Perl_seed(aTHX));
378 PL_srand_called = TRUE;
382 for (index = items ; index > 1 ; ) {
383 int swap = (int)(Drand01() * (double)(index--));
385 ST(swap) = ST(index);
392 MODULE=List::Util PACKAGE=Scalar::Util
402 char *ptr = SvPV(str,len);
403 ST(0) = sv_newmortal();
404 (void)SvUPGRADE(ST(0),SVt_PVNV);
405 sv_setpvn(ST(0),ptr,len);
408 if(SvNOK(num) || SvPOK(num) || SvMAGICAL(num)) {
409 SvNV_set(ST(0), SvNV(num));
413 else if (SvUOK(num)) {
414 SvUV_set(ST(0), SvUV(num));
420 SvIV_set(ST(0), SvIV(num));
423 if(PL_tainting && (SvTAINTED(num) || SvTAINTED(str)))
436 if(!(SvROK(sv) && SvOBJECT(SvRV(sv)))) {
439 RETVAL = (char*)sv_reftype(SvRV(sv),TRUE);
455 RETVAL = (char*)sv_reftype(SvRV(sv),FALSE);
471 RETVAL = PTR2UV(SvRV(sv));
484 croak("weak references are not implemented in this release of perl");
493 ST(0) = boolSV(SvROK(sv) && SvWEAKREF(sv));
496 croak("weak references are not implemented in this release of perl");
504 RETVAL = SvREADONLY(sv);
513 RETVAL = SvTAINTED(sv);
523 ST(0) = boolSV(SvVOK(sv));
526 croak("vstrings are not implemented in this release of perl");
530 looks_like_number(sv)
535 if (SvAMAGIC(sv) && (tempsv = AMG_CALLun(sv, numer))) {
538 else if (SvMAGICAL(sv)) {
541 #if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION <5)
542 if (SvPOK(sv) || SvPOKp(sv)) {
543 RETVAL = looks_like_number(sv);
546 RETVAL = SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
549 RETVAL = looks_like_number(sv);
555 set_prototype(subref, proto)
562 SV *sv = SvRV(subref);
563 if (SvTYPE(sv) != SVt_PVCV) {
564 /* not a subroutine reference */
565 croak("set_prototype: not a subroutine reference");
568 /* set the prototype */
570 char *ptr = SvPV(proto, len);
571 sv_setpvn(sv, ptr, len);
574 /* delete the prototype */
579 croak("set_prototype: not a reference");
586 HV *lu_stash = gv_stashpvn("List::Util", 10, TRUE);
587 GV *rmcgv = *(GV**)hv_fetch(lu_stash, "REAL_MULTICALL", 14, TRUE);
589 #if !defined(SvWEAKREF) || !defined(SvVOK)
590 HV *su_stash = gv_stashpvn("Scalar::Util", 12, TRUE);
591 GV *vargv = *(GV**)hv_fetch(su_stash, "EXPORT_FAIL", 11, TRUE);
593 if (SvTYPE(vargv) != SVt_PVGV)
594 gv_init(vargv, su_stash, "Scalar::Util", 12, TRUE);
595 varav = GvAVn(vargv);
597 if (SvTYPE(rmcgv) != SVt_PVGV)
598 gv_init(rmcgv, lu_stash, "List::Util", 12, TRUE);
599 rmcsv = GvSVn(rmcgv);
601 av_push(varav, newSVpv("weaken",6));
602 av_push(varav, newSVpv("isweak",6));
605 av_push(varav, newSVpv("isvstring",9));
607 #ifdef REAL_MULTICALL
608 sv_setsv(rmcsv, &PL_sv_yes);
610 sv_setsv(rmcsv, &PL_sv_no);