如何从 XS 访问当前上下文?
How to get access to current context from XS?
当用户从 main::
包调用 XS
时,我们无法使用
caller_cx(0, NULL);
因为 main::
和 XSUB
DOC
没有框架
Note that XSUBs don't get a stack frame, so C will return information for the immediately-surrounding Perl code
如何获取调用 XSUB
的 file:line
信息、main::
作用域的提示等信息?
从 mess_sv
复制(由 Perl API 函数 warn
和 croak
调用,它们附加行信息,如 Perl 函数 warn
和 die
):
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.
的一点点
当用户从 main::
包调用 XS
时,我们无法使用
caller_cx(0, NULL);
因为 main::
和 XSUB
DOC
Note that XSUBs don't get a stack frame, so C will return information for the immediately-surrounding Perl code
如何获取调用 XSUB
的 file:line
信息、main::
作用域的提示等信息?
从 mess_sv
复制(由 Perl API 函数 warn
和 croak
调用,它们附加行信息,如 Perl 函数 warn
和 die
):
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.