summaryrefslogtreecommitdiff
path: root/vxs.inc
blob: 80bb8ebad574fb50997e0f8b5d64b87bcac8bad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* This file is part of the "version" CPAN distribution.  Please avoid
   editing it in the perl core. */

#ifdef PERL_CORE
#  define VXS_CLASS "version"
#  define VXSp(name) XS_##name
/* VXSXSDP = XSUB Details Proto */
#  define VXSXSDP(x) x, 0
#else
#  define VXS_CLASS "version::vxs"
#  define VXSp(name) VXS_##name
/* proto member is unused in version, it is used in CORE by non version xsubs */
#  define VXSXSDP(x)
#endif

#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) static XSPROTO(name)
#endif

#define VXS(name) XS_INTERNAL(VXSp(name)); XS_INTERNAL(VXSp(name))

/* uses PUSHs, so SP must be at start, PUSHs sv on Perl stack, then returns from
   xsub; this is a little more machine code/tailcall friendly than mPUSHs(foo);
   PUTBACK; return; */

#define VXS_RETURN_M_SV(sv) \
    STMT_START {							\
	SV * sv_vtc = sv;						\
	PUSHs(sv_vtc);							\
	PUTBACK;							\
	sv_2mortal(sv_vtc);						\
	return;								\
    } STMT_END


#ifdef VXS_XSUB_DETAILS
#  ifdef PERL_CORE
    {"UNIVERSAL::VERSION", VXSp(universal_version), VXSXSDP(NULL)},
#  endif
    {VXS_CLASS "::_VERSION", VXSp(universal_version), VXSXSDP(NULL)},
    {VXS_CLASS "::()", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::new", VXSp(version_new), VXSXSDP(NULL)},
    {VXS_CLASS "::parse", VXSp(version_new), VXSXSDP(NULL)},
    {VXS_CLASS "::(\"\"", VXSp(version_stringify), VXSXSDP(NULL)},
    {VXS_CLASS "::stringify", VXSp(version_stringify), VXSXSDP(NULL)},
    {VXS_CLASS "::(0+", VXSp(version_numify), VXSXSDP(NULL)},
    {VXS_CLASS "::numify", VXSp(version_numify), VXSXSDP(NULL)},
    {VXS_CLASS "::normal", VXSp(version_normal), VXSXSDP(NULL)},
    {VXS_CLASS "::(cmp", VXSp(version_vcmp), VXSXSDP(NULL)},
    {VXS_CLASS "::(<=>", VXSp(version_vcmp), VXSXSDP(NULL)},
#  ifdef PERL_CORE
    {VXS_CLASS "::vcmp", XS_version_vcmp, VXSXSDP(NULL)},
#  else
    {VXS_CLASS "::VCMP", VXS_version_vcmp, VXSXSDP(NULL)},
#  endif
    {VXS_CLASS "::(bool", VXSp(version_boolean), VXSXSDP(NULL)},
    {VXS_CLASS "::boolean", VXSp(version_boolean), VXSXSDP(NULL)},
    {VXS_CLASS "::(+", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::(-", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::(*", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::(/", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::(+=", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::(-=", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::(*=", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::(/=", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::(abs", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::(nomethod", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::noop", VXSp(version_noop), VXSXSDP(NULL)},
    {VXS_CLASS "::is_alpha", VXSp(version_is_alpha), VXSXSDP(NULL)},
    {VXS_CLASS "::qv", VXSp(version_qv), VXSXSDP(NULL)},
    {VXS_CLASS "::declare", VXSp(version_qv), VXSXSDP(NULL)},
    {VXS_CLASS "::is_qv", VXSp(version_is_qv), VXSXSDP(NULL)},
#else

#ifndef dVAR
#  define dVAR
#endif

#ifdef HvNAME_HEK
typedef HEK HVNAME;
#  ifndef HEKf
#    define HEKfARG(arg)	((void*)(sv_2mortal(newSVhek(arg))))
#    define HEKf		SVf
#  endif
#else
typedef char HVNAME;
#  define HvNAME_HEK	HvNAME_get
#  define HEKfARG(arg)	arg
#  define HEKf		"s"
#endif

VXS(universal_version)
{
    dXSARGS;
    HV *pkg;
    GV **gvp;
    GV *gv;
    SV *sv;
    const char *undef;
    PERL_UNUSED_ARG(cv);

    if (items < 1)
       Perl_croak(aTHX_ "Usage: UNIVERSAL::VERSION(sv, ...)");

    sv = ST(0);

    if (SvROK(sv)) {
        sv = (SV*)SvRV(sv);
        if (!SvOBJECT(sv))
            Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
        pkg = SvSTASH(sv);
    }
    else {
        pkg = gv_stashsv(sv, FALSE);
    }

    gvp = pkg ? (GV**)hv_fetchs(pkg,"VERSION",FALSE) : (GV**)NULL;

    if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
        sv = sv_mortalcopy(sv);
	if ( ! ISA_VERSION_OBJ(sv) )
	    UPG_VERSION(sv, FALSE);
        undef = NULL;
    }
    else {
        sv = &PL_sv_undef;
        undef = "(undef)";
    }

    if (items > 1) {
	SV *req = ST(1);

	if (undef) {
	    if (pkg) {
		const HVNAME* const name = HvNAME_HEK(pkg);
		Perl_croak(aTHX_
			   "%" HEKf " does not define $%" HEKf
			   "::VERSION--version check failed",
			   HEKfARG(name), HEKfARG(name));
	    }
	    else {
#if PERL_VERSION_GE(5,8,0)
		Perl_croak(aTHX_
			     "%" SVf " defines neither package nor VERSION--"
                             "version check failed",
			     (void*)(ST(0)) );
#else
		Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed",
			   SvPVx_nolen_const(ST(0)),
			   SvPVx_nolen_const(ST(0)) );
#endif
	    }
	}

	if ( ! ISA_VERSION_OBJ(req) ) {
	    /* req may very well be R/O, so create a new object */
	    req = sv_2mortal( NEW_VERSION(req) );
	}

	if ( VCMP( req, sv ) > 0 ) {
	    if ( hv_exists(MUTABLE_HV(SvRV(req)), "qv", 2 ) ) {
		req = VNORMAL(req);
		sv  = VNORMAL(sv);
	    }
	    else {
		req = VSTRINGIFY(req);
		sv  = VSTRINGIFY(sv);
	    }
	    Perl_croak(aTHX_ "%" HEKf " version %" SVf " required--"
		"this is only version %" SVf, HEKfARG(HvNAME_HEK(pkg)),
		SVfARG(sv_2mortal(req)),
		SVfARG(sv_2mortal(sv)));
	}
    }

    /* if the package's $VERSION is not undef, it is upgraded to be a version object */
    if (ISA_VERSION_OBJ(sv)) {
	ST(0) = sv_2mortal(VSTRINGIFY(sv));
    } else {
	ST(0) = sv;
    }

    XSRETURN(1);
}

VXS(version_new)
{
    dXSARGS;
    SV *vs;
    SV *rv;
    const char * classname = "";
    STRLEN len;
    U32 flags = 0;
    SV * svarg0 = NULL;
    PERL_UNUSED_VAR(cv);

    SP -= items;

    switch((U32)items) {
    case 3: {
        SV * svarg2;
        vs = sv_newmortal();
        svarg2 = ST(2);
        Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(svarg2));
        break;
    }
    case 2:
        vs = ST(1);
    /* Just in case this is something like a tied hash */
        SvGETMAGIC(vs);
        if(SvOK(vs))
            break;
        /* fall through */
    case 1:
        /* no param or explicit undef */
        /* create empty object */
        vs = sv_newmortal();
        sv_setpvs(vs,"undef");
        break;
    default:
    case 0:
        Perl_croak_nocontext("Usage: version::new(class, version)");
    }

    svarg0 = ST(0);
    if ( sv_isobject(svarg0) ) {
	/* get the class if called as an object method */
	const HV * stash = SvSTASH(SvRV(svarg0));
	classname = HvNAME_get(stash);
	len	  = HvNAMELEN_get(stash);
#ifdef HvNAMEUTF8
	flags	  = HvNAMEUTF8(stash) ? SVf_UTF8 : 0;
#endif
    }
    else {
	classname = SvPV_nomg(svarg0, len);
	flags     = SvUTF8(svarg0);
    }

    rv = NEW_VERSION(vs);
    if ( len != sizeof(VXS_CLASS)-1
      || strcmp(classname,VXS_CLASS) != 0 ) /* inherited new() */
        sv_bless(rv, gv_stashpvn(classname, len, GV_ADD | flags));

    VXS_RETURN_M_SV(rv);
}

#define VTYPECHECK(var, val, varname) \
    STMT_START {							\
	SV * sv_vtc = val;						\
	if (ISA_VERSION_OBJ(sv_vtc)) {				\
	    (var) = SvRV(sv_vtc);						\
	}								\
	else								\
	    Perl_croak_nocontext(varname " is not of type version");	\
    } STMT_END

VXS(version_stringify)
{
     dXSARGS;
     if (items < 1)
	 croak_xs_usage(cv, "lobj, ...");
     SP -= items;
     {
	  SV *	lobj;
	  VTYPECHECK(lobj, ST(0), "lobj");

	  VXS_RETURN_M_SV(VSTRINGIFY(lobj));
     }
}

VXS(version_numify)
{
     dXSARGS;
     if (items < 1)
	 croak_xs_usage(cv, "lobj, ...");
     SP -= items;
     {
	  SV *	lobj;
	  VTYPECHECK(lobj, ST(0), "lobj");
	  VXS_RETURN_M_SV(VNUMIFY(lobj));
     }
}

VXS(version_normal)
{
     dXSARGS;
     if (items != 1)
	 croak_xs_usage(cv, "ver");
     SP -= items;
     {
	  SV *	ver;
	  VTYPECHECK(ver, ST(0), "ver");

	  VXS_RETURN_M_SV(VNORMAL(ver));
     }
}

VXS(version_vcmp)
{
     dXSARGS;
     if (items < 2)
	 croak_xs_usage(cv, "lobj, robj, ...");
     SP -= items;
     {
	  SV *	lobj;
	  VTYPECHECK(lobj, ST(0), "lobj");
	  {
	       SV	*rs;
	       SV	*rvs;
	       SV * robj = ST(1);
	       const int swap = items > 2 ? SvTRUE(ST(2)) : 0;

	       if ( !ISA_VERSION_OBJ(robj) )
	       {
		    robj = sv_2mortal(NEW_VERSION(SvOK(robj) ? robj : newSVpvs_flags("0", SVs_TEMP)));
	       }
	       rvs = SvRV(robj);

	       if ( swap )
	       {
		    rs = newSViv(VCMP(rvs,lobj));
	       }
	       else
	       {
		    rs = newSViv(VCMP(lobj,rvs));
	       }

	       VXS_RETURN_M_SV(rs);
	  }
     }
}

VXS(version_boolean)
{
    dXSARGS;
    SV *lobj;
    if (items < 1)
	croak_xs_usage(cv, "lobj, ...");
    SP -= items;
    VTYPECHECK(lobj, ST(0), "lobj");
    {
	SV * const rs =
	    newSViv( VCMP(lobj,
			  sv_2mortal(NEW_VERSION(
					sv_2mortal(newSVpvs("0"))
				    ))
			 )
		   );
	VXS_RETURN_M_SV(rs);
    }
}

VXS(version_noop)
{
    dXSARGS;
    if (items < 1)
	croak_xs_usage(cv, "lobj, ...");
    if (ISA_VERSION_OBJ(ST(0)))
	Perl_croak(aTHX_ "operation not supported with version object");
    else
	Perl_croak(aTHX_ "lobj is not of type version");
    XSRETURN_EMPTY;
}

static
void
S_version_check_key(pTHX_ CV * cv, const char * key, int keylen)
{
    dXSARGS;
    if (items != 1)
	croak_xs_usage(cv, "lobj");
    {
	SV *lobj = POPs;
	SV *ret;
	VTYPECHECK(lobj, lobj, "lobj");
	if ( hv_exists(MUTABLE_HV(lobj), key, keylen ) )
	    ret = &PL_sv_yes;
	else
	    ret = &PL_sv_no;
	PUSHs(ret);
	PUTBACK;
	return;
    }
}

VXS(version_is_alpha)
{
    S_version_check_key(aTHX_ cv, "alpha", 5);
}

VXS(version_qv)
{
    dXSARGS;
    PERL_UNUSED_ARG(cv);
    SP -= items;
    {
	SV * ver = ST(0);
	SV * sv0 = ver;
	SV * rv;
        STRLEN len = 0;
        const char * classname = "";
        U32 flags = 0;
        if ( items == 2 ) {
	    SV * sv1 = ST(1);
	    SvGETMAGIC(sv1);
	    if (SvOK(sv1)) {
		ver = sv1;
	    }
	    else {
		Perl_croak(aTHX_ "Invalid version format (version required)");
	    }
            if ( sv_isobject(sv0) ) { /* class called as an object method */
                const HV * stash = SvSTASH(SvRV(sv0));
                classname = HvNAME_get(stash);
                len       = HvNAMELEN_get(stash);
#ifdef HvNAMEUTF8
                flags     = HvNAMEUTF8(stash) ? SVf_UTF8 : 0;
#endif
            }
            else {
	       classname = SvPV(sv0, len);
                flags     = SvUTF8(sv0);
            }
	}
	if ( !SvVOK(ver) ) { /* not already a v-string */
	    rv = sv_newmortal();
	    SvSetSV_nosteal(rv,ver); /* make a duplicate */
	    UPG_VERSION(rv, TRUE);
	} else {
	    rv = sv_2mortal(NEW_VERSION(ver));
	}
	if ( items == 2 && (len != 7
                || strcmp(classname,"version")) ) { /* inherited new() */
	    sv_bless(rv, gv_stashpvn(classname, len, GV_ADD | flags));
        }
	PUSHs(rv);
    }
    PUTBACK;
    return;
}


VXS(version_is_qv)
{
    S_version_check_key(aTHX_ cv, "qv", 2);
}

#endif