-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScalarUtil.xs
316 lines (281 loc) · 5.74 KB
/
ScalarUtil.xs
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
/* Copyright (c) 1997-2000 Graham Barr <[email protected]>. All rights reserved.
* This program is free software; you can redistribute it and/or
* modify it under the same terms as Perl itself.
*/
#define PERL_NO_GET_CONTEXT /* we want efficiency */
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#define NEED_sv_2pv_flags
#include "ppport.h"
#ifndef sv_copypv
#define sv_copypv(a, b) my_sv_copypv(aTHX_ a, b)
static void
my_sv_copypv(pTHX_ SV *const dsv, SV *const ssv)
{
STRLEN len;
const char * const s = SvPV_const(ssv,len);
sv_setpvn(dsv,s,len);
if(SvUTF8(ssv))
SvUTF8_on(dsv);
else
SvUTF8_off(dsv);
}
#endif
#if PERL_VERSION < 14
# define croak_no_modify() croak("%s", PL_no_modify)
#endif
MODULE=Scalar::Util PACKAGE=Scalar::Util
void
dualvar(num,str)
SV *num
SV *str
PROTOTYPE: $$
CODE:
{
dXSTARG;
(void)SvUPGRADE(TARG, SVt_PVNV);
sv_copypv(TARG,str);
if(SvNOK(num) || SvPOK(num) || SvMAGICAL(num)) {
SvNV_set(TARG, SvNV(num));
SvNOK_on(TARG);
}
#ifdef SVf_IVisUV
else if(SvUOK(num)) {
SvUV_set(TARG, SvUV(num));
SvIOK_on(TARG);
SvIsUV_on(TARG);
}
#endif
else {
SvIV_set(TARG, SvIV(num));
SvIOK_on(TARG);
}
if(PL_tainting && (SvTAINTED(num) || SvTAINTED(str)))
SvTAINTED_on(TARG);
ST(0) = TARG;
XSRETURN(1);
}
void
isdual(sv)
SV *sv
PROTOTYPE: $
CODE:
if(SvMAGICAL(sv))
mg_get(sv);
ST(0) = boolSV((SvPOK(sv) || SvPOKp(sv)) && (SvNIOK(sv) || SvNIOKp(sv)));
XSRETURN(1);
char *
blessed(sv)
SV *sv
PROTOTYPE: $
CODE:
{
SvGETMAGIC(sv);
if(!(SvROK(sv) && SvOBJECT(SvRV(sv))))
XSRETURN_UNDEF;
RETVAL = (char*)sv_reftype(SvRV(sv),TRUE);
}
OUTPUT:
RETVAL
char *
reftype(sv)
SV *sv
PROTOTYPE: $
CODE:
{
SvGETMAGIC(sv);
if(!SvROK(sv))
XSRETURN_UNDEF;
RETVAL = (char*)sv_reftype(SvRV(sv),FALSE);
}
OUTPUT:
RETVAL
UV
refaddr(sv)
SV *sv
PROTOTYPE: $
CODE:
{
SvGETMAGIC(sv);
if(!SvROK(sv))
XSRETURN_UNDEF;
RETVAL = PTR2UV(SvRV(sv));
}
OUTPUT:
RETVAL
void
weaken(sv)
SV *sv
PROTOTYPE: $
CODE:
#ifdef SvWEAKREF
sv_rvweaken(sv);
#else
croak("weak references are not implemented in this release of perl");
#endif
void
unweaken(sv)
SV *sv
PROTOTYPE: $
INIT:
SV *tsv;
CODE:
#ifdef SvWEAKREF
/* This code stolen from core's sv_rvweaken() and modified */
if (!SvOK(sv))
return;
if (!SvROK(sv))
croak("Can't unweaken a nonreference");
else if (!SvWEAKREF(sv)) {
if(ckWARN(WARN_MISC))
warn("Reference is not weak");
return;
}
else if (SvREADONLY(sv)) croak_no_modify();
tsv = SvRV(sv);
#if PERL_VERSION >= 14
SvWEAKREF_off(sv); SvROK_on(sv);
SvREFCNT_inc_NN(tsv);
Perl_sv_del_backref(aTHX_ tsv, sv);
#else
/* Lacking sv_del_backref() the best we can do is clear the old (weak) ref
* then set a new strong one
*/
sv_setsv(sv, &PL_sv_undef);
SvRV_set(sv, SvREFCNT_inc_NN(tsv));
SvROK_on(sv);
#endif
#else
croak("weak references are not implemented in this release of perl");
#endif
void
isweak(sv)
SV *sv
PROTOTYPE: $
CODE:
#ifdef SvWEAKREF
ST(0) = boolSV(SvROK(sv) && SvWEAKREF(sv));
XSRETURN(1);
#else
croak("weak references are not implemented in this release of perl");
#endif
int
readonly(sv)
SV *sv
PROTOTYPE: $
CODE:
SvGETMAGIC(sv);
RETVAL = SvREADONLY(sv);
OUTPUT:
RETVAL
int
tainted(sv)
SV *sv
PROTOTYPE: $
CODE:
SvGETMAGIC(sv);
RETVAL = SvTAINTED(sv);
OUTPUT:
RETVAL
void
isvstring(sv)
SV *sv
PROTOTYPE: $
CODE:
#ifdef SvVOK
SvGETMAGIC(sv);
ST(0) = boolSV(SvVOK(sv));
XSRETURN(1);
#else
croak("vstrings are not implemented in this release of perl");
#endif
SV *
looks_like_number(sv)
SV *sv
PROTOTYPE: $
CODE:
SV *tempsv;
SvGETMAGIC(sv);
if(SvAMAGIC(sv) && (tempsv = AMG_CALLun(sv, numer))) {
sv = tempsv;
}
#if PERL_BCDVERSION < 0x5008005
if(SvPOK(sv) || SvPOKp(sv)) {
RETVAL = looks_like_number(sv) ? &PL_sv_yes : &PL_sv_no;
}
else {
RETVAL = (SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK)) ? &PL_sv_yes : &PL_sv_no;
}
#else
RETVAL = looks_like_number(sv) ? &PL_sv_yes : &PL_sv_no;
#endif
OUTPUT:
RETVAL
void
openhandle(SV *sv)
PROTOTYPE: $
CODE:
{
IO *io = NULL;
SvGETMAGIC(sv);
if(SvROK(sv)){
/* deref first */
sv = SvRV(sv);
}
/* must be GLOB or IO */
if(isGV(sv)){
io = GvIO((GV*)sv);
}
else if(SvTYPE(sv) == SVt_PVIO){
io = (IO*)sv;
}
if(io){
/* real or tied filehandle? */
if(IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)){
XSRETURN(1);
}
}
XSRETURN_UNDEF;
}
void
set_prototype(code, proto)
SV *code
SV *proto
PREINIT:
SV *cv; /* not CV * */
PPCODE:
SvGETMAGIC(code);
if(!SvROK(code))
croak("set_prototype: not a reference");
cv = SvRV(code);
if(SvTYPE(cv) != SVt_PVCV)
croak("set_prototype: not a subroutine reference");
if(SvPOK(proto)) {
/* set the prototype */
sv_copypv(cv, proto);
}
else {
/* delete the prototype */
SvPOK_off(cv);
}
PUSHs(code);
XSRETURN(1);
BOOT:
{
#if !defined(SvWEAKREF) || !defined(SvVOK)
HV *su_stash = gv_stashpvn("Scalar::Util", 12, TRUE);
GV *vargv = *(GV**)hv_fetch(su_stash, "EXPORT_FAIL", 11, TRUE);
AV *varav;
if(SvTYPE(vargv) != SVt_PVGV)
gv_init(vargv, su_stash, "Scalar::Util", 12, TRUE);
varav = GvAVn(vargv);
#endif
#ifndef SvWEAKREF
av_push(varav, newSVpv("weaken",6));
av_push(varav, newSVpv("isweak",6));
#endif
#ifndef SvVOK
av_push(varav, newSVpv("isvstring",9));
#endif
}