From 6fd4db3848dccf0fe44d1f09e24bec93db3c44dc Mon Sep 17 00:00:00 2001 From: kazu Date: Mon, 20 Feb 2017 12:20:22 +0000 Subject: extension.rdoc: add document title * doc/extension.rdoc, doc/extension.ja.rdoc: [DOC] add title and adapt subheading levels. * doc/extension.rdoc: [DOC] fix subheading level of section about "Ruby Constants That Can Be Accessed From C". * doc/extension.ja.rdoc: [DOC] add missing subheading. [ruby-core:79590] [Bug #13229] Author: Marcus Stollsteimer git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57665 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- doc/extension.ja.rdoc | 130 +++++++++++++++++++++++---------------------- doc/extension.rdoc | 142 +++++++++++++++++++++++++------------------------- 2 files changed, 139 insertions(+), 133 deletions(-) diff --git a/doc/extension.ja.rdoc b/doc/extension.ja.rdoc index 5323bce36b..5ae96a96ae 100644 --- a/doc/extension.ja.rdoc +++ b/doc/extension.ja.rdoc @@ -1,8 +1,10 @@ # extension.ja.rdoc - -*- RDoc -*- created at: Mon Aug 7 16:45:54 JST 1995 += Rubyの拡張ライブラリの作り方 + Rubyの拡張ライブラリの作り方を説明します. -= 基礎知識 +== 基礎知識 Cの変数には型があり,データには型がありません.ですから,た とえばポインタをintの変数に代入すると,その値は整数として取 @@ -23,7 +25,7 @@ VALUEからCにとって意味のあるデータを取り出すためには の両方が必要です.(1)を忘れると間違ったデータの変換が行われ て,最悪プログラムがcore dumpします. -== データタイプ +=== データタイプ Rubyにはユーザが使う可能性のある以下のタイプがあります. @@ -57,7 +59,7 @@ T_SYMBOL :: シンボル ほとんどのタイプはCの構造体で実装されています. -== VALUEのデータタイプをチェックする +=== VALUEのデータタイプをチェックする ruby.hではTYPE()というマクロが定義されていて,VALUEのデータ タイプを知ることが出来ます.TYPE()マクロは上で紹介したT_XXXX @@ -94,7 +96,7 @@ FIXNUMとNILに関してはより高速な判別マクロが用意されてい FIXNUM_P(obj) NIL_P(obj) -== VALUEをCのデータに変換する +=== VALUEをCのデータに変換する データタイプがT_NIL,T_FALSE,T_TRUEである時,データはそれぞ れnil,false,trueです.このデータタイプのオブジェクトはひと @@ -155,7 +157,7 @@ Rubyの構造体を直接アクセスする時に気をつけなければなら ないことです.直接変更した場合,オブジェクトの内容の整合性が とれなくなって,思わぬバグの原因になります. -== CのデータをVALUEに変換する +=== CのデータをVALUEに変換する VALUEの実際の構造は @@ -188,7 +190,7 @@ INT2NUM() :: 任意の整数からVALUEへ INT2NUM()は整数がFIXNUMの範囲に収まらない場合,Bignumに変換 してくれます(が,少し遅い). -== Rubyのデータを操作する +=== Rubyのデータを操作する 先程も述べた通り,Rubyの構造体をアクセスする時に内容の更新を 行うことは勧められません.で,Rubyのデータを操作する時には @@ -197,7 +199,7 @@ Rubyが用意している関数を用いてください. ここではもっとも使われるであろう文字列と配列の生成/操作を行 う関数をあげます(全部ではないです). -=== 文字列に対する関数 +==== 文字列に対する関数 rb_str_new(const char *ptr, long len) :: @@ -295,7 +297,7 @@ rb_str_set_len(VALUE str, long len) :: lenバイトまでの内容は保存される.lenはstrの容量を越えてい てはならない. -=== 配列に対する関数 +==== 配列に対する関数 rb_ary_new() :: @@ -353,14 +355,14 @@ rb_ary_cat(VALUE ary, const VALUE *ptr, long len) :: 配列aryにptrからlen個のオブジェクトを追加する. -= Rubyの機能を使う +== Rubyの機能を使う 原理的にRubyで書けることはCでも書けます.RubyそのものがCで記 述されているんですから,当然といえば当然なんですけど.ここで はRubyの拡張に使うことが多いだろうと予測される機能を中心に紹 介します. -== Rubyに機能を追加する +=== Rubyに機能を追加する Rubyで提供されている関数を使えばRubyインタプリタに新しい機能 を追加することができます.Rubyでは以下の機能を追加する関数が @@ -372,7 +374,7 @@ Rubyで提供されている関数を使えばRubyインタプリタに新しい では順に紹介します. -=== クラス/モジュール定義 +==== クラス/モジュール定義 クラスやモジュールを定義するためには,以下の関数を使います. @@ -389,7 +391,7 @@ Rubyで提供されている関数を使えばRubyインタプリタに新しい VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super) VALUE rb_define_module_under(VALUE outer, const char *name) -=== メソッド/特異メソッド定義 +==== メソッド/特異メソッド定義 メソッドや特異メソッドを定義するには以下の関数を使います. @@ -483,7 +485,7 @@ funcはクラスを引数として受け取って,新しく割り当てられ VALUE rb_current_receiver(void) -=== 定数定義 +==== 定数定義 拡張ライブラリが必要な定数はあらかじめ定義しておいた方が良い でしょう.定数を定義する関数は二つあります. @@ -494,7 +496,7 @@ funcはクラスを引数として受け取って,新しく割り当てられ 前者は特定のクラス/モジュールに属する定数を定義するもの,後 者はグローバルな定数を定義するものです. -== Rubyの機能をCから呼び出す +=== Rubyの機能をCから呼び出す 既に『1.5 Rubyのデータを操作する』で一部紹介したような関数を 使えば,Rubyの機能を実現している関数を直接呼び出すことが出来 @@ -505,7 +507,7 @@ funcはクラスを引数として受け取って,新しく割り当てられ それ以外にもRubyの機能を呼び出す方法はいくつかあります. -=== Rubyのプログラムをevalする +==== Rubyのプログラムをevalする CからRubyの機能を呼び出すもっとも簡単な方法として,文字列で 与えられたRubyのプログラムを評価する以下の関数があります. @@ -523,7 +525,7 @@ CからRubyの機能を呼び出すもっとも簡単な方法として,文字 この関数はエラーが発生するとnilを返します.そして,成功時には *stateはゼロに,さもなくば非ゼロになります. -=== IDまたはシンボル +==== IDまたはシンボル Cから文字列を経由せずにRubyのメソッドを呼び出すこともできま す.その前に,Rubyインタプリタ内でメソッドや変数名を指定する @@ -566,7 +568,7 @@ Rubyから引数として与えられたシンボル(または文字列)をシ これらの関数は,IDの代わりにシンボルを返すことを除けば上記の 関数と同じです. -=== CからRubyのメソッドを呼び出す +==== CからRubyのメソッドを呼び出す Cから文字列を経由せずにRubyのメソッドを呼び出すためには以下 の関数を使います. @@ -582,7 +584,7 @@ Cから文字列を経由せずにRubyのメソッドを呼び出すためには applyには引数としてRubyの配列を与えます. -=== 変数/定数を参照/更新する +==== 変数/定数を参照/更新する Cから関数を使って参照・更新できるのは,定数,インスタンス変 数です.大域変数は一部のものはCの大域変数としてアクセスでき @@ -603,11 +605,11 @@ idはrb_intern()で得られるものを使ってください. 定数を新しく定義するためには『2.1.3 定数定義』で紹介さ れている関数を使ってください. -= RubyとCとの情報共有 +== RubyとCとの情報共有 C言語とRubyの間で情報を共有する方法について解説します. -== Cから参照できるRubyの定数 +=== Cから参照できるRubyの定数 以下のRubyの定数はCのレベルから参照できます. @@ -620,7 +622,7 @@ Qnil :: C言語から見た「nil」. -== CとRubyで共有される大域変数 +=== CとRubyで共有される大域変数 CとRubyで大域変数を使って情報を共有できます.共有できる大域 変数にはいくつかの種類があります.そのなかでもっとも良く使わ @@ -672,7 +674,7 @@ getterとsetterの仕様は以下の通りです. (*getter)(ID id); (*setter)(VALUE val, ID id); -== CのデータをRubyオブジェクトにする +=== CのデータをRubyオブジェクトにする Cの世界で定義されたデータ(構造体)をRubyのオブジェクトとして 取り扱いたい場合がありえます.このような場合はTypedData_XXX @@ -685,7 +687,7 @@ Cの世界で定義されたデータ(構造体)をRubyのオブジェクトと があります. ++ -=== 構造体からオブジェクトへ +==== 構造体からオブジェクトへ 構造体へのポインタsvalをRubyオブジェクトに変換するには次のマ クロを使います。 @@ -793,7 +795,7 @@ klass, data_typeはData_Wrap_Structと同じ働きをします.type は割り当てるC構造体の型です.割り当てられた構造体は変数sval に代入されます.この変数の型は (type*) である必要があります. -=== オブジェクトから構造体へ +==== オブジェクトから構造体へ TypedData_Wrap_StructやTypedData_Make_Structで生成したオブジェ クトから構造体へのポインタを復元するには以下のマクロを用いま @@ -806,7 +808,9 @@ Cの構造体へのポインタは変数svalに代入されます. これらのマクロの使い方はちょっと分かりにくいので,後で説明す る例題を参照してください. -== ディレクトリを作る +== 例: dbmの拡張ライブラリの作成 + +=== ディレクトリを作る % mkdir ext/dbm @@ -816,14 +820,14 @@ Ruby 1.1からは任意のディレクトリでダイナミックライブラリ ライブラリ用のディレクトリを作る必要があります.名前は適当に 選んで構いません. -== 設計する +=== 設計する まあ,当然なんですけど,どういう機能を実現するかどうかまず設 計する必要があります.どんなクラスをつくるか,そのクラスには どんなメソッドがあるか,クラスが提供する定数などについて設計 します. -== Cコードを書く +=== Cコードを書く 拡張ライブラリ本体となるC言語のソースを書きます.C言語のソー スがひとつの時には「ライブラリ名.c」を選ぶと良いでしょう.C @@ -960,7 +964,7 @@ Cの大域変数は以下の関数を使ってRubyインタプリタに変数の void rb_global_variable(VALUE *var) -== extconf.rbを用意する +=== extconf.rbを用意する Makefileを作る場合の雛型になるextconf.rbというファイルを作り ます.extconf.rbはライブラリのコンパイルに必要な条件のチェッ @@ -991,7 +995,7 @@ Makefileを作る場合の雛型になるextconf.rbというファイルを作 パイルしない時にはcreate_makefileを呼ばなければMakefileは生 成されず,コンパイルも行われません. -== dependを用意する +=== dependを用意する もし,ディレクトリにdependというファイルが存在すれば, Makefileが依存関係をチェックしてくれます. @@ -1000,7 +1004,7 @@ Makefileが依存関係をチェックしてくれます. などで作ることが出来ます.あって損は無いでしょう. -== Makefileを生成する +=== Makefileを生成する Makefileを実際に生成するためには @@ -1022,7 +1026,7 @@ vendor_ruby ディレクトリにインストールする場合には ディレクトリをext以下に用意した場合にはRuby全体のmakeの時に 自動的にMakefileが生成されますので,このステップは不要です. -== makeする +=== makeする 動的リンクライブラリを生成する場合にはその場でmakeしてくださ い.必要であれば make install でインストールされます. @@ -1040,26 +1044,26 @@ extconf.rbを書き換えるなどしてMakefileの再生成が必要な時は を作り,そこに 拡張子 .rb のファイルを置いておけば同時にイン ストールされます. -== デバッグ +=== デバッグ まあ,デバッグしないと動かないでしょうね.ext/Setupにディレ クトリ名を書くと静的にリンクするのでデバッガが使えるようにな ります.その分コンパイルが遅くなりますけど. -== できあがり +=== できあがり 後はこっそり使うなり,広く公開するなり,売るなり,ご自由にお 使いください.Rubyの作者は拡張ライブラリに関して一切の権利を 主張しません. -= Appendix A. Rubyのソースコードの分類 +== Appendix A. Rubyのソースコードの分類 Rubyのソースはいくつかに分類することが出来ます.このうちクラ スライブラリの部分は基本的に拡張ライブラリと同じ作り方になっ ています.これらのソースは今までの説明でほとんど理解できると 思います. -== Ruby言語のコア +=== Ruby言語のコア class.c :: クラスとモジュール error.c :: 例外クラスと例外機構 @@ -1068,14 +1072,14 @@ load.c :: ライブラリのロード object.c :: オブジェクト variable.c :: 変数と定数 -== Rubyの構文解析器 +=== Rubyの構文解析器 parse.y :: 字句解析器と構文定義 parse.c :: 自動生成 defs/keywords :: 予約語 lex.c :: 自動生成 -== Rubyの評価器 (通称YARV) +=== Rubyの評価器 (通称YARV) compile.c eval.c @@ -1101,7 +1105,7 @@ lex.c :: 自動生成 -> opt*.inc : 自動生成 -> vm.inc : 自動生成 -== 正規表現エンジン (鬼車) +=== 正規表現エンジン (鬼車) regex.c regcomp.c @@ -1111,7 +1115,7 @@ lex.c :: 自動生成 regparse.c regsyntax.c -== ユーティリティ関数 +=== ユーティリティ関数 debug.c :: Cデバッガ用のデバッグシンボル dln.c :: 動的ローディング @@ -1119,7 +1123,7 @@ st.c :: 汎用ハッシュ表 strftime.c :: 時刻整形 util.c :: その他のユーティリティ -== Rubyコマンドの実装 +=== Rubyコマンドの実装 dmyext.c dmydln.c @@ -1133,7 +1137,7 @@ util.c :: その他のユーティリティ gem_prelude.rb prelude.rb -== クラスライブラリ +=== クラスライブラリ array.c :: Array bignum.c :: Bignum @@ -1165,24 +1169,24 @@ time.c :: Time defs/known_errors.def :: 例外クラス Errno::* -> known_errors.inc :: 自動生成 -== 多言語化 +=== 多言語化 encoding.c :: Encoding transcode.c :: Encoding::Converter enc/*.c :: エンコーディングクラス群 enc/trans/* :: コードポイント対応表 -== gorubyコマンドの実装 +=== gorubyコマンドの実装 goruby.c golf_prelude.rb : goruby固有のライブラリ -> golf_prelude.c : 自動生成 -= Appendix B. 拡張用関数リファレンス +== Appendix B. 拡張用関数リファレンス C言語からRubyの機能を利用するAPIは以下の通りである. -== 型 +=== 型 VALUE :: @@ -1191,7 +1195,7 @@ VALUE :: 体である.VALUE型をこれらにキャストするためにRで始まる構造体 名を全て大文字にした名前のマクロが用意されている. -== 変数・定数 +=== 変数・定数 Qnil :: @@ -1205,7 +1209,7 @@ Qfalse :: 定数: falseオブジェクト -== Cデータのカプセル化 +=== Cデータのカプセル化 Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval) :: @@ -1224,7 +1228,7 @@ Data_Get_Struct(data, type, sval) :: dataからtype型のポインタを取り出し変数svalに代入するマクロ. -== 型チェック +=== 型チェック RB_TYPE_P(value, type) TYPE(value) @@ -1235,7 +1239,7 @@ Data_Get_Struct(data, type, sval) :: void Check_Type(VALUE value, int type) SafeStringValue(value) -== 型変換 +=== 型変換 FIX2INT(value), INT2FIX(i) FIX2LONG(value), LONG2FIX(l) @@ -1258,7 +1262,7 @@ Data_Get_Struct(data, type, sval) :: StringValueCStr(value) rb_str_new2(s) -== クラス/モジュール定義 +=== クラス/モジュール定義 VALUE rb_define_class(const char *name, VALUE super) :: @@ -1286,7 +1290,7 @@ void rb_extend_object(VALUE object, VALUE module) :: オブジェクトをモジュール(で定義されているメソッド)で拡張する. -== 大域変数定義 +=== 大域変数定義 void rb_define_variable(const char *name, VALUE *var) :: @@ -1318,7 +1322,7 @@ void rb_global_variable(VALUE *var) :: GCのため,Rubyプログラムからはアクセスされないが, Rubyオブ ジェクトを含む大域変数をマークする. -== 定数 +=== 定数 void rb_define_const(VALUE klass, const char *name, VALUE val) :: @@ -1332,7 +1336,7 @@ void rb_define_global_const(const char *name, VALUE val) :: と同じ意味. -== メソッド定義 +=== メソッド定義 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc) :: @@ -1421,7 +1425,7 @@ VALUE rb_extract_keywords(VALUE *original_hash) :: 先には,元のHashがSymbol以外のキーを含んでいた場合はそれらが コピーされた別の新しいHash,そうでなければ0が保存されます. -== Rubyメソッド呼び出し +=== Rubyメソッド呼び出し VALUE rb_funcall(VALUE recv, ID mid, int narg, ...) :: @@ -1461,7 +1465,7 @@ int rb_respond_to(VALUE obj, ID id) :: objがidで示されるメソッドを持つかどうかを返す. -== インスタンス変数 +=== インスタンス変数 VALUE rb_iv_get(VALUE obj, const char *name) :: @@ -1474,7 +1478,7 @@ VALUE rb_iv_set(VALUE obj, const char *name, VALUE val) :: objのインスタンス変数をvalにセットする. -== 制御構造 +=== 制御構造 VALUE rb_block_call(VALUE obj, ID mid, int argc, VALUE * argv, VALUE (*func) (ANYARGS), VALUE data2) :: @@ -1536,7 +1540,7 @@ void rb_iter_break_value(VALUE value) :: 現在の最も内側のブロックをvalueで終了する.ブロックは引数で 与えられたvalueを返す.この関数は直接の呼び出し元に戻らない. -== 例外・エラー +=== 例外・エラー void rb_warning(const char *fmt, ...) :: @@ -1568,7 +1572,7 @@ void rb_bug(const char *fmt, ...) :: きはObject#inspect)を使ったVALUEの出力に利用できる.これは "%i"と衝突するため,整数には"%d"を使用すること. -== Rubyの初期化・実行 +=== Rubyの初期化・実行 Rubyをアプリケーションに埋め込む場合には以下のインタフェース を使う.通常の拡張ライブラリには必要ない. @@ -1592,7 +1596,7 @@ void ruby_script(char *name) :: Rubyのスクリプト名($0)を設定する. -== インタプリタのイベントのフック +=== インタプリタのイベントのフック void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data) :: @@ -1622,7 +1626,7 @@ int rb_remove_event_hook(rb_event_hook_func_t func) :: 指定されたフック関数を削除します. -== メモリ使用量 +=== メモリ使用量 void rb_gc_adjust_memory_usage(ssize_t diff) :: @@ -1634,7 +1638,7 @@ void rb_gc_adjust_memory_usage(ssize_t diff) :: す.メモリブロックが解放されたり,メモリブロックがより小さいサイズで再 確保されたりした場合などです.この関数はGCを引き起こすかもしれません. -== 互換性のためのマクロ +=== 互換性のためのマクロ APIの互換性をチェックするために以下のマクロがデフォルトで定義されています. @@ -1676,7 +1680,7 @@ RB_EVENT_HOOKS_HAVE_CALLBACK_DATA :: rb_add_event_hook() がフック関数に渡す data を第3引数として 受け取ることを意味する. -= Appendix C. extconf.rbで使える関数たち +== Appendix C. extconf.rbで使える関数たち extconf.rbの中では利用可能なコンパイル条件チェックの関数は以 下の通りである. @@ -1802,7 +1806,7 @@ pkg_config(pkg, option=nil) :: optionが指定された場合は,上記の配列の代わりにそのオプションを 指定して得られた出力をstripしたものを返す. -= Appendix D. 世代別GC +== Appendix D. 世代別GC Ruby 2.1から世代別GCに対応しました.我々はこれをRGenGCと呼んでいます. RGenGCは,過去の拡張ライブラリに(ほぼ)互換性を保つように開発されている diff --git a/doc/extension.rdoc b/doc/extension.rdoc index d5426ec3fe..303c7a2c32 100644 --- a/doc/extension.rdoc +++ b/doc/extension.rdoc @@ -1,8 +1,10 @@ # extension.rdoc - -*- RDoc -*- created at: Mon Aug 7 16:45:54 JST 1995 += Creating Extension Libraries for Ruby + This document explains how to make extension libraries for Ruby. -= Basic Knowledge +== Basic Knowledge In C, variables have types and data do not have types. In contrast, Ruby variables do not have a static type, and data themselves have @@ -18,7 +20,7 @@ To retrieve C data from a VALUE, you need to: Converting to the wrong data type may cause serious problems. -== Data Types +=== Data Types The Ruby interpreter has the following data types: @@ -52,7 +54,7 @@ T_ZOMBIE :: object awaiting finalization Most of the types are represented by C structures. -== Check Data Type of the VALUE +=== Check Data Type of the VALUE The macro TYPE() defined in ruby.h shows the data type of the VALUE. TYPE() returns the constant number T_XXXX described above. To handle @@ -86,7 +88,7 @@ There are also faster check macros for fixnums and nil. FIXNUM_P(obj) NIL_P(obj) -== Convert VALUE into C Data +=== Convert VALUE into C Data The data for type T_NIL, T_FALSE, T_TRUE are nil, false, true respectively. They are singletons for the data type. @@ -139,7 +141,7 @@ Notice: Do not change the value of the structure directly, unless you are responsible for the result. This ends up being the cause of interesting bugs. -== Convert C Data into VALUE +=== Convert C Data into VALUE To convert C data to Ruby values: @@ -165,14 +167,14 @@ INT2NUM() :: for arbitrary sized integers. INT2NUM() converts an integer into a Bignum if it is out of the FIXNUM range, but is a bit slower. -== Manipulating Ruby Data +=== Manipulating Ruby Data As I already mentioned, it is not recommended to modify an object's internal structure. To manipulate objects, use the functions supplied by the Ruby interpreter. Some (not all) of the useful functions are listed below: -=== String Functions +==== String Functions rb_str_new(const char *ptr, long len) :: @@ -273,7 +275,7 @@ rb_str_set_len(VALUE str, long len) :: up to len bytes, regardless RSTRING_LEN(str). len must not exceed the capacity of str. -=== Array Functions +==== Array Functions rb_ary_new() :: @@ -330,9 +332,9 @@ rb_ary_cat(VALUE ary, const VALUE *ptr, long len) :: Appends len elements of objects from ptr to the array. -= Extending Ruby with C +== Extending Ruby with C -== Adding New Features to Ruby +=== Adding New Features to Ruby You can add new features (classes, methods, etc.) to the Ruby interpreter. Ruby provides APIs for defining the following things: @@ -341,7 +343,7 @@ interpreter. Ruby provides APIs for defining the following things: - Methods, Singleton Methods - Constants -=== Class and Module Definition +==== Class and Module Definition To define a class or module, use the functions below: @@ -356,7 +358,7 @@ To define nested classes or modules, use the functions below: VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super) VALUE rb_define_module_under(VALUE outer, const char *name) -=== Method and Singleton Method Definition +==== Method and Singleton Method Definition To define methods or singleton methods, use these functions: @@ -449,7 +451,7 @@ available), you can use: VALUE rb_current_receiver(void) -=== Constant Definition +==== Constant Definition We have 2 functions to define constants: @@ -459,11 +461,11 @@ We have 2 functions to define constants: The former is to define a constant under specified class/module. The latter is to define a global constant. -== Use Ruby Features from C +=== Use Ruby Features from C There are several ways to invoke Ruby's features from C code. -=== Evaluate Ruby Programs in a String +==== Evaluate Ruby Programs in a String The easiest way to use Ruby's functionality from a C program is to evaluate the string as Ruby program. This function will do the job: @@ -481,7 +483,7 @@ function: It returns nil when an error occurred. Moreover, *state is zero if str was successfully evaluated, or nonzero otherwise. -=== ID or Symbol +==== ID or Symbol You can invoke methods directly, without parsing the string. First I need to explain about ID. ID is the integer number to represent @@ -532,7 +534,7 @@ and to convert Ruby Symbol object to ID, use ID SYM2ID(VALUE symbol) -=== Invoke Ruby Method from C +==== Invoke Ruby Method from C To invoke methods directly, you can use the function below @@ -541,7 +543,7 @@ To invoke methods directly, you can use the function below This function invokes a method on the recv, with the method name specified by the symbol mid. -=== Accessing the Variables and Constants +==== Accessing the Variables and Constants You can access class variables and instance variables using access functions. Also, global variables can be shared between both @@ -560,7 +562,7 @@ To access the constants of the class/module: See also Constant Definition above. -= Information Sharing Between Ruby and C +== Information Sharing Between Ruby and C === Ruby Constants That Can Be Accessed From C @@ -576,7 +578,7 @@ Qnil :: Ruby nil in C scope. -== Global Variables Shared Between C and Ruby +=== Global Variables Shared Between C and Ruby Information can be shared between the two environments using shared global variables. To define them, you can use functions listed below: @@ -618,7 +620,7 @@ The prototypes of the getter and setter functions are as follows: VALUE (*getter)(ID id); void (*setter)(VALUE val, ID id); -== Encapsulate C Data into a Ruby Object +=== Encapsulate C Data into a Ruby Object Sometimes you need to expose your struct in the C world as a Ruby object. @@ -632,7 +634,7 @@ In the future version of Ruby, it is possible old macros will not work. ++ -=== C struct to Ruby object +==== C struct to Ruby object You can convert sval, a pointer to your struct, into a Ruby object with the next macro. @@ -735,7 +737,7 @@ Arguments klass and data_type work like their counterparts in TypedData_Wrap_Struct(). A pointer to the allocated structure will be assigned to sval, which should be a pointer of the type specified. -=== Ruby object to C struct +==== Ruby object to C struct To retrieve the C pointer from the Data object, use the macro Data_Get_Struct(). @@ -746,23 +748,23 @@ A pointer to the structure will be assigned to the variable sval. See the example below for details. -= Example - Creating the dbm Extension +== Example - Creating the dbm Extension OK, here's the example of making an extension library. This is the extension to access DBMs. The full source is included in the ext/ directory in the Ruby's source tree. -== Make the Directory +=== Make the Directory % mkdir ext/dbm Make a directory for the extension library under ext directory. -== Design the Library +=== Design the Library You need to design the library features, before making it. -== Write the C Code +=== Write the C Code You need to write C code for your extension library. If your library has only one source file, choosing ``LIBRARY.c'' as a file name is @@ -887,7 +889,7 @@ but are not exported to the Ruby world. You need to protect them by void rb_global_variable(VALUE *var) -== Prepare extconf.rb +=== Prepare extconf.rb If the file named extconf.rb exists, it will be executed to generate Makefile. @@ -936,7 +938,7 @@ If a compilation condition is not fulfilled, you should not call ``create_makefile''. The Makefile will not be generated, compilation will not be done. -== Prepare Depend (Optional) +=== Prepare Depend (Optional) If the file named depend exists, Makefile will include that file to check dependencies. You can make this file by invoking @@ -945,7 +947,7 @@ check dependencies. You can make this file by invoking It's harmless. Prepare it. -== Generate Makefile +=== Generate Makefile Try generating the Makefile by: @@ -960,7 +962,7 @@ You don't need this step if you put the extension library under the ext directory of the ruby source tree. In that case, compilation of the interpreter will do this step for you. -== Run make +=== Run make Type @@ -969,21 +971,21 @@ Type to compile your extension. You don't need this step either if you have put the extension library under the ext directory of the ruby source tree. -== Debug +=== Debug You may need to rb_debug the extension. Extensions can be linked statically by adding the directory name in the ext/Setup file so that you can inspect the extension with the debugger. -== Done! Now You Have the Extension Library +=== Done! Now You Have the Extension Library You can do anything you want with your library. The author of Ruby will not claim any restrictions on your code depending on the Ruby API. Feel free to use, modify, distribute or sell your program. -= Appendix A. Ruby Source Files Overview +== Appendix A. Ruby Source Files Overview -== Ruby Language Core +=== Ruby Language Core class.c :: classes and modules error.c :: exception classes and exception mechanism @@ -992,14 +994,14 @@ load.c :: library loading object.c :: objects variable.c :: variables and constants -== Ruby Syntax Parser +=== Ruby Syntax Parser parse.y :: grammar definition parse.c :: automatically generated from parse.y defs/keywords :: reserved keywords lex.c :: automatically generated from keywords -== Ruby Evaluator (a.k.a. YARV) +=== Ruby Evaluator (a.k.a. YARV) compile.c eval.c @@ -1025,7 +1027,7 @@ lex.c :: automatically generated from keywords -> opt*.inc : automatically generated -> vm.inc : automatically generated -== Regular Expression Engine (Oniguruma) +=== Regular Expression Engine (Oniguruma) regex.c regcomp.c @@ -1035,7 +1037,7 @@ lex.c :: automatically generated from keywords regparse.c regsyntax.c -== Utility Functions +=== Utility Functions debug.c :: debug symbols for C debugger dln.c :: dynamic loading @@ -1043,7 +1045,7 @@ st.c :: general purpose hash table strftime.c :: formatting times util.c :: misc utilities -== Ruby Interpreter Implementation +=== Ruby Interpreter Implementation dmyext.c dmydln.c @@ -1057,7 +1059,7 @@ util.c :: misc utilities gem_prelude.rb prelude.rb -== Class Library +=== Class Library array.c :: Array bignum.c :: Bignum @@ -1089,22 +1091,22 @@ time.c :: Time defs/known_errors.def :: Errno::* exception classes -> known_errors.inc :: automatically generated -== Multilingualization +=== Multilingualization encoding.c :: Encoding transcode.c :: Encoding::Converter enc/*.c :: encoding classes enc/trans/* :: codepoint mapping tables -== goruby Interpreter Implementation +=== goruby Interpreter Implementation goruby.c golf_prelude.rb : goruby specific libraries. -> golf_prelude.c : automatically generated -= Appendix B. Ruby Extension API Reference +== Appendix B. Ruby Extension API Reference -== Types +=== Types VALUE :: @@ -1112,7 +1114,7 @@ VALUE :: such as struct RString, etc. To refer the values in structures, use casting macros like RSTRING(obj). -== Variables and Constants +=== Variables and Constants Qnil :: @@ -1126,7 +1128,7 @@ Qfalse :: false object -== C Pointer Wrapping +=== C Pointer Wrapping Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval) :: @@ -1146,7 +1148,7 @@ Data_Get_Struct(data, type, sval) :: This macro retrieves the pointer value from DATA, and assigns it to the variable sval. -== Checking Data Types +=== Checking Data Types RB_TYPE_P(value, type) :: @@ -1180,7 +1182,7 @@ SafeStringValue(value) :: Checks that +value+ is a String and is not tainted -== Data Type Conversion +=== Data Type Conversion FIX2INT(value), INT2FIX(i) :: @@ -1264,7 +1266,7 @@ rb_str_new2(s) :: char * -> String -== Defining Classes and Modules +=== Defining Classes and Modules VALUE rb_define_class(const char *name, VALUE super) :: @@ -1291,7 +1293,7 @@ void rb_extend_object(VALUE object, VALUE module) :: Extend the object with the module's attributes. -== Defining Global Variables +=== Defining Global Variables void rb_define_variable(const char *name, VALUE *var) :: @@ -1332,7 +1334,7 @@ void rb_global_variable(VALUE *var) :: GC requires C global variables which hold Ruby values to be marked. rb_global_variable tells GC to protect these variables. -== Constant Definition +=== Constant Definition void rb_define_const(VALUE klass, const char *name, VALUE val) :: @@ -1344,7 +1346,7 @@ void rb_define_global_const(const char *name, VALUE val) :: rb_define_const(rb_cObject, name, val) -== Method Definition +=== Method Definition rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc) :: @@ -1447,7 +1449,7 @@ VALUE rb_extract_keywords(VALUE *original_hash) :: non-symbol keys, then they are copied to another hash and the new hash is stored through +original_hash+, else 0 is stored. -== Invoking Ruby method +=== Invoking Ruby method VALUE rb_funcall(VALUE recv, ID mid, int narg, ...) :: @@ -1485,7 +1487,7 @@ int rb_respond_to(VALUE obj, ID id) :: Returns true if the object responds to the message specified by id. -== Instance Variables +=== Instance Variables VALUE rb_iv_get(VALUE obj, const char *name) :: @@ -1496,7 +1498,7 @@ VALUE rb_iv_set(VALUE obj, const char *name, VALUE val) :: Sets the value of the instance variable. -== Control Structure +=== Control Structure VALUE rb_block_call(VALUE recv, ID mid, int argc, VALUE * argv, VALUE (*func) (ANYARGS), VALUE data2) :: @@ -1562,7 +1564,7 @@ void rb_iter_break_value(VALUE value) :: return the given argument value. This function never return to the caller. -== Exceptions and Errors +=== Exceptions and Errors void rb_warn(const char *fmt, ...) :: @@ -1597,7 +1599,7 @@ Note: In the format string, "%"PRIsVALUE can be used for Object#to_s must be a VALUE). Since it conflicts with "%i", for integers in format strings, use "%d". -== Initialize and Start the Interpreter +=== Initialize and Start the Interpreter The embedding API functions are below (not needed for extension libraries): @@ -1622,7 +1624,7 @@ void ruby_script(char *name) :: Specifies the name of the script ($0). -== Hooks for the Interpreter Events +=== Hooks for the Interpreter Events void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data) :: @@ -1652,7 +1654,7 @@ int rb_remove_event_hook(rb_event_hook_func_t func) :: Removes the specified hook function. -== Memory usage +=== Memory usage void rb_gc_adjust_memory_usage(ssize_t diff) :: @@ -1664,7 +1666,7 @@ void rb_gc_adjust_memory_usage(ssize_t diff) :: is decreased; a memory block is freed or a block is reallocated as smaller size. This function may trigger the GC. -== Macros for Compatibility +=== Macros for Compatibility Some macros to check API compatibilities are available by default. @@ -1704,11 +1706,11 @@ RB_EVENT_HOOKS_HAVE_CALLBACK_DATA :: Means that rb_add_event_hook() takes the third argument `data', to be passed to the given event hook function. -= Appendix C. Functions available for use in extconf.rb +== Appendix C. Functions available for use in extconf.rb See documentation for {mkmf}[rdoc-ref:MakeMakefile]. -= Appendix D. Generational GC +== Appendix D. Generational GC Ruby 2.1 introduced a generational garbage collector (called RGenGC). RGenGC (mostly) keeps compatibility. @@ -1722,7 +1724,7 @@ If your library adheres to the following tips, performance can be further improved. Especially, the "Don't touch pointers directly" section is important. -== Incompatibility +=== Incompatibility You can't write RBASIC(obj)->klass field directly because it is const value now. @@ -1741,13 +1743,13 @@ VALUE rb_obj_reveal(VALUE obj, VALUE klass) :: Reset RBasic::klass to be klass. We expect the `klass' is hidden class by rb_obj_hide(). -== Write barriers +=== Write barriers RGenGC doesn't require write barriers to support generational GC. However, caring about write barrier can improve the performance of RGenGC. Please check the following tips. -=== Don't touch pointers directly +==== Don't touch pointers directly In MRI (include/ruby/ruby.h), some macros to acquire pointers to the internal data structures are supported such as RARRAY_PTR(), @@ -1756,7 +1758,7 @@ RSTRUCT_PTR() and so on. DO NOT USE THESE MACROS and instead use the corresponding C-APIs such as rb_ary_aref(), rb_ary_store() and so on. -=== Consider whether to insert write barriers +==== Consider whether to insert write barriers You don't need to care about write barriers if you only use built-in types. @@ -1778,7 +1780,7 @@ introduce critical bugs. And inserting write barriers has several areas of overhead. Basically we don't recommend you insert write barriers. Please carefully consider the risks. -=== Combine with built-in types +==== Combine with built-in types Please consider utilizing built-in types. Most built-in types support write barrier, so you can use them to avoid manually inserting write @@ -1793,7 +1795,7 @@ references. With use of such techniques, you don't need to insert write barriers anymore. -=== Insert write barriers +==== Insert write barriers \[AGAIN] Inserting write barriers is a very difficult hack, and it is easy to introduce critical bugs. And inserting write barriers has @@ -1807,7 +1809,7 @@ available in include/ruby/ruby.h. An example is available in iseq.c. For a complete guide for RGenGC and write barriers, please refer to . -= Appendix E. RB_GC_GUARD to protect from premature GC +== Appendix E. RB_GC_GUARD to protect from premature GC C Ruby currently uses conservative garbage collection, thus VALUE variables must remain visible on the stack or registers to ensure any -- cgit v1.2.3