diff options
-rw-r--r-- | builtin.c | 53 | ||||
-rw-r--r-- | ext/Opcode/Opcode.pm | 1 | ||||
-rw-r--r-- | lib/B/Deparse.pm | 5 | ||||
-rw-r--r-- | lib/B/Deparse.t | 6 | ||||
-rw-r--r-- | lib/B/Op_private.pm | 7 | ||||
-rw-r--r-- | lib/builtin.pm | 27 | ||||
-rw-r--r-- | lib/builtin.t | 20 | ||||
-rw-r--r-- | opcode.h | 25 | ||||
-rw-r--r-- | opnames.h | 5 | ||||
-rw-r--r-- | pod/perlref.pod | 3 | ||||
-rw-r--r-- | pp.c | 31 | ||||
-rw-r--r-- | pp_proto.h | 3 | ||||
-rw-r--r-- | regen/opcodes | 3 | ||||
-rw-r--r-- | t/perf/opcount.t | 21 |
14 files changed, 186 insertions, 24 deletions
@@ -71,12 +71,8 @@ static OP *ck_builtin_const(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) return newSVOP(OP_CONST, 0, constval); } -enum { - BUILTIN_FUNC1_ISBOOL = 1, -}; - -XS(XS_builtin_func1); -XS(XS_builtin_func1) +XS(XS_builtin_func1_scalar); +XS(XS_builtin_func1_scalar) { dXSARGS; dXSI32; @@ -85,17 +81,46 @@ XS(XS_builtin_func1) croak_xs_usage(cv, "arg"); switch(ix) { - case BUILTIN_FUNC1_ISBOOL: + case OP_ISBOOL: Perl_pp_isbool(aTHX); break; + case OP_ISWEAK: + Perl_pp_isweak(aTHX); + break; + default: - Perl_die(aTHX_ "panic: unhandled ix value %d for xs_builtin_func1()", ix); + Perl_die(aTHX_ "panic: unhandled opcode %d for xs_builtin_func1_scalar()", ix); } XSRETURN(1); } +XS(XS_builtin_func1_void); +XS(XS_builtin_func1_void) +{ + dXSARGS; + dXSI32; + + if(items != 1) + croak_xs_usage(cv, "arg"); + + switch(ix) { + case OP_WEAKEN: + Perl_pp_weaken(aTHX); + break; + + case OP_UNWEAKEN: + Perl_pp_unweaken(aTHX); + break; + + default: + Perl_die(aTHX_ "panic: unhandled opcode %d for xs_builtin_func1_void()", ix); + } + + XSRETURN(0); +} + static OP *ck_builtin_func1(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) { const struct BuiltinFuncDescriptor *builtin = NUM2PTR(const struct BuiltinFuncDescriptor *, SvUV(ckobj)); @@ -125,12 +150,7 @@ static OP *ck_builtin_func1(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) op_free(entersubop); - OPCODE opcode; - switch(builtin->ckval) { - case BUILTIN_FUNC1_ISBOOL: opcode = OP_ISBOOL; break; - default: - DIE(aTHX_ "panic: unhandled ckval value %" IVdf " for ck_builtin_func1()", builtin->ckval); - } + OPCODE opcode = builtin->ckval; return newUNOP(opcode, wantflags, argop); } @@ -143,7 +163,10 @@ static const struct BuiltinFuncDescriptor builtins[] = { { "builtin::false", &XS_builtin_false, &ck_builtin_const, BUILTIN_CONST_FALSE }, /* unary functions */ - { "builtin::isbool", &XS_builtin_func1, &ck_builtin_func1, BUILTIN_FUNC1_ISBOOL }, + { "builtin::isbool", &XS_builtin_func1_scalar, &ck_builtin_func1, OP_ISBOOL }, + { "builtin::weaken", &XS_builtin_func1_void, &ck_builtin_func1, OP_WEAKEN }, + { "builtin::unweaken", &XS_builtin_func1_void, &ck_builtin_func1, OP_UNWEAKEN }, + { "builtin::isweak", &XS_builtin_func1_scalar, &ck_builtin_func1, OP_ISWEAK }, { 0 } }; diff --git a/ext/Opcode/Opcode.pm b/ext/Opcode/Opcode.pm index ff6640c3fa..b4c321a306 100644 --- a/ext/Opcode/Opcode.pm +++ b/ext/Opcode/Opcode.pm @@ -354,6 +354,7 @@ invert_opset function. cmpchain_and cmpchain_dup isbool + isweak weaken unweaken leaveeval -- needed for Safe to operate, is safe without entereval diff --git a/lib/B/Deparse.pm b/lib/B/Deparse.pm index ec4a8a5469..14a13bcee4 100644 --- a/lib/B/Deparse.pm +++ b/lib/B/Deparse.pm @@ -6611,7 +6611,10 @@ sub builtin1 { return "builtin::$name($arg)"; } -sub pp_isbool { builtin1(@_, "isbool") } +sub pp_isbool { $_[0]->maybe_targmy(@_[1,2], \&builtin1, "isbool"); } +sub pp_isweak { $_[0]->maybe_targmy(@_[1,2], \&builtin1, "isweak"); } +sub pp_weaken { builtin1(@_, "weaken"); } +sub pp_unweaken { builtin1(@_, "unweaken"); } 1; __END__ diff --git a/lib/B/Deparse.t b/lib/B/Deparse.t index 138b31bdf3..9fafd84bf3 100644 --- a/lib/B/Deparse.t +++ b/lib/B/Deparse.t @@ -3200,4 +3200,8 @@ defer { } #### # builtin:: functions -my $x = builtin::isbool(undef); +my $x; +$x = builtin::isbool(undef); +$x = builtin::isweak(undef); +builtin::weaken($x); +builtin::unweaken($x); diff --git a/lib/B/Op_private.pm b/lib/B/Op_private.pm index be96920b6f..208ffe4bc0 100644 --- a/lib/B/Op_private.pm +++ b/lib/B/Op_private.pm @@ -149,7 +149,7 @@ $bits{$_}{6} = 'OPpPAD_STATE' for qw(lvavref lvref padav padhv padsv pushmark re $bits{$_}{7} = 'OPpPV_IS_UTF8' for qw(dump goto last next redo); $bits{$_}{6} = 'OPpREFCOUNTED' for qw(leave leaveeval leavesub leavesublv leavewrite); $bits{$_}{2} = 'OPpSLICEWARNING' for qw(aslice hslice padav padhv rv2av rv2hv); -$bits{$_}{4} = 'OPpTARGET_MY' for qw(abs add atan2 chdir chmod chomp chown chr chroot concat cos crypt divide exec exp flock getpgrp getppid getpriority hex i_add i_divide i_modulo i_multiply i_subtract index int isbool kill left_shift length link log mkdir modulo multiconcat multiply nbit_and nbit_or nbit_xor ncomplement oct ord pow push rand rename right_shift rindex rmdir schomp scomplement setpgrp setpriority sin sleep sqrt srand stringify subtract symlink system time unlink unshift utime wait waitpid); +$bits{$_}{4} = 'OPpTARGET_MY' for qw(abs add atan2 chdir chmod chomp chown chr chroot concat cos crypt divide exec exp flock getpgrp getppid getpriority hex i_add i_divide i_modulo i_multiply i_subtract index int isbool isweak kill left_shift length link log mkdir modulo multiconcat multiply nbit_and nbit_or nbit_xor ncomplement oct ord pow push rand rename right_shift rindex rmdir schomp scomplement setpgrp setpriority sin sleep sqrt srand stringify subtract symlink system time unlink unshift utime wait waitpid); $bits{$_}{0} = 'OPpTRANS_CAN_FORCE_UTF8' for qw(trans transr); $bits{$_}{5} = 'OPpTRANS_COMPLEMENT' for qw(trans transr); $bits{$_}{7} = 'OPpTRANS_DELETE' for qw(trans transr); @@ -402,6 +402,7 @@ $bits{int}{0} = $bf[0]; @{$bits{ioctl}}{3,2,1,0} = ($bf[4], $bf[4], $bf[4], $bf[4]); @{$bits{isa}}{1,0} = ($bf[1], $bf[1]); $bits{isbool}{0} = $bf[0]; +$bits{isweak}{0} = $bf[0]; @{$bits{join}}{3,2,1,0} = ($bf[4], $bf[4], $bf[4], $bf[4]); $bits{keys}{0} = $bf[0]; @{$bits{kill}}{3,2,1,0} = ($bf[4], $bf[4], $bf[4], $bf[4]); @@ -570,11 +571,13 @@ $bits{undef}{0} = $bf[0]; @{$bits{unpack}}{3,2,1,0} = ($bf[4], $bf[4], $bf[4], $bf[4]); @{$bits{unshift}}{3,2,1,0} = ($bf[4], $bf[4], $bf[4], $bf[4]); $bits{untie}{0} = $bf[0]; +$bits{unweaken}{0} = $bf[0]; @{$bits{utime}}{3,2,1,0} = ($bf[4], $bf[4], $bf[4], $bf[4]); $bits{values}{0} = $bf[0]; @{$bits{vec}}{1,0} = ($bf[1], $bf[1]); @{$bits{waitpid}}{3,2,1,0} = ($bf[4], $bf[4], $bf[4], $bf[4]); @{$bits{warn}}{3,2,1,0} = ($bf[4], $bf[4], $bf[4], $bf[4]); +$bits{weaken}{0} = $bf[0]; @{$bits{xor}}{1,0} = ($bf[1], $bf[1]); @@ -838,7 +841,7 @@ our %ops_using = ( OPpSORT_DESCEND => [qw(sort)], OPpSPLIT_ASSIGN => [qw(split)], OPpSUBSTR_REPL_FIRST => [qw(substr)], - OPpTARGET_MY => [qw(abs add atan2 chdir chmod chomp chown chr chroot concat cos crypt divide exec exp flock getpgrp getppid getpriority hex i_add i_divide i_modulo i_multiply i_subtract index int isbool kill left_shift length link log mkdir modulo multiconcat multiply nbit_and nbit_or nbit_xor ncomplement oct ord pow push rand rename right_shift rindex rmdir schomp scomplement setpgrp setpriority sin sleep sqrt srand stringify subtract symlink system time unlink unshift utime wait waitpid)], + OPpTARGET_MY => [qw(abs add atan2 chdir chmod chomp chown chr chroot concat cos crypt divide exec exp flock getpgrp getppid getpriority hex i_add i_divide i_modulo i_multiply i_subtract index int isbool isweak kill left_shift length link log mkdir modulo multiconcat multiply nbit_and nbit_or nbit_xor ncomplement oct ord pow push rand rename right_shift rindex rmdir schomp scomplement setpgrp setpriority sin sleep sqrt srand stringify subtract symlink system time unlink unshift utime wait waitpid)], OPpTRANS_CAN_FORCE_UTF8 => [qw(trans transr)], OPpTRUEBOOL => [qw(grepwhile index length padav padhv pos ref rindex rv2av rv2hv subst)], OPpUSEINT => [qw(bit_and bit_or bit_xor complement left_shift nbit_and nbit_or nbit_xor ncomplement right_shift sbit_and sbit_or sbit_xor)], diff --git a/lib/builtin.pm b/lib/builtin.pm index 461f7ca5fe..2be1cdb455 100644 --- a/lib/builtin.pm +++ b/lib/builtin.pm @@ -16,7 +16,10 @@ builtin - Perl pragma to import built-in utility functions =head1 SYNOPSIS - use builtin qw( true false isbool ); + use builtin qw( + true false isbool + weaken unweaken isweak + ); =head1 DESCRIPTION @@ -95,6 +98,28 @@ function (such as C<true> or C<isbool> itself), boolean-returning operator (such as the C<eq> or C<==> comparison tests or the C<!> negation operator), or any variable containing one of these results. +=head2 weaken + + weaken($ref); + +Weakens a reference. A weakened reference does not contribute to the reference +count of its referent. If only weaekend references to it remain then it will +be disposed of, and all remaining weak references will have their value set to +C<undef>. + +=head2 unweaken + + unweaken($ref); + +Strengthens a reference, undoing the effects of a previous call to L</weaken>. + +=head2 isweak + + $bool = isweak($ref); + +Returns true when given a weakened reference, or false if not a reference or +not weak. + =head1 SEE ALSO L<perlop>, L<perlfunc>, L<Scalar::Util> diff --git a/lib/builtin.t b/lib/builtin.t index 3338799721..4f4e33a49e 100644 --- a/lib/builtin.t +++ b/lib/builtin.t @@ -49,6 +49,26 @@ package FetchStoreCounter { is($storecount, 1, 'isbool() TARG invokes STORE magic'); } +# weakrefs +{ + use builtin qw( isweak weaken unweaken ); + + my $arr = []; + my $ref = $arr; + + ok(!isweak($ref), 'ref is not weak initially'); + + weaken($ref); + ok(isweak($ref), 'ref is weak after weaken()'); + + unweaken($ref); + ok(!isweak($ref), 'ref is not weak after unweaken()'); + + weaken($ref); + undef $arr; + ok(!defined $ref, 'ref is now undef after arr is cleared'); +} + # imports are lexical; should not be visible here { my $ok = eval 'true()'; my $e = $@; @@ -555,6 +555,9 @@ EXTCONST char* const PL_op_name[] = { "catch", "pushdefer", "isbool", + "isweak", + "weaken", + "unweaken", "freed", }; #endif @@ -969,6 +972,9 @@ EXTCONST char* const PL_op_desc[] = { "catch {} block", "push defer {} block", "boolean type test", + "weakref type test", + "reference weaken", + "reference unweaken", "freed op", }; #endif @@ -1386,6 +1392,9 @@ EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */ Perl_pp_catch, Perl_pp_pushdefer, Perl_pp_isbool, + Perl_pp_isweak, + Perl_pp_weaken, + Perl_pp_unweaken, } #endif ; @@ -1799,6 +1808,9 @@ EXT Perl_check_t PL_check[] /* or perlvars.h */ Perl_ck_null, /* catch */ Perl_ck_null, /* pushdefer */ Perl_ck_null, /* isbool */ + Perl_ck_null, /* isweak */ + Perl_ck_null, /* weaken */ + Perl_ck_null, /* unweaken */ } #endif ; @@ -2213,6 +2225,9 @@ EXTCONST U32 PL_opargs[] = { 0x00000300, /* catch */ 0x00000300, /* pushdefer */ 0x0000011e, /* isbool */ + 0x0000011e, /* isweak */ + 0x00000100, /* weaken */ + 0x00000100, /* unweaken */ }; #endif @@ -2884,6 +2899,9 @@ EXTCONST I16 PL_op_private_bitdef_ix[] = { 0, /* catch */ 0, /* pushdefer */ 75, /* isbool */ + 75, /* isweak */ + 0, /* weaken */ + 0, /* unweaken */ }; @@ -2902,7 +2920,7 @@ EXTCONST I16 PL_op_private_bitdef_ix[] = { */ EXTCONST U16 PL_op_private_bitdefs[] = { - 0x0003, /* scalar, prototype, refgen, srefgen, readline, regcmaybe, regcreset, regcomp, substcont, chop, schop, defined, undef, study, preinc, i_preinc, predec, i_predec, postinc, i_postinc, postdec, i_postdec, negate, i_negate, not, ucfirst, lcfirst, uc, lc, quotemeta, aeach, avalues, each, pop, shift, grepstart, mapstart, mapwhile, range, and, or, dor, andassign, orassign, dorassign, argcheck, argdefelem, method, method_named, method_super, method_redir, method_redir_super, entergiven, leavegiven, enterwhen, leavewhen, untie, tied, dbmclose, getsockname, getpeername, lstat, stat, readlink, readdir, telldir, rewinddir, closedir, localtime, alarm, require, dofile, entertry, ghbyname, gnbyname, gpbyname, shostent, snetent, sprotoent, sservent, gpwnam, gpwuid, ggrnam, ggrgid, lock, once, fc, anonconst, cmpchain_and, cmpchain_dup, entertrycatch, catch, pushdefer */ + 0x0003, /* scalar, prototype, refgen, srefgen, readline, regcmaybe, regcreset, regcomp, substcont, chop, schop, defined, undef, study, preinc, i_preinc, predec, i_predec, postinc, i_postinc, postdec, i_postdec, negate, i_negate, not, ucfirst, lcfirst, uc, lc, quotemeta, aeach, avalues, each, pop, shift, grepstart, mapstart, mapwhile, range, and, or, dor, andassign, orassign, dorassign, argcheck, argdefelem, method, method_named, method_super, method_redir, method_redir_super, entergiven, leavegiven, enterwhen, leavewhen, untie, tied, dbmclose, getsockname, getpeername, lstat, stat, readlink, readdir, telldir, rewinddir, closedir, localtime, alarm, require, dofile, entertry, ghbyname, gnbyname, gpbyname, shostent, snetent, sprotoent, sservent, gpwnam, gpwuid, ggrnam, ggrgid, lock, once, fc, anonconst, cmpchain_and, cmpchain_dup, entertrycatch, catch, pushdefer, weaken, unweaken */ 0x2fdc, 0x40d9, /* pushmark */ 0x00bd, /* wantarray, runcv */ 0x0438, 0x1a50, 0x418c, 0x3d28, 0x3505, /* const */ @@ -2923,7 +2941,7 @@ EXTCONST U16 PL_op_private_bitdefs[] = { 0x117c, 0x21b8, 0x09b4, 0x3fec, 0x2548, 0x4764, 0x07c1, /* trans, transr */ 0x0fbc, 0x04d8, 0x0067, /* sassign */ 0x0c78, 0x0b74, 0x0a70, 0x30cc, 0x05a8, 0x0067, /* aassign */ - 0x4530, 0x0003, /* chomp, schomp, scomplement, sin, cos, exp, log, sqrt, int, hex, oct, abs, ord, chr, chroot, rmdir, isbool */ + 0x4530, 0x0003, /* chomp, schomp, scomplement, sin, cos, exp, log, sqrt, int, hex, oct, abs, ord, chr, chroot, rmdir, isbool, isweak */ 0x05b4, 0x30cc, 0x0003, /* pos */ 0x4530, 0x0067, /* pow, multiply, i_multiply, divide, i_divide, modulo, i_modulo, add, i_add, subtract, i_subtract */ 0x1538, 0x0067, /* repeat */ @@ -3388,6 +3406,9 @@ EXTCONST U8 PL_op_private_valid[] = { /* CATCH */ (OPpARG1_MASK), /* PUSHDEFER */ (OPpARG1_MASK), /* ISBOOL */ (OPpARG1_MASK|OPpTARGET_MY), + /* ISWEAK */ (OPpARG1_MASK|OPpTARGET_MY), + /* WEAKEN */ (OPpARG1_MASK), + /* UNWEAKEN */ (OPpARG1_MASK), }; @@ -420,10 +420,13 @@ typedef enum opcode { OP_CATCH = 403, OP_PUSHDEFER = 404, OP_ISBOOL = 405, + OP_ISWEAK = 406, + OP_WEAKEN = 407, + OP_UNWEAKEN = 408, OP_max } opcode; -#define MAXO 406 +#define MAXO 409 #define OP_FREED MAXO /* the OP_IS_* macros are optimized to a simple range check because diff --git a/pod/perlref.pod b/pod/perlref.pod index 5cd9ee21f8..f14d85bb3f 100644 --- a/pod/perlref.pod +++ b/pod/perlref.pod @@ -493,7 +493,8 @@ You can break circular references by creating a "weak reference". A weak reference does not increment the reference count for a variable, which means that the object can go out of scope and be destroyed. You can weaken a reference with the C<weaken> function exported by the -L<Scalar::Util> module. +L<Scalar::Util> module, or available as C<builtin::weaken> directly in +Perl version 5.35.7 or later. Here's how we can make the first example safer: @@ -7225,6 +7225,37 @@ PP(pp_isbool) RETURN; } +PP(pp_isweak) +{ + dSP; + dTARGET; + SV *arg = POPs; + + SvGETMAGIC(arg); + + sv_setbool_mg(TARG, SvROK(arg) && SvWEAKREF(arg)); + PUSHs(TARG); + RETURN; +} + +PP(pp_weaken) +{ + dSP; + SV *arg = POPs; + + sv_rvweaken(arg); + RETURN; +} + +PP(pp_unweaken) +{ + dSP; + SV *arg = POPs; + + sv_rvunweaken(arg); + RETURN; +} + /* * ex: set ts=8 sts=4 sw=4 et: */ diff --git a/pp_proto.h b/pp_proto.h index 1210c80662..ebae2e9cf2 100644 --- a/pp_proto.h +++ b/pp_proto.h @@ -132,6 +132,7 @@ PERL_CALLCONV OP *Perl_pp_introcv(pTHX); PERL_CALLCONV OP *Perl_pp_ioctl(pTHX); PERL_CALLCONV OP *Perl_pp_isa(pTHX); PERL_CALLCONV OP *Perl_pp_isbool(pTHX); +PERL_CALLCONV OP *Perl_pp_isweak(pTHX); PERL_CALLCONV OP *Perl_pp_iter(pTHX); PERL_CALLCONV OP *Perl_pp_join(pTHX); PERL_CALLCONV OP *Perl_pp_kvaslice(pTHX); @@ -298,11 +299,13 @@ PERL_CALLCONV OP *Perl_pp_unpack(pTHX); PERL_CALLCONV OP *Perl_pp_unshift(pTHX); PERL_CALLCONV OP *Perl_pp_unstack(pTHX); PERL_CALLCONV OP *Perl_pp_untie(pTHX); +PERL_CALLCONV OP *Perl_pp_unweaken(pTHX); PERL_CALLCONV OP *Perl_pp_vec(pTHX); PERL_CALLCONV OP *Perl_pp_wait(pTHX); PERL_CALLCONV OP *Perl_pp_waitpid(pTHX); PERL_CALLCONV OP *Perl_pp_wantarray(pTHX); PERL_CALLCONV OP *Perl_pp_warn(pTHX); +PERL_CALLCONV OP *Perl_pp_weaken(pTHX); PERL_CALLCONV OP *Perl_pp_xor(pTHX); PERL_CALLCONV OP *Perl_unimplemented_op(pTHX); diff --git a/regen/opcodes b/regen/opcodes index 063a628e7a..523aa32dea 100644 --- a/regen/opcodes +++ b/regen/opcodes @@ -585,3 +585,6 @@ catch catch {} block ck_null | pushdefer push defer {} block ck_null | isbool boolean type test ck_null fsT1 +isweak weakref type test ck_null fsT1 +weaken reference weaken ck_null 1 +unweaken reference unweaken ck_null 1 diff --git a/t/perf/opcount.t b/t/perf/opcount.t index 80e3973d7d..db9786058b 100644 --- a/t/perf/opcount.t +++ b/t/perf/opcount.t @@ -712,4 +712,25 @@ test_opcount(0, "builtin::isbool gets constant-folded", const => 1, }); +test_opcount(0, "builtin::weaken is replaced with direct opcode", + sub { my $x = []; builtin::weaken($x); }, + { + entersub => 0, + weaken => 1, + }); + +test_opcount(0, "builtin::unweaken is replaced with direct opcode", + sub { my $x = []; builtin::unweaken($x); }, + { + entersub => 0, + unweaken => 1, + }); + +test_opcount(0, "builtin::isweak is replaced with direct opcode", + sub { builtin::isweak([]); }, + { + entersub => 0, + isweak => 1, + }); + done_testing(); |