66<chapter xml : id =" ffi.examples" xmlns =" http://docbook.org/ns/docbook" xmlns : xlink =" http://www.w3.org/1999/xlink" >
77 &reftitle.examples;
88 <section xml : id =" ffi.examples-basic" >
9- <title >Basic FFI usage </title >
9+ <title >FFI の基本的な使い方 </title >
1010 <para >
11- Before diving into the details of the FFI API, lets take a look at a few examples
12- demonstrating the simplicity of the FFI API usage for regular tasks.
11+ FFI API の詳細に深く立ち入る前に、よくあるタスクに対する FFI API の使い方が
12+ どれほど簡単かを示す例をいくつか見てみましょう。
1313 </para >
1414 <note >
1515 <para >
16- Some of these examples require <filename >libc.so.6</filename > and as such will
17- not work on systems where it is not available.
16+ これらの例の中には、 <filename >libc.so.6</filename > を必要とするものがあります。
17+ それらは、このライブラリが利用できないシステムでは動きません。
1818 </para >
1919 </note >
2020 <para >
2121 <example xml : id =" ffi.examples.function" >
22- <title >Calling a function from shared library </title >
22+ <title >共有ライブラリの関数を呼ぶ </title >
2323 <programlisting role =" php" >
2424<![CDATA[
2525<?php
26- // create FFI object, loading libc and exporting function printf()
26+ // FFI オブジェクトを作成し、 libc を読み込んで printf() 関数をエクスポートする
2727$ffi = FFI::cdef(
28- "int printf(const char *format, ...);", // this is a regular C declaration
28+ "int printf(const char *format, ...);", // ここは通常の C の宣言
2929 "libc.so.6");
30- // call C's printf()
30+ // C の printf() を呼ぶ
3131$ffi->printf("Hello %s!\n", "world");
3232?>
3333]]>
@@ -42,17 +42,17 @@ Hello world!
4242 </para >
4343 <note >
4444 <para >
45- Note that some C functions need specific calling conventions, e.g. <literal >__fastcall</literal >,
46- <literal >__stdcall</literal > or <literal >,__vectorcall</literal >.
45+ C の関数のうちのいくつかは、特定の呼び出し規約 (例: <literal >__fastcall</literal >、
46+ <literal >__stdcall</literal >、 <literal >,__vectorcall</literal > など) を必要とすることに注意してください。
4747 </para >
4848 </note >
4949 <para >
5050 <example xml : id =" ffi.examples.structure" >
51- <title >Calling a function, returning a structure through an argument </title >
51+ <title >関数を呼び出し、構造体を引数経由で返す </title >
5252 <programlisting role =" php" >
5353<![CDATA[
5454<?php
55- // create gettimeofday() binding
55+ // gettimeofday() のバインディングを作成する
5656$ffi = FFI::cdef("
5757 typedef unsigned int time_t;
5858 typedef unsigned int suseconds_t;
@@ -69,14 +69,14 @@ $ffi = FFI::cdef("
6969
7070 int gettimeofday(struct timeval *tv, struct timezone *tz);
7171", "libc.so.6");
72- // create C data structures
72+ // C のデータ構造を作成する
7373$tv = $ffi->new("struct timeval");
7474$tz = $ffi->new("struct timezone");
75- // call C's gettimeofday()
75+ // C の gettimeofday() を呼ぶ
7676var_dump($ffi->gettimeofday(FFI::addr($tv), FFI::addr($tz)));
77- // access field of C data structure
77+ // C のデータ構造のフィールドにアクセスする
7878var_dump($tv->tv_sec);
79- // print the whole C data structure
79+ // C のデータ構造全体を出力する
8080var_dump($tz);
8181?>
8282]]>
@@ -98,7 +98,8 @@ object(FFI\CData:struct timezone)#3 (2) {
9898 </para >
9999 <para >
100100 <example xml : id =" ffi.examples.variable-existing" >
101- <title >Accessing existing C variables</title >
101+ <title >既存の C の変数にアクセスする</title >
102+ !!!
102103 <programlisting role =" php" >
103104<![CDATA[
104105<?php
@@ -121,7 +122,8 @@ int(0)
121122 </para >
122123 <para >
123124 <example xml : id =" ffi.examples.variable-creating" >
124- <title >Creating and Modifying C variables</title >
125+ <title >C の変数を作成して書き換える</title >
126+ !!!
125127 <programlisting role =" php" >
126128<![CDATA[
127129<?php
@@ -151,7 +153,8 @@ int(7)
151153 </para >
152154 <para >
153155 <example xml : id =" ffi.examples.array" >
154- <title >Working with C arrays</title >
156+ <title >C の配列を扱う</title >
157+ !!!
155158 <programlisting role =" php" >
156159<![CDATA[
157160<?php
@@ -185,7 +188,8 @@ int(8192)
185188 </para >
186189 <para >
187190 <example xml : id =" ffi.examples.enum" >
188- <title >Working with C enums</title >
191+ <title >C の enum を扱う</title >
192+ !!!
189193 <programlisting role =" php" >
190194<![CDATA[
191195<?php
@@ -214,12 +218,15 @@ int(3)
214218 </para >
215219 </section >
216220 <section xml : id =" ffi.examples-callback" >
217- <title >PHP Callbacks</title >
221+ <title >PHP のコールバック</title >
222+ !!!
218223 <para >
219224 It is possible to assign a PHP closure to a native variable of function pointer type
220225 or to pass it as a function argument:
221226 <example >
227+ !!!
222228 <title >Assigning a PHP <classname >Closure</classname > to a C function pointer</title >
229+ !!!
223230 <programlisting role =" php" >
224231<![CDATA[
225232<?php
@@ -255,17 +262,20 @@ Hello World 3!
255262]]>
256263 </screen >
257264 </example >
265+ !!!
258266 Although this works, this functionality is not supported on all libffi platforms, is not efficient
259267 and leaks resources by the end of request.
260268 <tip >
261269 <simpara >
270+ !!!
262271 It is therefore recommended to minimize the usage of PHP callbacks.
263272 </simpara >
264273 </tip >
265274 </para >
266275 </section >
267276
268277 <section xml : id =" ffi.examples-complete" >
278+ !!!
269279 <title >A Complete PHP/FFI/preloading Example</title >
270280 <informalexample >
271281 <simpara ><filename >php.ini</filename ></simpara >
0 commit comments