如何从XS访问当前上下文?

问题描述 投票:1回答:1

当用户从XS包中调用main::时,我们无法使用

caller_cx(0, NULL);

因为main::XSUB DOC没有框架

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

如何获取file:line信息XSUB被调用,提示main::范围等信息?

perl perl-xs perlapi
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 here的一点点。

© www.soinside.com 2019 - 2024. All rights reserved.