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.
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
25 /* Some platforms have strict exports. And before 5.7.3 cxinc (or Perl_cxinc)
26 was not exported. Therefore platforms like win32, VMS etc have problems
27 so we redefine it here -- GMB
31 # define SvUOK(sv) SvIOK_UV(sv)
35 # define cxinc() my_cxinc(aTHX)
39 cxstack_max = cxstack_max * 3 / 2;
40 Renew(cxstack, cxstack_max + 1, struct context); /* XXX should fix CXINC macro */
41 return cxstack_ix + 1;
50 # define slu_sv_value(sv) (SvIOK(sv)) ? (SvIOK_UV(sv)) ? (NV)(SvUVX(sv)) : (NV)(SvIVX(sv)) : (SvNV(sv))
52 # define slu_sv_value(sv) (SvIOK(sv)) ? (NV)(SvIVX(sv)) : (SvNV(sv))
56 # define Drand01() ((rand() & 0x7FFF) / (double) ((unsigned long)1 << 15))
61 # define gv_stashpvn(n,l,c) gv_stashpv(n,c)
68 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
69 MAGIC *mg = mg_find(sv, 't');
70 if (mg && ((mg->mg_len & 1) || (mg->mg_len & 2) && mg->mg_obj == sv))
76 # define SvTAINTED_on(sv) sv_magic((sv), Nullsv, 't', Nullch, 0)
77 # define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
79 # define PL_defgv defgv
81 # define PL_curpad curpad
82 # define CALLRUNOPS runops
83 # define PL_curpm curpm
84 # define PL_sv_undef sv_undef
85 # define PERL_CONTEXT struct context
87 #if (PERL_VERSION < 5) || (PERL_VERSION == 5 && PERL_SUBVERSION <50)
89 # define PL_tainting tainting
91 # ifndef PL_stack_base
92 # define PL_stack_base stack_base
95 # define PL_stack_sp stack_sp
98 # define PL_ppaddr ppaddr
103 # define PTR2UV(ptr) (UV)(ptr)
107 # define SvUV_set(sv, val) (((XPVUV*)SvANY(sv))->xuv_uv = (val))
110 #ifndef PERL_UNUSED_DECL
112 # if (defined(__GNUC__) && defined(__cplusplus)) || defined(__INTEL_COMPILER)
113 # define PERL_UNUSED_DECL
115 # define PERL_UNUSED_DECL __attribute__((unused))
118 # define PERL_UNUSED_DECL
123 #define dNOOP extern int Perl___notused PERL_UNUSED_DECL
130 MODULE=List::Util PACKAGE=List::Util
147 retval = slu_sv_value(retsv);
148 for(index = 1 ; index < items ; index++) {
149 SV *stacksv = ST(index);
150 NV val = slu_sv_value(stacksv);
151 if(val < retval ? !ix : ix) {
173 RETVAL = slu_sv_value(sv);
174 for(index = 1 ; index < items ; index++) {
176 RETVAL += slu_sv_value(sv);
197 sv_cmp & sv_cmp_locale return 1,0,-1 for gt,eq,lt
198 so we set ix to the value we are looking for
199 xsubpp does not allow -ve values, so we start with 0,2 and subtract 1
204 if(MAXARG & OPpLOCALE) {
205 for(index = 1 ; index < items ; index++) {
206 SV *right = ST(index);
207 if(sv_cmp_locale(left, right) == ix)
213 for(index = 1 ; index < items ; index++) {
214 SV *right = ST(index);
215 if(sv_cmp(left, right) == ix)
234 SV *ret = sv_newmortal();
242 I32 gimme = G_SCALAR;
244 bool oldcatch = CATCH_GET;
249 agv = gv_fetchpv("a", TRUE, SVt_PV);
250 bgv = gv_fetchpv("b", TRUE, SVt_PV);
254 cv = sv_2cv(block, &stash, &gv, 0);
255 reducecop = CvSTART(cv);
256 SAVESPTR(CvROOT(cv)->op_ppaddr);
257 CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL];
259 PAD_SET_CUR(CvPADLIST(cv),1);
262 PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
268 PUSHBLOCK(cx, CXt_SUB, SP);
270 for(index = 2 ; index < items ; index++) {
271 GvSV(bgv) = ST(index);
274 SvSetSV(ret, *PL_stack_sp);
277 POPBLOCK(cx,PL_curpm);
296 I32 gimme = G_SCALAR;
298 bool oldcatch = CATCH_GET;
303 SAVESPTR(GvSV(PL_defgv));
304 cv = sv_2cv(block, &stash, &gv, 0);
305 reducecop = CvSTART(cv);
306 SAVESPTR(CvROOT(cv)->op_ppaddr);
307 CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL];
309 PAD_SET_CUR(CvPADLIST(cv),1);
312 PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
317 PUSHBLOCK(cx, CXt_SUB, SP);
320 for(index = 1 ; index < items ; index++) {
321 GvSV(PL_defgv) = ST(index);
324 if (SvTRUE(*PL_stack_sp)) {
326 POPBLOCK(cx,PL_curpm);
331 POPBLOCK(cx,PL_curpm);
344 struct op *old_op = PL_op;
346 /* We call pp_rand here so that Drand01 get initialized if rand()
347 or srand() has not already been called
349 memzero((char*)(&dmy_op), sizeof(struct op));
350 /* we let pp_rand() borrow the TARG allocated for this XS sub */
351 dmy_op.op_targ = PL_op->op_targ;
353 (void)*(PL_ppaddr[OP_RAND])(aTHX);
355 for (index = items ; index > 1 ; ) {
356 int swap = (int)(Drand01() * (double)(index--));
358 ST(swap) = ST(index);
365 MODULE=List::Util PACKAGE=Scalar::Util
375 char *ptr = SvPV(str,len);
376 ST(0) = sv_newmortal();
377 (void)SvUPGRADE(ST(0),SVt_PVNV);
378 sv_setpvn(ST(0),ptr,len);
379 if(SvNOK(num) || SvPOK(num) || SvMAGICAL(num)) {
380 SvNV_set(ST(0), SvNV(num));
384 else if (SvUOK(num)) {
385 SvUV_set(ST(0), SvUV(num));
391 SvIV_set(ST(0), SvIV(num));
394 if(PL_tainting && (SvTAINTED(num) || SvTAINTED(str)))
407 if(!sv_isobject(sv)) {
410 RETVAL = sv_reftype(SvRV(sv),TRUE);
426 RETVAL = sv_reftype(SvRV(sv),FALSE);
442 RETVAL = PTR2UV(SvRV(sv));
455 croak("weak references are not implemented in this release of perl");
464 ST(0) = boolSV(SvROK(sv) && SvWEAKREF(sv));
467 croak("weak references are not implemented in this release of perl");
475 RETVAL = SvREADONLY(sv);
484 RETVAL = SvTAINTED(sv);
494 ST(0) = boolSV(SvVOK(sv));
497 croak("vstrings are not implemented in this release of perl");
501 looks_like_number(sv)
505 RETVAL = looks_like_number(sv);
510 set_prototype(subref, proto)
517 SV *sv = SvRV(subref);
518 if (SvTYPE(sv) != SVt_PVCV) {
519 /* not a subroutine reference */
520 croak("set_prototype: not a subroutine reference");
523 /* set the prototype */
525 char *ptr = SvPV(proto, len);
526 sv_setpvn(sv, ptr, len);
529 /* delete the prototype */
534 croak("set_prototype: not a reference");
541 #if !defined(SvWEAKREF) || !defined(SvVOK)
542 HV *stash = gv_stashpvn("Scalar::Util", 12, TRUE);
543 GV *vargv = *(GV**)hv_fetch(stash, "EXPORT_FAIL", 11, TRUE);
545 if (SvTYPE(vargv) != SVt_PVGV)
546 gv_init(vargv, stash, "Scalar::Util", 12, TRUE);
547 varav = GvAVn(vargv);
550 av_push(varav, newSVpv("weaken",6));
551 av_push(varav, newSVpv("isweak",6));
554 av_push(varav, newSVpv("isvstring",9));