/
perlxs.pod
4655 lines (3333 loc) · 141 KB
/
perlxs.pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=encoding euc-jp
=head1 NAME
=begin original
perlxs - XS language reference manual
=end original
perlxs - XS 言語リファレンスマニュアル
=head1 DESCRIPTION
=head2 Introduction
(序論)
=begin original
XS is an interface description file format used to create an extension
interface between Perl and C code (or a C library) which one wishes
to use with Perl. The XS interface is combined with the library to
create a new library which can then be either dynamically loaded
or statically linked into perl. The XS interface description is
written in the XS language and is the core component of the Perl
extension interface.
=end original
XS は Perl と(Perl と一緒に使いたい)C のコード(または C ライブラリ)との
間の拡張インターフェースを作るのに使われるインターフェース記述
ファイルフォーマットです。
XS インターフェースはライブラリと動的または静的にリンクされて、
Perl とリンクすることのできる新しいライブラリを生成します。
XS インターフェース記述はは XS 言語で書かれており、
Perl 拡張インターフェースのコアコンポーネントです。
=begin original
Before writing XS, read the L</CAVEATS> section below.
=end original
XS を書く前に、後述する L</CAVEATS> 節を読んでください。
=begin original
An B<XSUB> forms the basic unit of the XS interface. After compilation
by the B<xsubpp> compiler, each XSUB amounts to a C function definition
which will provide the glue between Perl calling conventions and C
calling conventions.
=end original
B<XSUB> は XS インターフェースの基本単位を形成します。
B<xsubpp> コンパイラによるコンパイル後、
それぞれの XSUB は Perl 呼び出し規則と C 呼び出し規則の間の糊を提供する
C 関数定義となります。
=begin original
The glue code pulls the arguments from the Perl stack, converts these
Perl values to the formats expected by a C function, call this C function,
transfers the return values of the C function back to Perl.
Return values here may be a conventional C return value or any C
function arguments that may serve as output parameters. These return
values may be passed back to Perl either by putting them on the
Perl stack, or by modifying the arguments supplied from the Perl side.
=end original
糊のコードは Perl スタックから引数を取り出し、Perl の値を C 関数が
想定している形式に変換し、C 関数を呼び出し、C 関数の返り値を Perl に
返します。
ここでの返り値は、伝統的な C の返り値か、出力パラメータの役目をする
C 関数引数です。
これらの返り値は、Perl スタックに置かれるか、Perl 側から供給された
引数を変更することによって Perl に返されます。
=begin original
The above is a somewhat simplified view of what really happens. Since
Perl allows more flexible calling conventions than C, XSUBs may do much
more in practice, such as checking input parameters for validity,
throwing exceptions (or returning undef/empty list) if the return value
from the C function indicates failure, calling different C functions
based on numbers and types of the arguments, providing an object-oriented
interface, etc.
=end original
上記は実際に起きることをいくらか単純化したものです。
Perl は C よりもより柔軟な呼び出し規則を認めているので、
実際には XSUB は、検証のために入力をチェックする、もし C 関数からの
返り値が失敗を示していたら例外を投げる(あるいは undef/空リストを返す)、
引数の数と型によって異なった C 関数を呼び出す、オブジェクト指向
インターフェースを提供する、といった、遥かに多くのことができます。
=begin original
Of course, one could write such glue code directly in C. However, this
would be a tedious task, especially if one needs to write glue for
multiple C functions, and/or one is not familiar enough with the Perl
stack discipline and other such arcana. XS comes to the rescue here:
instead of writing this glue C code in long-hand, one can write
a more concise short-hand I<description> of what should be done by
the glue, and let the XS compiler B<xsubpp> handle the rest.
=end original
もちろん、このような糊のコードを直接 C で書くこともできます。
しかし、これはつまらない仕事です; 特に複数の C 関数に対する糊を書く
必要があったり、Perl スタックの分野やその奥義に親しくない場合はそうです。
XS はこれを助けます:
糊の C コードを長々と書く代わりに、糊にしてほしいことに関する、
もっと簡潔で短い I<記述> を書いて、XS コンパイラ B<xsubpp> に
残りを扱わせることができます。
=begin original
The XS language allows one to describe the mapping between how the C
routine is used, and how the corresponding Perl routine is used. It
also allows creation of Perl routines which are directly translated to
C code and which are not related to a pre-existing C function. In cases
when the C interface coincides with the Perl interface, the XSUB
declaration is almost identical to a declaration of a C function (in K&R
style). In such circumstances, there is another tool called C<h2xs>
that is able to translate an entire C header file into a corresponding
XS file that will provide glue to the functions/macros described in
the header file.
=end original
XS 言語は、どのように C ルーチンが使うのかと、どのように対応する Perl
ルーチンが使うのかとのマッピングを記述できます。
これはまた、直接 C コードに変換され、既に存在する C 関数と関係ない
Perl ルーチンの作成も可能にします。
C インターフェースが Perl インターフェースと同期している場合、
XSUB 宣言はほとんど (K&R 形式の) C 関数の宣言と同じです。
このような事情から、C ヘッダ全体から、ヘッダファイルに記述されている
関数/マクロへの糊を提供する XS ファイルへ変換する、C<h2xs> と呼ばれる
もう一つのツールがあります。
=begin original
The XS compiler is called B<xsubpp>. This compiler creates
the constructs necessary to let an XSUB manipulate Perl values, and
creates the glue necessary to let Perl call the XSUB. The compiler
uses B<typemaps> to determine how to map C function parameters
and output values to Perl values and back. The default typemap
(which comes with Perl) handles many common C types. A supplementary
typemap may also be needed to handle any special structures and types
for the library being linked. For more information on typemaps,
see L<perlxstypemap>.
=end original
XS コンパイラは B<xsubpp> と呼ばれます。
このコンパイラは XSUB が Perl の値を扱うための構造や、
Perl が XSUB を呼び出すために必要なものを作成します。
このコンパイラは B<typemaps> を使って C の関数の引数とと出力値を
Perlの値とどのようにマッピングするかを決定します。
デフォルトの typemap (Perl に同梱しているもの)は多くの標準的な C の型を
扱います。
リンクされるライブラリのための特殊な構造体や型を扱えるようするための
補助的な typemap も必要になるかもしれません。
typemaps に関するさらなる情報については、L<perlxstypemap> を
参照してください。
=begin original
A file in XS format starts with a C language section which goes until the
first C<MODULE =Z<>> directive. Other XS directives and XSUB definitions
may follow this line. The "language" used in this part of the file
is usually referred to as the XS language. B<xsubpp> recognizes and
skips POD (see L<perlpod>) in both the C and XS language sections, which
allows the XS file to contain embedded documentation.
=end original
XS 形式のファイルは、最初の C<MODULE =Z<>> 指示子が現れるまで C 言語
セクションから開始します。
その他の XS 指示子と XSUB 定義はこの行に続きます。
ファイルのこの部分で使われる「言語」は普通 XS 言語として参照されます。
B<xsubpp> は C と XS の言語セクションで POD (L<perlpod> を
参照してください)を認識して呼び飛ばすので、XS ファイルに組み込み
ドキュメントを含めることができます。
=begin original
See L<perlxstut> for a tutorial on the whole extension creation process.
=end original
エクステンションの作成手順についてのチュートリアルは L<perlxstut> を
参照してください。
=begin original
Note: For some extensions, Dave Beazley's SWIG system may provide a
significantly more convenient mechanism for creating the extension
glue code. See L<http://www.swig.org/> for more information.
=end original
注意: エクステンションによっては、エクステンションの糊コードの作成には
Dave Beazley の SWIG システムがはるかに便利な機構を提供するでしょう。
さらなる情報については L<http://www.swig.org/> を参照してください。
=head2 On The Road
=begin original
Many of the examples which follow will concentrate on creating an interface
between Perl and the ONC+ RPC bind library functions. The rpcb_gettime()
function is used to demonstrate many features of the XS language. This
function has two parameters; the first is an input parameter and the second
is an output parameter. The function also returns a status value.
=end original
この後で出てくる例の多くは、Perl と ONC+ RPC bind ライブラリ関数との間の
インターフェースを具体的に生成します。
rpcb_gettime() という関数が、XS 言語の多くの機能を説明するために使われます。
この関数は二つの引数を取ります;
最初のものは入力パラメータで、二番目のものは出力パラメータです。
関数はステータス値も返します。
bool_t rpcb_gettime(const char *host, time_t *timep);
=begin original
From C this function will be called with the following
statements.
=end original
この関数は C では、次のように呼ばれます。
#include <rpc/rpc.h>
bool_t status;
time_t timep;
status = rpcb_gettime( "localhost", &timep );
=begin original
If an XSUB is created to offer a direct translation between this function
and Perl, then this XSUB will be used from Perl with the following code.
The $status and $timep variables will contain the output of the function.
=end original
XSUB がこの関数と perl との間の直接的な変換をするように作られていれば、
この XSUB は Perl から以下のようにして使われます。
$status と $timep という変数は関数の出力を保持します。
use RPC;
$status = rpcb_gettime( "localhost", $timep );
=begin original
The following XS file shows an XS subroutine, or XSUB, which
demonstrates one possible interface to the rpcb_gettime()
function. This XSUB represents a direct translation between
C and Perl and so preserves the interface even from Perl.
This XSUB will be invoked from Perl with the usage shown
above. Note that the first three #include statements, for
C<EXTERN.h>, C<perl.h>, and C<XSUB.h>, will always be present at the
beginning of an XS file. This approach and others will be
expanded later in this document. A #define for C<PERL_NO_GET_CONTEXT>
should be present to fetch the interpreter context more efficiently,
see L<perlguts|perlguts/How multiple interpreters and concurrency are
supported> for details.
=end original
以下に示す XS ファイルは XS サブルーチン、もしくは rpcb_gettime() 関数に
対して可能なインターフェースの一つを例示する XSUB の例です。
この XSUB は C と Perl との間の直接的な変換を表わし、また、Perl からも
インターフェースを保護します。
この XSUB は Perl から、先程の例のようにして呼び出されます。
最初の三つの #include 文、C<EXTERN.h>, C<perl.h>, C<XSUB.h> は常に
XS ファイルの先頭に置かれることに注意してください。
このアプローチなどはこのドキュメントの後のほうで説明します。
A #define for C<PERL_NO_GET_CONTEXT>
should be present to fetch the interpreter context more efficiently;
詳しくは L<perlguts|perlguts/How multiple interpreters and concurrency are
supported> を参照してください。
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <rpc/rpc.h>
MODULE = RPC PACKAGE = RPC
bool_t
rpcb_gettime(host,timep)
char *host
time_t &timep
OUTPUT:
timep
=begin original
Any extension to Perl, including those containing XSUBs,
should have a Perl module to serve as the bootstrap which
pulls the extension into Perl. This module will export the
extension's functions and variables to the Perl program and
will cause the extension's XSUBs to be linked into Perl.
The following module will be used for most of the examples
in this document and should be used from Perl with the C<use>
command as shown earlier. Perl modules are explained in
more detail later in this document.
=end original
Perl に対する全てのエクステンションは、このような XSUB も含めて、
Perl にエクステンションを押し込むブートストラップとして働く
Perl モジュールを持つべきです。
このモジュールはエクステンションの関数と変数とをエクスポートし、
Perl プログラムや Perl にリンクされるエクステンションの XSUB を起動します。
以下のモジュールはこのドキュメントのほとんどの例で使われるもので、
(先に挙げた例のように) Perl から C<use> コマンドを使うべきものです。
Perl のモジュールはこのドキュメントの後のほうでより詳しく説明されます。
package RPC;
require Exporter;
require DynaLoader;
@ISA = qw(Exporter DynaLoader);
@EXPORT = qw( rpcb_gettime );
bootstrap RPC;
1;
=begin original
Throughout this document a variety of interfaces to the rpcb_gettime()
XSUB will be explored. The XSUBs will take their parameters in different
orders or will take different numbers of parameters. In each case the
XSUB is an abstraction between Perl and the real C rpcb_gettime()
function, and the XSUB must always ensure that the real rpcb_gettime()
function is called with the correct parameters. This abstraction will
allow the programmer to create a more Perl-like interface to the C
function.
=end original
このドキュメントを通して、rpcb_gettime() XSUB に対する様々な
インターフェースが検討されます。
XSUB はパラメータを違った順番で受け取ったり、異なる個数のパラメータを
とったりします。
それぞれの場合において XSUB は Perl と実際の C の rpcb_gettime 関数の間の
抽象作用(abstraction)であり、かつ、XSUB は常に実際の rpcb_gettime() が
正しいパラメータで呼ばれることを保証しなければなりません。
この抽象作用はプログラマが C に対するより Perl 的なインターフェースを
作成することを許します。
=head2 The Anatomy of an XSUB
(XSUB の構造)
=begin original
The simplest XSUBs consist of 3 parts: a description of the return
value, the name of the XSUB routine and the names of its arguments,
and a description of types or formats of the arguments.
=end original
もっとも単純な XSUB は 3 つの部分からなります: 返り値の記述、XSUB
ルーチンの名前とその引数の名前、引数の型や形式の記述です。
=begin original
The following XSUB allows a Perl program to access a C library function
called sin(). The XSUB will imitate the C function which takes a single
argument and returns a single value.
=end original
以下に示す XSUB は、Perl プログラムに対して sin() と呼ばれる C の
ライブラリ関数にアクセスすることを許します。
この XSUB は引数を一つ取り、一つの値を返す C の関数を模倣します。
double
sin(x)
double x
=begin original
Optionally, one can merge the description of types and the list of
argument names, rewriting this as
=end original
オプションとして、型の記述と引数の名前のリストを結合できます;
これを以下のように書き換えます:
double
sin(double x)
=begin original
This makes this XSUB look similar to an ANSI C declaration. An optional
semicolon is allowed after the argument list, as in
=end original
これによって XSUB を ANSI C 宣言と似せて見せます。
以下のように、引数の後にオプションのセミコロンも付けられます:
double
sin(double x);
=begin original
Parameters with C pointer types can have different semantic: C functions
with similar declarations
=end original
C ポインタ型の引数は異なった意味論を持ちます: 同じような定義の
C 関数:
bool string_looks_as_a_number(char *s);
bool make_char_uppercase(char *c);
=begin original
are used in absolutely incompatible manner. Parameters to these functions
could be described B<xsubpp> like this:
=end original
は完全に互換性のない方式です。
これらの関数への引数は以下のように B<xsubpp> が記述します:
char * s
char &c
=begin original
Both these XS declarations correspond to the C<char*> C type, but they have
different semantics, see L<"The & Unary Operator">.
=end original
これらの XS 宣言の両方とも C の C<char*> 型に対応しますが、異なる
動作をします; L<"The & Unary Operator"> を参照してください。
=begin original
It is convenient to think that the indirection operator
C<*> should be considered as a part of the type and the address operator C<&>
should be considered part of the variable. See L<perlxstypemap>
for more info about handling qualifiers and unary operators in C types.
=end original
間接参照演算子 C<*> は型の一部とみなすべきで、アドレス演算子 C<&> は
変数の一部であるとみなすと便利です。
C の型における単項演算子や修飾子(qualifiers)の扱いに関する詳細は
L<perlxstypemap> を参照してください。
=begin original
The function name and the return type must be placed on
separate lines and should be flush left-adjusted.
=end original
関数名と、その返り値の型は別々の行に分けておかなければなりません;
また左揃えにするべきです。
=begin original
INCORRECT CORRECT
=end original
間違い 正しい
double sin(x) double
double x sin(x)
double x
=begin original
The rest of the function description may be indented or left-adjusted. The
following example shows a function with its body left-adjusted. Most
examples in this document will indent the body for better readability.
=end original
残りの関数記述はインデントを付けたり、左寄せすることができます。
以下の例では関数の本体を左寄せしています。
本ドキュメントにあるほとんどの例では読みやすいように本体にインデントを
付けています。
CORRECT
double
sin(x)
double x
=begin original
More complicated XSUBs may contain many other sections. Each section of
an XSUB starts with the corresponding keyword, such as INIT: or CLEANUP:.
However, the first two lines of an XSUB always contain the same data:
descriptions of the return type and the names of the function and its
parameters. Whatever immediately follows these is considered to be
an INPUT: section unless explicitly marked with another keyword.
(See L<The INPUT: Keyword>.)
=end original
もっと複雑な XSUB は多くのその他のセクションを含んでいるかもしれません。
XSUB のそれぞれのセクションは、INIT: や CLEANUP: のような、対応する
キーワードで始まります。
しかし、XSUB の最初の 2 行はいつも同じデータからなります:
返り値の種類の記述と、関数およびその引数の名前です。
これらに引き続くものはなんでも、明示的にその他のキーワードがない限りは
INPUT: セクションと考えられます。
(L<The INPUT: Keyword> を参照してください。)
=begin original
An XSUB section continues until another section-start keyword is found.
=end original
XSUB セクションは他のセクション開始キーワードが現れるまで続きます。
=head2 The Argument Stack
(引数スタック)
=begin original
The Perl argument stack is used to store the values which are
sent as parameters to the XSUB and to store the XSUB's
return value(s). In reality all Perl functions (including non-XSUB
ones) keep their values on this stack all the same time, each limited
to its own range of positions on the stack. In this document the
first position on that stack which belongs to the active
function will be referred to as position 0 for that function.
=end original
Perl の引数スタックは XSUB にパラメータとして渡される値や XSUB から返って
きた値を格納するのに使われます。
すべての(非 XSUB 関数を含む)Perl 関数は、その値をこのスタックに全て同時に
保持していて、それぞれこのスタック上の位置の範囲によって制限されます。
このドキュメントでは、アクティブな関数に所属するスタックの最初の位置は
その関数からは位置 0として参照されます。
=begin original
XSUBs refer to their stack arguments with the macro B<ST(x)>, where I<x>
refers to a position in this XSUB's part of the stack. Position 0 for that
function would be known to the XSUB as ST(0). The XSUB's incoming
parameters and outgoing return values always begin at ST(0). For many
simple cases the B<xsubpp> compiler will generate the code necessary to
handle the argument stack by embedding code fragments found in the
typemaps. In more complex cases the programmer must supply the code.
=end original
XSUB はそのスタックにある引数に対して B<ST(x)> というマクロを使って、
I<x> が示している XSUB のスタック上にある位置の参照を行います。
関数に対する位置 0 は ST(0) となります。
XSUB に対するパラメータと XSUB から返される値は常に ST(0) から始まります。
多くの単純な場合においては、B<xsubpp>コンパイラは typemap にある
埋め込みコード片(embedding code fragments)から引数スタックを扱う
コードを生成します。
もっと複雑な場合には、プログラマがコードを提供しなければなりません。
=head2 The RETVAL Variable
(RETVAL 変数)
=begin original
The RETVAL variable is a special C variable that is declared automatically
for you. The C type of RETVAL matches the return type of the C library
function. The B<xsubpp> compiler will declare this variable in each XSUB
with non-C<void> return type. By default the generated C function
will use RETVAL to hold the return value of the C library function being
called. In simple cases the value of RETVAL will be placed in ST(0) of
the argument stack where it can be received by Perl as the return value
of the XSUB.
=end original
RETVAL という変数は自動的に宣言される特別な C 変数です。
RETVAL の C での型は C のライブラリ関数が返す型に一致します。
B<xsubpp> コンパイラは各 XSUB の非 C<void> 返り値に対してこの変数を
宣言します。
デフォルトでは生成された C 関数は RETVAL を C ライブラリ関数が呼ばれたときの
返り値の型を保持するのに使われます。
単純な場合には、RETVAL の値は Perl から XSUB の返り値として
受け取ることのできる引数スタックの ST(0) に置かれます。
=begin original
If the XSUB has a return type of C<void> then the compiler will
not declare a RETVAL variable for that function. When using
a PPCODE: section no manipulation of the RETVAL variable is required, the
section may use direct stack manipulation to place output values on the stack.
=end original
XSUB の返り値の方が C<void> であった場合には、コンパイラは
その関数に対して変数 RETVAL を宣言しません。
PPCODE: セクションをを使う場合、RETVAL 変数の操作を操作する必要がないので、
このセクションはスタックに出力値を置くためにスタックを直接操作します。
=begin original
If PPCODE: directive is not used, C<void> return value should be used
only for subroutines which do not return a value, I<even if> CODE:
directive is used which sets ST(0) explicitly.
=end original
PPCODE: 指示子を使わないのであれば、C<void> な返り値は
CODE: 指示子が ST(0) へ陽にセットするのに使われていたとしても、
値を返さないサブルーチンに対してのみ使うべきです。
=begin original
Older versions of this document recommended to use C<void> return
value in such cases. It was discovered that this could lead to
segfaults in cases when XSUB was I<truly> C<void>. This practice is
now deprecated, and may be not supported at some future version. Use
the return value C<SV *> in such cases. (Currently C<xsubpp> contains
some heuristic code which tries to disambiguate between "truly-void"
and "old-practice-declared-as-void" functions. Hence your code is at
mercy of this heuristics unless you use C<SV *> as return value.)
=end original
このドキュメントの以前のものでは、そういった場合に返り値として C<void> を
使うことを勧めていました。
これは XSUB が I<本当に> C<void> であるときにセグメンテーション違反を起こす
可能性があることが確認されました。
このやり方は今では使わないことが推奨されていて、将来のバージョンで
サポートされなくなるでしょう。
こういった場合、返り値 C<SV *> を使います(現在 C<xsubpp> は
"truely-void" な関数と"old-practice-declared-as-void" な関数とを明確に
しようとする経験則的なコードが入っています。
このためあなたのプログラムはあなたが返り値として C<SV *> を使わない限り、
この経験則の振る舞いに左右されます。)
=head2 Returning SVs, AVs and HVs through RETVAL
(RETVAL で SV, AV, HV を返す)
=begin original
When you're using RETVAL to return an C<SV *>, there's some magic
going on behind the scenes that should be mentioned. When you're
manipulating the argument stack using the ST(x) macro, for example,
you usually have to pay special attention to reference counts. (For
more about reference counts, see L<perlguts>.) To make your life
easier, the typemap file automatically makes C<RETVAL> mortal when
you're returning an C<SV *>. Thus, the following two XSUBs are more
or less equivalent:
=end original
C<SV *> を返すために RETVAL を使うとき、言及しておくべき魔法が舞台裏で
行われます。
例えば、ST(x) マクロを使って引数スタックを操作するとき、普通は
参照カウントに特別な注意を払う必要があります。
(参照カウントに関しては、L<perlguts> を参照してください。)
人生をより簡単にするために、C<SV *> を返そうとしているときは
typemap ファイルは自動的に C<RETVAL> を揮発性にします。
従って、以下の 2 つの XSUB はだいたい等価です:
void
alpha()
PPCODE:
ST(0) = newSVpv("Hello World",0);
sv_2mortal(ST(0));
XSRETURN(1);
SV *
beta()
CODE:
RETVAL = newSVpv("Hello World",0);
OUTPUT:
RETVAL
=begin original
This is quite useful as it usually improves readability. While
this works fine for an C<SV *>, it's unfortunately not as easy
to have C<AV *> or C<HV *> as a return value. You I<should> be
able to write:
=end original
これは普通可読性を改善するのでかなり有用です。
これは C<SV *> ではうまく動作する一方、残念ながら返り値として
C<AV *> や C<HV *> を使うときには簡単ではありません。
以下のように書ける I<べき> です:
AV *
array()
CODE:
RETVAL = newAV();
/* do something with RETVAL */
OUTPUT:
RETVAL
=begin original
But due to an unfixable bug (fixing it would break lots of existing
CPAN modules) in the typemap file, the reference count of the C<AV *>
is not properly decremented. Thus, the above XSUB would leak memory
whenever it is being called. The same problem exists for C<HV *>,
C<CV *>, and C<SVREF> (which indicates a scalar reference, not
a general C<SV *>).
In XS code on perls starting with perl 5.16, you can override the
typemaps for any of these types with a version that has proper
handling of refcounts. In your C<TYPEMAP> section, do
=end original
しかし typemap ファイルにある修正されていないバグのために(これを
修正すると既存の多くの CPANモジュールが動かなくなります)、
C<AV *> の参照カウントは適切にデクリメントされません。
従って、上述の XSUB は、呼び出されるごとにメモリリークします。
同じ問題は C<HV *>, C<CV *>, (一般的な C<SV *> ではなく
スカラリファレンスを示す) C<SVREF> にもあります。
perl 5.16 以降の perl の XS コードでは、これらの型の typemap を、
適切に参照カウントを扱える版に上書きできます。
C<TYPEMAP> セクションで、
AV* T_AVREF_REFCOUNT_FIXED
=begin original
to get the repaired variant. For backward compatibility with older
versions of perl, you can instead decrement the reference count
manually when you're returning one of the aforementioned
types using C<sv_2mortal>:
=end original
とすると修正版を得られます。
古いバージョンの perl との後方互換性のために、C<sv_2mortal> を使って
前述の型の一つを返すとき、代わりに参照カウントを手動でデクリメントすることも
できます:
AV *
array()
CODE:
RETVAL = newAV();
sv_2mortal((SV*)RETVAL);
/* do something with RETVAL */
OUTPUT:
RETVAL
=begin original
Remember that you don't have to do this for an C<SV *>. The reference
documentation for all core typemaps can be found in L<perlxstypemap>.
=end original
C<SV *> のためにはこれをする必要はないことも覚えておいてください。
全てのコア typemap に関するリファレンス文書は L<perlxstypemap> です。
=head2 The MODULE Keyword
(MODULE キーワード)
=begin original
The MODULE keyword is used to start the XS code and to specify the package
of the functions which are being defined. All text preceding the first
MODULE keyword is considered C code and is passed through to the output with
POD stripped, but otherwise untouched. Every XS module will have a
bootstrap function which is used to hook the XSUBs into Perl. The package
name of this bootstrap function will match the value of the last MODULE
statement in the XS source files. The value of MODULE should always remain
constant within the same XS file, though this is not required.
=end original
キーワード MODULE は XS コードの始まりと、定義する関数のパッケージを
指定するのに使われます。
キーワード MODULE よりも前にあるテキストはCプログラムとして扱われ、
POD が除去されますが、それ以外は何の変更も加えられずに出力されます。
すべての XS モジュールは、XSUB を Perl にフックするのに使われる
ブートストラップ関数を持ちます。
このブートストラップ関数のパッケージ名は、XS のソースファイルにある
最後の MODULE 文の値に一致します。
MODULE の値は同じ XS ファイルの中では変更されないようにすべきです
(が、必須ではありません)。
=begin original
The following example will start the XS code and will place
all functions in a package named RPC.
=end original
以下の例は XS コードを開始し、すべての関数を RPC という名前のパッケージに
置きます。
MODULE = RPC
=head2 The PACKAGE Keyword
(PACKAGE キーワード)
=begin original
When functions within an XS source file must be separated into packages
the PACKAGE keyword should be used. This keyword is used with the MODULE
keyword and must follow immediately after it when used.
=end original
XS ソースファイルの関数をパッケージに分割しなければならないとき、
キーワード PACKAGE を使うべきです。
このキーワードは MODULE キーワードと共に使われ、かつ、そのすぐ後に
なければなりません。
MODULE = RPC PACKAGE = RPC
[ XS code in package RPC ]
MODULE = RPC PACKAGE = RPCB
[ XS code in package RPCB ]
MODULE = RPC PACKAGE = RPC
[ XS code in package RPC ]
=begin original
The same package name can be used more than once, allowing for
non-contiguous code. This is useful if you have a stronger ordering
principle than package names.
=end original
同じパッケージ名を複数回使えるので、連続していないコードも可能です。
これは、パッケージ名よりより強い順序原則を持っている場合に有用です。
=begin original
Although this keyword is optional and in some cases provides redundant
information it should always be used. This keyword will ensure that the
XSUBs appear in the desired package.
=end original
このキーワードは省略可能であって、また、一部のケースにおいては
重複する情報となるにもかかわらず、常に使うべきものです。
このキーワードは XSUB が要求したパッケージにあることを保証します。
=head2 The PREFIX Keyword
(PREFIX キーワード)
=begin original
The PREFIX keyword designates prefixes which should be
removed from the Perl function names. If the C function is
C<rpcb_gettime()> and the PREFIX value is C<rpcb_> then Perl will
see this function as C<gettime()>.
=end original
キーワード PREFIX は Perl の関数名から取り除かれるべきプリフィクスを
指定するものです。
C 関数が C<rpcb_gettime()> で、その PREFIX の値が C<rpcb_> であったとすると、
Perl はこの関数を C<gettime()> として見ます。
=begin original
This keyword should follow the PACKAGE keyword when used.
If PACKAGE is not used then PREFIX should follow the MODULE
keyword.
=end original
このキーワードはキーワード PACKAGE を使った後にあるべきです。
PACKAGE が使われなければ、PREFIX はキーワード MODULE の後にあるべきです。
MODULE = RPC PREFIX = rpc_
MODULE = RPC PACKAGE = RPCB PREFIX = rpcb_
=head2 The OUTPUT: Keyword
(OUTPUT: キーワード)
=begin original
The OUTPUT: keyword indicates that certain function parameters should be
updated (new values made visible to Perl) when the XSUB terminates or that
certain values should be returned to the calling Perl function. For
simple functions which have no CODE: or PPCODE: section,
such as the sin() function above, the RETVAL variable is
automatically designated as an output value. For more complex functions
the B<xsubpp> compiler will need help to determine which variables are output
variables.
=end original
キーワード OUTPUT: は、幾つかの関数パラメータが XSUB が終了するときに
更新されるべき(Perl のために新しい値を見えるようにする)ものであることや、
幾つかの値が呼び出し元の Perl 関数に戻されるべきものであることを示します。
前述の sin() のような CODE: や PPCODE: セクションのない単純な関数には、変数
RETVALは自動的に出力値に割り当てられます。
もっと複雑な関数では、B<xsubpp> コンパイラはどの変数が 出力変数であるかを
決めるための助けが必要です。
=begin original
This keyword will normally be used to complement the CODE: keyword.
The RETVAL variable is not recognized as an output variable when the
CODE: keyword is present. The OUTPUT: keyword is used in this
situation to tell the compiler that RETVAL really is an output
variable.
=end original
このキーワードは、通常 キーワード CODE: を完全にするために使われます。
変数 RETVALは、キーワード CODE: が使われている場合には出力変数としては
認識されません。
キーワード OUTPUT はこういった場合にコンパイラに RETVAL が本当に
出力変数であることを教えるために使われます。
=begin original
The OUTPUT: keyword can also be used to indicate that function parameters
are output variables. This may be necessary when a parameter has been
modified within the function and the programmer would like the update to
be seen by Perl.
=end original
キーワード OUTPUT: はまた、関数のパラメータが出力変数であるということを
示すのにも使えます。
これは関数の中でパラメータが変更されたり、プログラマが更新したことを
Perl に教えたいといったときに必要になります。
bool_t
rpcb_gettime(host,timep)
char *host
time_t &timep
OUTPUT:
timep
=begin original
The OUTPUT: keyword will also allow an output parameter to
be mapped to a matching piece of code rather than to a
typemap.
=end original
キーワード OUTPUT: はまた、出力パラメータを(typemap ではなく)
それにマッチするコード片にマッピングするのにも使えます。
bool_t
rpcb_gettime(host,timep)
char *host
time_t &timep
OUTPUT:
timep sv_setnv(ST(1), (double)timep);
=begin original
B<xsubpp> emits an automatic C<SvSETMAGIC()> for all parameters in the
OUTPUT section of the XSUB, except RETVAL. This is the usually desired
behavior, as it takes care of properly invoking 'set' magic on output
parameters (needed for hash or array element parameters that must be
created if they didn't exist). If for some reason, this behavior is
not desired, the OUTPUT section may contain a C<SETMAGIC: DISABLE> line
to disable it for the remainder of the parameters in the OUTPUT section.
Likewise, C<SETMAGIC: ENABLE> can be used to reenable it for the
remainder of the OUTPUT section. See L<perlguts> for more details
about 'set' magic.
=end original