2

当用户XSmain::包中调用时,我们不能使用

caller_cx(0, NULL);

因为没有适用于main::和适用于XSUB DOC的框架

请注意,XSUB 不获取堆栈帧,因此 C 将返回紧邻 Perl 代码的信息

如何获取被调用的file:line信息、范围提示等信息?XSUBmain::

4

1 回答 1

4

复制自mess_sv(由 Perl API 函数调用warncroak,附加行信息,如 Perl 函数warndie):

use strict;
use warnings;
use feature qw( say );

use Inline C => <<'__EOS__';

void testing() {
    dXSARGS;

    /*
     * Try and find the file and line for PL_op.  This will usually be
     * PL_curcop, but it might be a cop that has been optimised away.  We
     * can try to find such a cop by searching through the optree star ting
     * from the sibling of PL_curcop.
     */
    if (PL_curcop) {
        const COP *cop =
            Perl_closest_cop(aTHX_ PL_curcop, OpSIBLING(PL_curcop), PL_op, FALSE);
        if (!cop)
            cop = PL_curcop;

        if (CopLINE(cop)) {
            EXTEND(SP, 2);
            mPUSHs(newSVpv(OutCopFILE(cop), 0));
            mPUSHs(newSViv((IV)CopLINE(cop)));
            XSRETURN(2);
        }
    }

    XSRETURN(0);
}

__EOS__

say join ":", testing();

稍微介绍一下PL_curcop 这里

于 2018-01-17T20:00:35.903 回答