åèã«ãããµã¤ãé
- perlxs - perldoc.perl.org
- perlxstut - perldoc.perl.org
- perlguts - perldoc.perl.org
- perlapi - perldoc.perl.org
- http://d.hatena.ne.jp/tokuhirom/20081209/1228829454
- Perl XS メモ - typemap と MAGIC をからめて - daily dayflower
- http://pub.ne.jp/wakapon/?entry_id=1109553
- http://pub.ne.jp/wakapon/?entry_id=1109664
å
- SV
- AV
- é å
- HV
- ããã·ã¥
- CV
- ã³ã¼ã
- GV
- ã°ãã
- RV
- ãªãã¡ã¬ã³ã¹
- IV
- æ´æ°
- NV
- å®æ°
- PV
- æåå
PerlIO_printf 㨠printf
use MyXS; binmode STDOUT, ":encoding(cp932)"; # Windows ã®ã¨ã³ã³ã¼ãã£ã³ã°ãæå® MyXS::hoge();
MODULE = MyXS PACKAGE = MyXS void hoge() CODE: PerlIO_printf(PerlIO_stdout(), "ã\n"); // Windows ã§åããªã fprintf(stdout, "ã\n"); // Windows ã§åãã
typemap
lib/ExtUtils/typemap ã«ããã©ã«ãã® typemap ãããã®ã§ã int ã¨ãè¿ãã¦ããï½ãåºæ¬çã« typemap ããªãã¨å¤æã§ããªãã
ãã¨ãã°ãlib/ExtUtils/typemap ã«ä»¥ä¸ã®ããã«æ¸ãã¦ãããã
bool T_BOOL INPUT T_BOOL $var = (bool)SvTRUE($arg) OUTPUT T_BOOL $arg = boolSV($var);
以ä¸ã®ãããªã³ã¼ããæ¸ããã¨ãã«
bool hello(b) bool b CODE: RETVAL = b; OUTPUT: RETVAL
以ä¸ã®ãã㪠C ãçæããã¦
XS(XS_Mytest_hello); /* prototype to pass -Wmissing-prototypes */ XS(XS_Mytest_hello) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif if (items != 1) Perl_croak(aTHX_ "Usage: %s(%s)", "Mytest::hello", "b"); PERL_UNUSED_VAR(cv); /* -W */ { bool b = (bool)SvTRUE(ST(0)); // typemap ã® INPUT ã®å®ç¾©ã§ãããä½ããã bool RETVAL; #line 14 "Mytest.xs" RETVAL = b; #line 40 "Mytest.c" ST(0) = boolSV(RETVAL); // ãã㯠OUTPUT ã®å®ç¾© sv_2mortal(ST(0)); } XSRETURN(1); }
èªå㧠Perl ã®åã«å¤æãããã
ãªãã¡ã¬ã³ã¹ã«ã¦ã³ã¿
int get_count(sv) SV* sv CODE: RETVAL = SvREFCNT(sv); OUTPUT: RETVAL
my $a = ""; my $b = \$a; my $c = $b; print Mytest::get_count($a) . "\n"; # => 3
SvREFCNT_inc
SvREFCNT_dec
mortal
mortal ã«ãã¨ãã°ãªãã¡ã¬ã³ã¹ã«ã¦ã³ã¿ã 0 ã«ãªã£ããèªå㧠GC ãã¦ããã
newSV** ãããã¨ãã¯ã sv_2mortal ãã»ããã§å¼ã³åºãããã«ãã¨ãã°ããã
sv_2mortal(newSVnv(0.1));
ã§ããè¿ãå¤ã¯ã
SV* hello() CODE: RETVAL = newSVnv(0.1); OUTPUT: RETVAL
ã¨ã mortal ã«ããªãã¦ã大ä¸å¤«ã
C ã«ãªã£ãã¨ãã«ä»¥ä¸ã®ããã« sv_2mortal ãå¼ã°ãããã
XS(XS_Mytest_hello); /* prototype to pass -Wmissing-prototypes */ XS(XS_Mytest_hello) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif if (items != 0) Perl_croak(aTHX_ "Usage: %s(%s)", "Mytest::hello", ""); PERL_UNUSED_VAR(cv); /* -W */ { SV * RETVAL; #line 13 "Mytest.xs" RETVAL = newSVnv(0.1); #line 39 "Mytest.c" ST(0) = RETVAL; sv_2mortal(ST(0)); // <- ããã§åæã«ã¢ã¼ã¿ã«å } XSRETURN(1); }
ã·ã³ãã«ãã¼ãã«ããã·ã¥
gv_stashsv ã¨ã gv_stashpv ã使ãã
HV* get_stash(sv) SV* sv CODE: // 第äºå¼æ°ã¯ãã·ã³ãã«ãã¼ãã«ããªãã£ãã空ã®ã·ã³ãã«ãã¼ãã«ãä½ããã©ããã RETVAL = gv_stashsv(sv, true); OUTPUT: RETVAL
ã¯ã©ã¹åãç¥ã
SvSTASH ã§ã·ã³ãã«ãã¼ãã«ããã·ã¥ãåå¾ãã·ã³ãã«ãã¼ãã«ããã·ã¥ã®ååãåå¾
char* get_blessed_name(sv) SV* sv CODE: RETVAL = HvNAME(SvSTASH(SvRV(sv))); OUTPUT: RETVAL
Devel::Peek 便å©
use Devel::Peek "Dump"; print Dump(MyXS::hoge()) . "\n";
ã¨ãæ¸ãã¦ããã°ããªãã¡ã¬ã³ã¹ã«ã¦ã³ã¿ã®å¤ã¨ãè¦ãã¦è¶ 便å©ã
è¤æ°ã®å¤ï¼ãªã¹ãï¼ãè¿ãã¨ã
PPCODE ã使ã
void get_child_window(h, type) HWND h; int type; PPCODE: std::vector<HWND> v; EnumChildWindows(h, reinterpret_cast<WNDENUMPROC>(addhwnd), reinterpret_cast<LPARAM>(&v)); for (std::vector<HWND>::iterator i = v.begin(); i != v.end(); ++i) XPUSHs(sv_2mortal(newSViv(reinterpret_cast<IV>(*i)))); // XPUSHsãã¡ããã¨èªå㧠mortal åãã
UTF8 ãã©ã°ã on ã«ãã¦æååãè¿ã
SV* get_text() CODE: RETVAL = newSVpvn("ã»ãã»ã", sizeof("ã»ãã»ã") - 1); SvUTF8_on( RETVAL); OUTPUT: RETVAL
ï¼ã¤ã¥ãããï¼