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.
10 #include "multicall.h"
13 # include <patchlevel.h>
14 # if !(defined(PERL_VERSION) || (SUBVERSION > 0 && defined(PATCHLEVEL)))
15 # include <could_not_find_Perl_patchlevel.h>
17 # define PERL_REVISION 5
18 # define PERL_VERSION PATCHLEVEL
19 # define PERL_SUBVERSION SUBVERSION
27 /* Some platforms have strict exports. And before 5.7.3 cxinc (or Perl_cxinc)
28 was not exported. Therefore platforms like win32, VMS etc have problems
29 so we redefine it here -- GMB
33 # define SvUOK(sv) SvIOK_UV(sv)
37 # define cxinc() my_cxinc(aTHX)
41 cxstack_max = cxstack_max * 3 / 2;
42 Renew(cxstack, cxstack_max + 1, struct context); /* XXX should fix CXINC macro */
43 return cxstack_ix + 1;
52 # define slu_sv_value(sv) (SvIOK(sv)) ? (SvIOK_UV(sv)) ? (NV)(SvUVX(sv)) : (NV)(SvIVX(sv)) : (SvNV(sv))
54 # define slu_sv_value(sv) (SvIOK(sv)) ? (NV)(SvIVX(sv)) : (SvNV(sv))
58 # define Drand01() ((rand() & 0x7FFF) / (double) ((unsigned long)1 << 15))
63 # define gv_stashpvn(n,l,c) gv_stashpv(n,c)
70 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
71 MAGIC *mg = mg_find(sv, 't');
72 if (mg && ((mg->mg_len & 1) || (mg->mg_len & 2) && mg->mg_obj == sv))
78 # define SvTAINTED_on(sv) sv_magic((sv), Nullsv, 't', Nullch, 0)
79 # define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
81 # define PL_defgv defgv
83 # define PL_curpad curpad
84 # define CALLRUNOPS runops
85 # define PL_curpm curpm
86 # define PL_sv_undef sv_undef
87 # define PERL_CONTEXT struct context
89 #if (PERL_VERSION < 5) || (PERL_VERSION == 5 && PERL_SUBVERSION <50)
91 # define PL_tainting tainting
93 # ifndef PL_stack_base
94 # define PL_stack_base stack_base
97 # define PL_stack_sp stack_sp
100 # define PL_ppaddr ppaddr
105 # define PTR2UV(ptr) (UV)(ptr)
109 # define SvUV_set(sv, val) (((XPVUV*)SvANY(sv))->xuv_uv = (val))
112 #ifndef PERL_UNUSED_DECL
114 # if (defined(__GNUC__) && defined(__cplusplus)) || defined(__INTEL_COMPILER)
115 # define PERL_UNUSED_DECL
117 # define PERL_UNUSED_DECL __attribute__((unused))
120 # define PERL_UNUSED_DECL
125 #define dNOOP extern int Perl___notused PERL_UNUSED_DECL
136 MODULE=List::Util PACKAGE=List::Util
153 retval = slu_sv_value(retsv);
154 for(index = 1 ; index < items ; index++) {
155 SV *stacksv = ST(index);
156 NV val = slu_sv_value(stacksv);
157 if(val < retval ? !ix : ix) {
179 RETVAL = slu_sv_value(sv);
180 for(index = 1 ; index < items ; index++) {
182 RETVAL += slu_sv_value(sv);
203 sv_cmp & sv_cmp_locale return 1,0,-1 for gt,eq,lt
204 so we set ix to the value we are looking for
205 xsubpp does not allow -ve values, so we start with 0,2 and subtract 1
210 if(MAXARG & OPpLOCALE) {
211 for(index = 1 ; index < items ; index++) {
212 SV *right = ST(index);
213 if(sv_cmp_locale(left, right) == ix)
219 for(index = 1 ; index < items ; index++) {
220 SV *right = ST(index);
221 if(sv_cmp(left, right) == ix)
240 SV *ret = sv_newmortal();
244 I32 gimme = G_SCALAR;
245 SV **args = &PL_stack_base[ax];
250 cv = sv_2cv(block, &stash, &gv, 0);
252 agv = gv_fetchpv("a", TRUE, SVt_PV);
253 bgv = gv_fetchpv("b", TRUE, SVt_PV);
257 SvSetSV(ret, args[1]);
258 for(index = 2 ; index < items ; index++) {
259 GvSV(bgv) = args[index];
261 SvSetSV(ret, *PL_stack_sp);
278 I32 gimme = G_SCALAR;
279 SV **args = &PL_stack_base[ax];
284 cv = sv_2cv(block, &stash, &gv, 0);
286 SAVESPTR(GvSV(PL_defgv));
288 for(index = 1 ; index < items ; index++) {
289 GvSV(PL_defgv) = args[index];
291 if (SvTRUE(*PL_stack_sp)) {
309 struct op *old_op = PL_op;
311 /* We call pp_rand here so that Drand01 get initialized if rand()
312 or srand() has not already been called
314 memzero((char*)(&dmy_op), sizeof(struct op));
315 /* we let pp_rand() borrow the TARG allocated for this XS sub */
316 dmy_op.op_targ = PL_op->op_targ;
318 (void)*(PL_ppaddr[OP_RAND])(aTHX);
320 for (index = items ; index > 1 ; ) {
321 int swap = (int)(Drand01() * (double)(index--));
323 ST(swap) = ST(index);
330 MODULE=List::Util PACKAGE=Scalar::Util
340 char *ptr = SvPV(str,len);
341 ST(0) = sv_newmortal();
342 (void)SvUPGRADE(ST(0),SVt_PVNV);
343 sv_setpvn(ST(0),ptr,len);
344 if(SvNOK(num) || SvPOK(num) || SvMAGICAL(num)) {
345 SvNV_set(ST(0), SvNV(num));
349 else if (SvUOK(num)) {
350 SvUV_set(ST(0), SvUV(num));
356 SvIV_set(ST(0), SvIV(num));
359 if(PL_tainting && (SvTAINTED(num) || SvTAINTED(str)))
372 if(!sv_isobject(sv)) {
375 RETVAL = sv_reftype(SvRV(sv),TRUE);
391 RETVAL = sv_reftype(SvRV(sv),FALSE);
407 RETVAL = PTR2UV(SvRV(sv));
420 croak("weak references are not implemented in this release of perl");
429 ST(0) = boolSV(SvROK(sv) && SvWEAKREF(sv));
432 croak("weak references are not implemented in this release of perl");
440 RETVAL = SvREADONLY(sv);
449 RETVAL = SvTAINTED(sv);
459 ST(0) = boolSV(SvVOK(sv));
462 croak("vstrings are not implemented in this release of perl");
466 looks_like_number(sv)
470 RETVAL = looks_like_number(sv);
475 set_prototype(subref, proto)
482 SV *sv = SvRV(subref);
483 if (SvTYPE(sv) != SVt_PVCV) {
484 /* not a subroutine reference */
485 croak("set_prototype: not a subroutine reference");
488 /* set the prototype */
490 char *ptr = SvPV(proto, len);
491 sv_setpvn(sv, ptr, len);
494 /* delete the prototype */
499 croak("set_prototype: not a reference");
506 HV *lu_stash = gv_stashpvn("List::Util", 10, TRUE);
507 GV *rmcgv = *(GV**)hv_fetch(lu_stash, "REAL_MULTICALL", 14, TRUE);
509 #if !defined(SvWEAKREF) || !defined(SvVOK)
510 HV *su_stash = gv_stashpvn("Scalar::Util", 12, TRUE);
511 GV *vargv = *(GV**)hv_fetch(su_stash, "EXPORT_FAIL", 11, TRUE);
513 if (SvTYPE(vargv) != SVt_PVGV)
514 gv_init(vargv, su_stash, "Scalar::Util", 12, TRUE);
515 varav = GvAVn(vargv);
517 if (SvTYPE(rmcgv) != SVt_PVGV)
518 gv_init(rmcgv, lu_stash, "List::Util", 12, TRUE);
519 rmcsv = GvSVn(rmcgv);
521 av_push(varav, newSVpv("weaken",6));
522 av_push(varav, newSVpv("isweak",6));
525 av_push(varav, newSVpv("isvstring",9));
527 #ifdef REAL_MULTICALL
528 sv_setsv(rmcsv, &PL_sv_yes);
530 sv_setsv(rmcsv, &PL_sv_no);