summaryrefslogtreecommitdiff
path: root/builtin.c
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <leonerd@leonerd.org.uk>2022-03-14 14:31:29 +0000
committerPaul Evans <leonerd@leonerd.org.uk>2022-04-01 13:23:41 +0100
commitbd79e3f7d284479ecb035f81d788c633981e6769 (patch)
tree71b56efbd99a95436ce272ce3742d137f131e031 /builtin.c
parent26323ce3e8729c6eb9ce31f554c0038b34578ede (diff)
downloadperl-bd79e3f7d284479ecb035f81d788c633981e6769.tar.gz
Initial implementation and unit-tests of created_as_{string,number}
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c39
1 files changed, 37 insertions, 2 deletions
diff --git a/builtin.c b/builtin.c
index 648f4c6965..9df6630082 100644
--- a/builtin.c
+++ b/builtin.c
@@ -255,6 +255,36 @@ XS(XS_builtin_func1_void)
XSRETURN(0);
}
+XS(XS_builtin_created_as_string)
+{
+ dXSARGS;
+
+ if(items != 1)
+ croak_xs_usage(cv, "arg");
+
+ SV *arg = ST(0);
+ SvGETMAGIC(arg);
+
+ /* SV was created as string if it has POK and isn't bool */
+ ST(0) = boolSV(SvPOK(arg) && !SvIsBOOL(arg));
+ XSRETURN(1);
+}
+
+XS(XS_builtin_created_as_number)
+{
+ dXSARGS;
+
+ if(items != 1)
+ croak_xs_usage(cv, "arg");
+
+ SV *arg = ST(0);
+ SvGETMAGIC(arg);
+
+ /* SV was created as number if it has NOK or IOK but not POK and is not bool */
+ ST(0) = boolSV(SvNIOK(arg) && !SvPOK(arg) && !SvIsBOOL(arg));
+ XSRETURN(1);
+}
+
static OP *ck_builtin_func1(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
{
const struct BuiltinFuncDescriptor *builtin = NUM2PTR(const struct BuiltinFuncDescriptor *, SvUV(ckobj));
@@ -268,6 +298,10 @@ static OP *ck_builtin_func1(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
entersubop = ck_entersub_args_proto(entersubop, namegv, prototype);
+ OPCODE opcode = builtin->ckval;
+ if(!opcode)
+ return entersubop;
+
OP *parent = entersubop, *pushop, *argop;
pushop = cUNOPx(entersubop)->op_first;
@@ -286,8 +320,6 @@ static OP *ck_builtin_func1(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
op_free(entersubop);
- OPCODE opcode = builtin->ckval;
-
return newUNOP(opcode, wantflags, argop);
}
@@ -359,6 +391,9 @@ static const struct BuiltinFuncDescriptor builtins[] = {
{ "builtin::floor", &XS_builtin_func1_scalar, &ck_builtin_func1, OP_FLOOR },
{ "builtin::trim", &XS_builtin_trim, NULL, 0 },
+ { "builtin::created_as_string", &XS_builtin_created_as_string, &ck_builtin_func1, 0 },
+ { "builtin::created_as_number", &XS_builtin_created_as_number, &ck_builtin_func1, 0 },
+
/* list functions */
{ "builtin::indexed", &XS_builtin_indexed, &ck_builtin_funcN, 0 },
{ 0 }