Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new %{^HOOK} hash, similar to %SIG, and add support for "require__before" and "require__after" hooks. #20637

Merged
merged 7 commits into from
Mar 18, 2023

Commits on Mar 17, 2023

  1. t/lib/GH_15109 - rename to t/lib/caller

    I want to use these modules in other tests, so changing the name
    makes sense.
    demerphq committed Mar 17, 2023
    Configuration menu
    Copy the full SHA
    77efb39 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    283e506 View commit details
    Browse the repository at this point in the history
  3. sv.h - add SvREFCNT_dec_set_NULL()

    and also SvREFCNT_dec_ret_NULL() which is used to implement
    SvREFCNT_dec_set_NULL(). The set_NULL() macro is intended to
    be used to replace code like this:
    
        if (sv) {
            SvREFCNT_dec_NN(sv);
            sv = NULL;
        }
    
    The function form just facilitates it, and can be used in situations
    where returning NULL after decrementing a refcount would be reduce
    code complexity.
    demerphq committed Mar 17, 2023
    Configuration menu
    Copy the full SHA
    41bfa2b View commit details
    Browse the repository at this point in the history
  4. pod/perldiag.pod - provide full path to issues tracker

    https://github.com/Perl/perl5/issues shows the list of open issues,
    whereas https://github.com/Perl/perl5/issues/new/choose is where someone
    can create a new ticket.
    demerphq committed Mar 17, 2023
    Configuration menu
    Copy the full SHA
    de26a03 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    7dceac4 View commit details
    Browse the repository at this point in the history
  6. scope.c - add mortal_destructor_sv() and mortal_svfunc_x()

    The function SAVEDESTRUCTOR_X() (save_destructor_x) can be used to
    execute a C function at the end of the current psuedo-block. Prior to
    this patch there was no "mortal" equivalent that would execute at the
    end of the current statement. We offer a collection of functions which
    are intended to free SV's at either point in time, but only support
    callbacks at the end of the current pseudo-block.
    
    This patch adds two such functions, "mortal_destructor_sv" which can be
    used to trigger a perl code reference to execute at the end of the
    current statement, and "mortal_svfunc_x" which can be used to trigger an
    SVFUNC_t C function at the end of the current statement.
    
    Both functions differ from save_destructor_x() in that instead of
    supporting a void pointer argument they both require their argument to
    be some sort of SV pointer. The Perl callback function triggered by
    "mortal_destructor_sv" may be provided no arguments, a single argument
    or a list of arguments, depending on the type of argument provided to
    mortal_destructor_sv(): when the argument is a raw AV (with no SV ref
    wrapping it), then the contents of the AV are passed in as a list of
    arguments. When the argument is anything else but NULL, the argument is
    provided as a single argument, and when it is NULL the perl function is
    called with no arguments.
    
    Both functions are implemented on top of a mortal SV (unseen by the
    user) which has PERL_MAGIC_destruct magic associated with it, which
    triggers the destructor behavior when the SV is freed.
    
    Both functions are provided with macros to match the normal SAVExx()
    API, with MORTALDESTRUCTOR_SV() wrapping mortal_destructor_sv() and
    MORTALSVFUNC_X() wrapping mortal_svfunc_x().
    
    The heart of this logic cribbed from Leon Timmermans' Variable-OnDestruct.
    See the code at:
    
    https://metacpan.org/dist/Variable-OnDestruct/source/lib/Variable/OnDestruct.xs#L6-17
    
    I am very grateful to him for his help on this. Any errors or omissions
    in this code are my fault, not his.
    demerphq committed Mar 17, 2023
    Configuration menu
    Copy the full SHA
    9961aac View commit details
    Browse the repository at this point in the history
  7. pp_ctl.c - add support for hooking require.

    This defines a new magic hash C<%{^HOOK}> which is intended to be used for
    hooking keywords. It is similar to %SIG in that the values it contains
    are validated on set, and it is not allowed to store something in
    C<%{^HOOK}> that isn't supposed to be there. Hooks are expected to be
    coderefs (people can use currying if they really want to put an object
    in there, the API is deliberately simple.)
    
    The C<%{^HOOK}> hash is documented to have keys of the form
    "${keyword}__${phase}" where $phase is either "before" or "after"
    and in this initial release two hooks are supported,
    "require__before" and "require__after":
    
    The C<require__before> hook is called before require is executed,
    including any @inc hooks that might be fired. It is called with the path
    of the file being required, just as would be stored in %INC. The hook
    may alter the filename by writing to $_[0] and it may return a coderef
    to be executed *after* the require has completed, otherwise the return
    is ignored.  This coderef is also called with the path of the file which
    was required, and it will be called regardless as to whether the require
    (or its dependencies) die during execution.  This mechanism makes it
    trivial and safe to share state between the initial hook and the coderef
    it returns.
    
    The C<require__after> hook is similar to the C<require__before> hook
    however except that it is called after the require completes
    (successfully or not), and its return is ignored always.
    demerphq committed Mar 17, 2023
    Configuration menu
    Copy the full SHA
    d0b1ad1 View commit details
    Browse the repository at this point in the history