From 56fca7f141c2a911ad96df56fed0b9bdf657930e Mon Sep 17 00:00:00 2001 From: drbrain Date: Mon, 3 Dec 2012 23:34:17 +0000 Subject: * README.EXT: Converted to RDoc format * README.EXT.ja: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38170 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 + README.EXT | 1164 ++++++++++++++++++++++++++++++--------------------------- README.EXT.ja | 863 +++++++++++++++++++++--------------------- 3 files changed, 1038 insertions(+), 994 deletions(-) diff --git a/ChangeLog b/ChangeLog index a3cfbba6c9..25853841e4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Dec 4 08:33:46 2012 Eric Hodel + + * README.EXT: Converted to RDoc format + * README.EXT.ja: ditto + Tue Dec 4 08:32:10 2012 Eric Hodel * lib/rdoc/ri/driver.rb: Fixed ri page display for files with diff --git a/README.EXT b/README.EXT index d81debcba9..ba74e5c8d2 100644 --- a/README.EXT +++ b/README.EXT @@ -1,8 +1,8 @@ -.\" README.EXT - -*- Text -*- created at: Mon Aug 7 16:45:54 JST 1995 +# README.EXT - -*- RDoc -*- created at: Mon Aug 7 16:45:54 JST 1995 This document explains how to make extension libraries for Ruby. -1. 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 @@ -13,47 +13,46 @@ has its data-type. To retrieve C data from a VALUE, you need to: - (1) Identify the VALUE's data type - (2) Convert the VALUE into C data +1. Identify the VALUE's data type +2. Convert the VALUE into C data Converting to the wrong data type may cause serious problems. - -1.1 Data-types +== Data-Types The Ruby interpreter has the following data types: - T_NIL nil - T_OBJECT ordinary object - T_CLASS class - T_MODULE module - T_FLOAT floating point number - T_STRING string - T_REGEXP regular expression - T_ARRAY array - T_HASH associative array - T_STRUCT (Ruby) structure - T_BIGNUM multi precision integer - T_FIXNUM Fixnum(31bit or 63bit integer) - T_COMPLEX complex number - T_RATIONAL rational number - T_FILE IO - T_TRUE true - T_FALSE false - T_DATA data - T_SYMBOL symbol +T_NIL :: nil +T_OBJECT :: ordinary object +T_CLASS :: class +T_MODULE :: module +T_FLOAT :: floating point number +T_STRING :: string +T_REGEXP :: regular expression +T_ARRAY :: array +T_HASH :: associative array +T_STRUCT :: (Ruby) structure +T_BIGNUM :: multi precision integer +T_FIXNUM :: Fixnum(31bit or 63bit integer) +T_COMPLEX :: complex number +T_RATIONAL :: rational number +T_FILE :: IO +T_TRUE :: true +T_FALSE :: false +T_DATA :: data +T_SYMBOL :: symbol In addition, there are several other types used internally: - T_ICLASS - T_MATCH - T_UNDEF - T_NODE - T_ZOMBIE +T_ICLASS :: included module +T_MATCH :: MatchData object +T_UNDEF :: undefined +T_NODE :: syntax tree node +T_ZOMBIE :: object awaiting finalization Most of the types are represented by C structures. -1.2 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 @@ -87,7 +86,7 @@ There are also faster check macros for fixnums and nil. FIXNUM_P(obj) NIL_P(obj) -1.3 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. @@ -137,17 +136,17 @@ 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. -1.4 Convert C data into VALUE +== Convert C Data into VALUE To convert C data to Ruby values: - * FIXNUM +FIXNUM :: - left shift 1 bit, and turn on LSB. + left shift 1 bit, and turn on LSB. - * Other pointer values +Other pointer values:: - cast to VALUE. + cast to VALUE. You can determine whether a VALUE is pointer or not by checking its LSB. @@ -157,149 +156,149 @@ structures are defined in . To convert C numbers to Ruby values, use these macros. - INT2FIX() for integers within 31bits. - INT2NUM() for arbitrary sized integer. +INT2FIX() :: for integers within 31bits. +INT2NUM() :: for arbitrary sized integer. INT2NUM() converts an integer into a Bignum if it is out of the FIXNUM range, but is a bit slower. -1.5 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) +rb_str_new(const char *ptr, long len) :: - Creates a new Ruby string. + Creates a new Ruby string. - rb_str_new2(const char *ptr) - rb_str_new_cstr(const char *ptr) +rb_str_new2(const char *ptr) :: +rb_str_new_cstr(const char *ptr) :: - Creates a new Ruby string from a C string. This is equivalent to - rb_str_new(ptr, strlen(ptr)). + Creates a new Ruby string from a C string. This is equivalent to + rb_str_new(ptr, strlen(ptr)). - rb_tainted_str_new(const char *ptr, long len) +rb_tainted_str_new(const char *ptr, long len) :: - Creates a new tainted Ruby string. Strings from external data - sources should be tainted. + Creates a new tainted Ruby string. Strings from external data + sources should be tainted. - rb_tainted_str_new2(const char *ptr) - rb_tainted_str_new_cstr(const char *ptr) +rb_tainted_str_new2(const char *ptr) :: +rb_tainted_str_new_cstr(const char *ptr) :: - Creates a new tainted Ruby string from a C string. + Creates a new tainted Ruby string from a C string. - rb_sprintf(const char *format, ...) - rb_vsprintf(const char *format, va_list ap) +rb_sprintf(const char *format, ...) :: +rb_vsprintf(const char *format, va_list ap) :: - Creates a new Ruby string with printf(3) format. + Creates a new Ruby string with printf(3) format. - rb_str_cat(VALUE str, const char *ptr, long len) +rb_str_cat(VALUE str, const char *ptr, long len) :: - Appends len bytes of data from ptr to the Ruby string. + Appends len bytes of data from ptr to the Ruby string. - rb_str_cat2(VALUE str, const char* ptr) +rb_str_cat2(VALUE str, const char* ptr) :: - Appends C string ptr to Ruby string str. This function is - equivalent to rb_str_cat(str, ptr, strlen(ptr)). + Appends C string ptr to Ruby string str. This function is + equivalent to rb_str_cat(str, ptr, strlen(ptr)). - rb_str_catf(VALUE str, const char* format, ...) - rb_str_vcatf(VALUE str, const char* format, va_list ap) +rb_str_catf(VALUE str, const char* format, ...) :: +rb_str_vcatf(VALUE str, const char* format, va_list ap) :: - Appends C string format and successive arguments to Ruby string - str according to a printf-like format. These functions are - equivalent to rb_str_cat2(str, rb_sprintf(format, ...)) and - rb_str_cat2(str, rb_vsprintf(format, ap)), respectively. + Appends C string format and successive arguments to Ruby string + str according to a printf-like format. These functions are + equivalent to rb_str_cat2(str, rb_sprintf(format, ...)) and + rb_str_cat2(str, rb_vsprintf(format, ap)), respectively. - rb_enc_str_new(const char *ptr, long len, rb_encoding *enc) +rb_enc_str_new(const char *ptr, long len, rb_encoding *enc) :: - Creates a new Ruby string with the specified encoding. + Creates a new Ruby string with the specified encoding. - rb_usascii_str_new(const char *ptr, long len) - rb_usascii_str_new_cstr(const char *ptr) +rb_usascii_str_new(const char *ptr, long len) :: +rb_usascii_str_new_cstr(const char *ptr) :: - Creates a new Ruby string with encoding US-ASCII. + Creates a new Ruby string with encoding US-ASCII. - rb_str_resize(VALUE str, long len) +rb_str_resize(VALUE str, long len) :: - Resizes Ruby string to len bytes. If str is not modifiable, this - function raises an exception. The length of str must be set in - advance. If len is less than the old length the content beyond - len bytes is discarded, else if len is greater than the old length - the content beyond the old length bytes will not be preserved but - will be garbage. Note that RSTRING_PTR(str) may change by calling - this function. + Resizes Ruby string to len bytes. If str is not modifiable, this + function raises an exception. The length of str must be set in + advance. If len is less than the old length the content beyond + len bytes is discarded, else if len is greater than the old length + the content beyond the old length bytes will not be preserved but + will be garbage. Note that RSTRING_PTR(str) may change by calling + this function. - rb_str_set_len(VALUE str, long len) +rb_str_set_len(VALUE str, long len) :: - Sets the length of Ruby string. If str is not modifiable, this - function raises an exception. This function preserves the content - upto len bytes, regardless RSTRING_LEN(str). len must not exceed - the capacity of str. + Sets the length of Ruby string. If str is not modifiable, this + function raises an exception. This function preserves the content + upto len bytes, regardless RSTRING_LEN(str). len must not exceed + the capacity of str. - Array functions +=== Array Functions - rb_ary_new() +rb_ary_new() :: - Creates an array with no elements. + Creates an array with no elements. - rb_ary_new2(long len) +rb_ary_new2(long len) :: - Creates an array with no elements, allocating internal buffer - for len elements. + Creates an array with no elements, allocating internal buffer + for len elements. - rb_ary_new3(long n, ...) +rb_ary_new3(long n, ...) :: - Creates an n-element array from the arguments. + Creates an n-element array from the arguments. - rb_ary_new4(long n, VALUE *elts) +rb_ary_new4(long n, VALUE *elts) :: - Creates an n-element array from a C array. + Creates an n-element array from a C array. - rb_ary_to_ary(VALUE obj) +rb_ary_to_ary(VALUE obj) :: - Converts the object into an array. - Equivalent to Object#to_ary. + Converts the object into an array. + Equivalent to Object#to_ary. - There are many functions to operate an array. - They may dump core if other types are given. +There are many functions to operate an array. They may dump core if other +types are given. - rb_ary_aref(argc, VALUE *argv, VALUE ary) +rb_ary_aref(argc, VALUE *argv, VALUE ary) :: - Equivaelent to Array#[]. + Equivaelent to Array#[]. - rb_ary_entry(VALUE ary, long offset) +rb_ary_entry(VALUE ary, long offset) :: - ary[offset] + ary[offset] - rb_ary_subseq(VALUE ary, long beg, long len) +rb_ary_subseq(VALUE ary, long beg, long len) :: - ary[beg, len] + ary[beg, len] - rb_ary_push(VALUE ary, VALUE val) - rb_ary_pop(VALUE ary) - rb_ary_shift(VALUE ary) - rb_ary_unshift(VALUE ary, VALUE val) +rb_ary_push(VALUE ary, VALUE val) :: +rb_ary_pop(VALUE ary) :: +rb_ary_shift(VALUE ary) :: +rb_ary_unshift(VALUE ary, VALUE val) :: - rb_ary_cat(VALUE ary, const VALUE *ptr, long len) +rb_ary_cat(VALUE ary, const VALUE *ptr, long len) :: - Appends len elements of objects from ptr to the array. + Appends len elements of objects from ptr to the array. -2. Extending Ruby with C += Extending Ruby with C -2.1 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: - * Classes, Modules - * Methods, Singleton Methods - * Constants +* Classes, Modules +* Methods, Singleton Methods +* Constants -2.1.1 Class/module definition +=== Class and Module Definition To define a class or module, use the functions below: @@ -314,7 +313,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) -2.1.2 Method/singleton method definition +=== Method and Singleton Method Definition To define methods or singleton methods, use these functions: @@ -346,7 +345,7 @@ where obj is the receiver, and args is the Ruby array containing actual arguments. There are some more functions to define methods. One takes an ID -as the name of method to be defined. See 2.2.2 for IDs. +as the name of method to be defined. See also ID or Symbol below. void rb_define_method_id(VALUE klass, ID name, VALUE (*func)(ANYARGS), int argc) @@ -397,7 +396,7 @@ func has to take the klass as the argument and return a newly allocated instance. This instance should be as empty as possible, without any expensive (including external) resources. -2.1.3 Constant definition +=== Constant Definition We have 2 functions to define constants: @@ -407,11 +406,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. -2.2 Use Ruby features from C +== Use Ruby Features from C There are several ways to invoke Ruby's features from C code. -2.2.1 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: @@ -429,8 +428,7 @@ function: It returns nil when an error occur. Moreover, *state is zero if str was successfully evaluated, or nonzero otherwise. - -2.2.2 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 @@ -438,9 +436,11 @@ Ruby's identifiers such as variable names. The Ruby data type corresponding to ID is Symbol. It can be accessed from Ruby in the form: - :Identifier + :Identifier + or - :"any kind of string" + + :"any kind of string" You can get the ID value from a string within C code by using @@ -469,7 +469,7 @@ and to convert Ruby Symbol object to ID, use ID SYM2ID(VALUE symbol) -2.2.3 Invoke Ruby method from C +=== Invoke Ruby Method from C To invoke methods directly, you can use the function below @@ -478,7 +478,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. -2.2.4 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 @@ -495,11 +495,11 @@ To access the constants of the class/module: VALUE rb_const_get(VALUE obj, ID id) -See 2.1.3 for defining new constant. +See also Constant Definition above. -3. Information sharing between Ruby and C += Information Sharing Between Ruby and C -3.1 Ruby constants that C can be accessed from C +=== Ruby Constants That C Can Be Accessed From C As stated in section 1.3, the following Ruby constants can be referred from C. @@ -513,7 +513,7 @@ Boolean values. Qfalse is false in C also (i.e. 0). Ruby nil in C scope. -3.2 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: @@ -557,7 +557,7 @@ The prototypes of the getter and setter functions are as follows: void (*setter)(VALUE val, ID id); -3.3 Encapsulate C data into a Ruby object +== Encapsulate C Data into a Ruby Object To wrap and objectify a C pointer as a Ruby object (so called DATA), use Data_Wrap_Struct(). @@ -597,23 +597,23 @@ A pointer to the structure will be assigned to the variable sval. See the example below for details. -4. Example - Creating dbm extension += Example - Creating 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. -(1) make the directory +== Make the Directory % mkdir ext/dbm Make a directory for the extension library under ext directory. -(2) design the library +== Design the Library You need to design the library features, before making it. -(3) write 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 @@ -630,41 +630,37 @@ the library. Here's the example of an initializing function. --- -void -Init_dbm(void) -{ - /* define DBM class */ - cDBM = rb_define_class("DBM", rb_cObject); - /* DBM includes Enumerate module */ - rb_include_module(cDBM, rb_mEnumerable); - - /* DBM has class method open(): arguments are received as C array */ - rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1); - - /* DBM instance method close(): no args */ - rb_define_method(cDBM, "close", fdbm_close, 0); - /* DBM instance method []: 1 argument */ - rb_define_method(cDBM, "[]", fdbm_fetch, 1); - : - - /* ID for a instance variable to store DBM data */ - id_dbm = rb_intern("dbm"); -} --- + void + Init_dbm(void) + { + /* define DBM class */ + cDBM = rb_define_class("DBM", rb_cObject); + /* DBM includes Enumerate module */ + rb_include_module(cDBM, rb_mEnumerable); + + /* DBM has class method open(): arguments are received as C array */ + rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1); + + /* DBM instance method close(): no args */ + rb_define_method(cDBM, "close", fdbm_close, 0); + /* DBM instance method []: 1 argument */ + rb_define_method(cDBM, "[]", fdbm_fetch, 1); + + /* ... */ + + /* ID for a instance variable to store DBM data */ + id_dbm = rb_intern("dbm"); + } The dbm extension wraps the dbm struct in the C environment using Data_Make_Struct. --- -struct dbmdata { - int di_size; - DBM *di_dbm; -}; - + struct dbmdata { + int di_size; + DBM *di_dbm; + }; -obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp); --- + obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp); This code wraps the dbmdata structure into a Ruby object. We avoid wrapping DBM* directly, because we want to cache size information. @@ -672,12 +668,10 @@ wrapping DBM* directly, because we want to cache size information. To retrieve the dbmdata structure from a Ruby object, we define the following macro: --- -#define GetDBM(obj, dbmp) {\ - Data_Get_Struct(obj, struct dbmdata, dbmp);\ - if (dbmp->di_dbm == 0) closed_dbm();\ -} --- + #define GetDBM(obj, dbmp) {\ + Data_Get_Struct(obj, struct dbmdata, dbmp);\ + if (dbmp->di_dbm == 0) closed_dbm();\ + } This sort of complicated macro does the retrieving and close checking for the DBM. @@ -685,13 +679,11 @@ the DBM. There are three kinds of way to receive method arguments. First, methods with a fixed number of arguments receive arguments like this: --- -static VALUE -fdbm_delete(VALUE obj, VALUE keystr) -{ - : -} --- + static VALUE + fdbm_delete(VALUE obj, VALUE keystr) + { + /* ... */ + } The first argument of the C function is the self, the rest are the arguments to the method. @@ -699,17 +691,15 @@ arguments to the method. Second, methods with an arbitrary number of arguments receive arguments like this: --- -static VALUE -fdbm_s_open(int argc, VALUE *argv, VALUE klass) -{ - : - if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) { - mode = 0666; /* default value */ - } - : -} --- + static VALUE + fdbm_s_open(int argc, VALUE *argv, VALUE klass) + { + /* ... */ + if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) { + mode = 0666; /* default value */ + } + /* ... */ + } The first argument is the number of method arguments, the second argument is the C array of the method arguments, and the third @@ -724,25 +714,21 @@ references. The following is an example of a method that takes arguments by Ruby's array: --- -static VALUE -thread_initialize(VALUE thread, VALUE args) -{ - : -} --- + static VALUE + thread_initialize(VALUE thread, VALUE args) + { + /* ... */ + } The first argument is the receiver, the second one is the Ruby array which contains the arguments to the method. -** Notice - -GC should know about global variables which refer to Ruby's objects, but -are not exported to the Ruby world. You need to protect them by +*Notice*: GC should know about global variables which refer to Ruby's objects, +but are not exported to the Ruby world. You need to protect them by void rb_global_variable(VALUE *var) -(4) prepare extconf.rb +== Prepare extconf.rb If the file named extconf.rb exists, it will be executed to generate Makefile. @@ -791,7 +777,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. -(5) 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 @@ -800,7 +786,7 @@ check dependencies. You can make this file by invoking It's harmless. Prepare it. -(6) generate Makefile +== Generate Makefile Try generating the Makefile by: @@ -815,7 +801,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. -(7) make +== Run make Type @@ -824,36 +810,38 @@ 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. -(8) 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. -(9) 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 - gc.c : memory management - load.c : library loading - object.c : objects - variable.c : variables and constants +class.c :: classes and modules +error.c :: exception classes and exception mechanism +gc.c :: memory management +load.c :: library loading +object.c :: objects +variable.c :: variables and constants -ruby syntax parser - parse.y - -> parse.c : automatically generated - keywords : reserved keywords - -> lex.c : automatically generated +== Ruby Syntax Parser + +parse.y :: grammar definition +parse.c :: automatically generated from parse.y +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 eval_error.c @@ -878,7 +866,8 @@ ruby evaluator (a.k.a. YARV) -> opt*.inc : automatically generated -> vm.inc : automatically generated -regular expression engine (oniguruma) +== Regular Expression Engine (Oniguruma) + regex.c regcomp.c regenc.c @@ -887,15 +876,15 @@ regular expression engine (oniguruma) regparse.c regsyntax.c -utility functions +== Utility Functions - debug.c : debug symbols for C debuggger - dln.c : dynamic loading - st.c : general purpose hash table - strftime.c : formatting times - util.c : misc utilities +debug.c :: debug symbols for C debuggger +dln.c :: dynamic loading +st.c :: general purpose hash table +strftime.c :: formatting times +util.c :: misc utilities -ruby interpreter implementation +== Ruby Interpreter Implementation dmyext.c dmydln.c @@ -909,505 +898,564 @@ ruby interpreter implementation gem_prelude.rb prelude.rb - -class library - - array.c : Array - bignum.c : Bignum - compar.c : Comparable - complex.c : Complex - cont.c : Fiber, Continuation - dir.c : Dir - enum.c : Enumerable - enumerator.c : Enumerator - file.c : File - hash.c : Hash - io.c : IO - marshal.c : Marshal - math.c : Math - numeric.c : Numeric, Integer, Fixnum, Float - pack.c : Array#pack, String#unpack - proc.c : Binding, Proc - process.c : Process - random.c : random number - range.c : Range - rational.c : Rational - re.c : Regexp, MatchData - signal.c : Signal - sprintf.c : - string.c : String - struct.c : Struct - time.c : Time - - defs/known_errors.def : Errno::* exception classes - -> known_errors.inc : automatically generated - -multilingualization - encoding.c : Encoding - transcode.c : Encoding::Converter - enc/*.c : encoding classes - enc/trans/* : codepoint mapping tables - -goruby interpreter implementation +== Class Library + +array.c :: Array +bignum.c :: Bignum +compar.c :: Comparable +complex.c :: Complex +cont.c :: Fiber, Continuation +dir.c :: Dir +enum.c :: Enumerable +enumerator.c :: Enumerator +file.c :: File +hash.c :: Hash +io.c :: IO +marshal.c :: Marshal +math.c :: Math +numeric.c :: Numeric, Integer, Fixnum, Float +pack.c :: Array#pack, String#unpack +proc.c :: Binding, Proc +process.c :: Process +random.c :: random number +range.c :: Range +rational.c :: Rational +re.c :: Regexp, MatchData +signal.c :: Signal +sprintf.c :: String#sprintf +string.c :: String +struct.c :: Struct +time.c :: Time + +defs/known_errors.def :: Errno::* exception classes +-> known_errors.inc :: automatically generated + +== Multilingualization + +encoding.c :: Encoding +transcode.c :: Encoding::Converter +enc/*.c :: encoding classes +enc/trans/* :: codepoint mapping tables + +== 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 + +VALUE :: + + The type for the Ruby object. Actual structures are defined in ruby.h, + such as struct RString, etc. To refer the values in structures, use + casting macros like RSTRING(obj). + +== Variables and Constants + +Qnil:: + nil object + +Qtrue:: + true object (default true value) + +Qfalse:: + false object + +== C Pointer Wrapping + +Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval) :: + + Wrap a C pointer into a Ruby object. If object has references to other + Ruby objects, they should be marked by using the mark function during + the GC process. Otherwise, mark should be 0. When this object is no + longer referred by anywhere, the pointer will be discarded by free + function. + +Data_Make_Struct(klass, type, mark, free, sval) :: + + This macro allocates memory using malloc(), assigns it to the variable + sval, and returns the DATA encapsulating the pointer to memory region. + +Data_Get_Struct(data, type, sval) :: + + This macro retrieves the pointer value from DATA, and assigns it to + the variable sval. + +== Checking Data Types + +TYPE(value) :: + + Internal type (T_NIL, T_FIXNUM, etc.) + +FIXNUM_P(value) :: + + Is +value+ a Fixnum? + +NIL_P(value) :: + + Is +value+ nil? + +void Check_Type(VALUE value, int type) :: + + Ensures +value+ is of the given internal +type+ or raises a TypeError + +SaveStringValue(value) :: + + Checks that +value+ is a String and is not tainted + +== Data Type Conversion + +FIX2INT(value), INT2FIX(i) :: + + Fixnum <-> integer + +FIX2LONG(value), LONG2FIX(l) :: + + Fixnum <-> long + +NUM2INT(value), INT2NUM(i) :: + + Numeric <-> integer + +NUM2UINT(value), UINT2NUM(ui) :: + + Numeric <-> unsigned integer + +NUM2LONG(value), LONG2NUM(l) :: + + Numeric <-> long + +NUM2ULONG(value), ULONG2NUM(ul) :: + + Numeric <-> unsigned long + +NUM2LL(value), LL2NUM(ll) :: -** Types + Numeric <-> long long - VALUE +NUM2ULL(value), ULL2NUM(ull) :: -The type for the Ruby object. Actual structures are defined in ruby.h, -such as struct RString, etc. To refer the values in structures, use -casting macros like RSTRING(obj). + Numeric <-> unsigned long long -** Variables and constants +NUM2OFFT(value), OFFT2NUM(off) :: - Qnil + Numeric <-> off_t -const: nil object +NUM2SIZET(value), SIZET2NUM(size) :: - Qtrue + Numeric <-> size_t -const: true object(default true value) +NUM2SSIZET(value), SSIZET2NUM(ssize) :: - Qfalse + Numeric <-> ssize_t -const: false object +NUM2DBL(value) :: -** C pointer wrapping + Numeric -> double - Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval) +rb_float_new(f) :: -Wrap a C pointer into a Ruby object. If object has references to other -Ruby objects, they should be marked by using the mark function during -the GC process. Otherwise, mark should be 0. When this object is no -longer referred by anywhere, the pointer will be discarded by free -function. + double -> Float - Data_Make_Struct(klass, type, mark, free, sval) +StringValue(value) :: -This macro allocates memory using malloc(), assigns it to the variable -sval, and returns the DATA encapsulating the pointer to memory region. + Object with #to_str -> String - Data_Get_Struct(data, type, sval) +StringValuePtr(value) :: -This macro retrieves the pointer value from DATA, and assigns it to -the variable sval. + Object with #to_str -> pointer to String data -** Checking data types +StringValueCStr(value) :: -TYPE(value) -FIXNUM_P(value) -NIL_P(value) -void Check_Type(VALUE value, int type) -void Check_SafeStr(VALUE value) + Object with #to_str -> pointer to String data without NULL bytes -** Data type conversion +rb_str_new2(s) :: -FIX2INT(value), INT2FIX(i) -FIX2LONG(value), LONG2FIX(l) -NUM2INT(value), INT2NUM(i) -NUM2UINT(value), UINT2NUM(ui) -NUM2LONG(value), LONG2NUM(l) -NUM2ULONG(value), ULONG2NUM(ul) -NUM2LL(value), LL2NUM(ll) -NUM2ULL(value), ULL2NUM(ull) -NUM2OFFT(value), OFFT2NUM(off) -NUM2SIZET(value), SIZET2NUM(size) -NUM2SSIZET(value), SSIZET2NUM(ssize) -NUM2DBL(value) -rb_float_new(f) -StringValue(value) -StringValuePtr(value) -StringValueCStr(value) -rb_str_new2(s) + char * -> String -** defining class/module +== Defining Class and Module - VALUE rb_define_class(const char *name, VALUE super) +VALUE rb_define_class(const char *name, VALUE super) :: -Defines a new Ruby class as a subclass of super. + Defines a new Ruby class as a subclass of super. - VALUE rb_define_class_under(VALUE module, const char *name, VALUE super) +VALUE rb_define_class_under(VALUE module, const char *name, VALUE super) :: -Creates a new Ruby class as a subclass of super, under the module's -namespace. + Creates a new Ruby class as a subclass of super, under the module's + namespace. - VALUE rb_define_module(const char *name) +VALUE rb_define_module(const char *name) :: -Defines a new Ruby module. + Defines a new Ruby module. - VALUE rb_define_module_under(VALUE module, const char *name) +VALUE rb_define_module_under(VALUE module, const char *name) :: -Defines a new Ruby module under the module's namespace. + Defines a new Ruby module under the module's namespace. - void rb_include_module(VALUE klass, VALUE module) +void rb_include_module(VALUE klass, VALUE module) :: -Includes module into class. If class already includes it, just -ignored. + Includes module into class. If class already includes it, just ignored. - void rb_extend_object(VALUE object, VALUE module) +void rb_extend_object(VALUE object, VALUE module) :: -Extend the object with the module's attributes. + Extend the object with the module's attributes. -** Defining Global Variables +== Defining Global Variables - void rb_define_variable(const char *name, VALUE *var) +void rb_define_variable(const char *name, VALUE *var) :: -Defines a global variable which is shared between C and Ruby. If name -contains a character which is not allowed to be part of the symbol, -it can't be seen from Ruby programs. + Defines a global variable which is shared between C and Ruby. If name + contains a character which is not allowed to be part of the symbol, + it can't be seen from Ruby programs. - void rb_define_readonly_variable(const char *name, VALUE *var) +void rb_define_readonly_variable(const char *name, VALUE *var) :: -Defines a read-only global variable. Works just like -rb_define_variable(), except the defined variable is read-only. + Defines a read-only global variable. Works just like + rb_define_variable(), except the defined variable is read-only. - void rb_define_virtual_variable(const char *name, - VALUE (*getter)(), VALUE (*setter)()) +void rb_define_virtual_variable(const char *name, VALUE (*getter)(), VALUE (*setter)()) :: -Defines a virtual variable, whose behavior is defined by a pair of C -functions. The getter function is called when the variable is -referenced. The setter function is called when the variable is set to a -value. The prototype for getter/setter functions are: + Defines a virtual variable, whose behavior is defined by a pair of C + functions. The getter function is called when the variable is + referenced. The setter function is called when the variable is set to a + value. The prototype for getter/setter functions are: - VALUE getter(ID id) - void setter(VALUE val, ID id) + VALUE getter(ID id) + void setter(VALUE val, ID id) -The getter function must return the value for the access. + The getter function must return the value for the access. - void rb_define_hooked_variable(const char *name, VALUE *var, - VALUE (*getter)(), VALUE (*setter)()) +void rb_define_hooked_variable(const char *name, VALUE *var, VALUE (*getter)(), VALUE (*setter)()) :: -Defines hooked variable. It's a virtual variable with a C variable. -The getter is called as + Defines hooked variable. It's a virtual variable with a C variable. + The getter is called as - VALUE getter(ID id, VALUE *var) + VALUE getter(ID id, VALUE *var) -returning a new value. The setter is called as + returning a new value. The setter is called as - void setter(VALUE val, ID id, VALUE *var) + void setter(VALUE val, ID id, VALUE *var) -GC requires C global variables which hold Ruby values to be marked. + GC requires C global variables which hold Ruby values to be marked. - void rb_global_variable(VALUE *var) +void rb_global_variable(VALUE *var) -Tells GC to protect these variables. + Tells GC to protect these variables. -** Constant Definition +== Constant Definition - void rb_define_const(VALUE klass, const char *name, VALUE val) +void rb_define_const(VALUE klass, const char *name, VALUE val) :: -Defines a new constant under the class/module. + Defines a new constant under the class/module. - void rb_define_global_const(const char *name, VALUE val) +void rb_define_global_const(const char *name, VALUE val) :: -Defines a global constant. This is just the same as + Defines a global constant. This is just the same as - rb_define_const(cKernal, name, val) + rb_define_const(cKernal, name, val) -** Method Definition +== Method Definition - rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc) +rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc) :: -Defines a method for the class. func is the function pointer. argc -is the number of arguments. if argc is -1, the function will receive -3 arguments: argc, argv, and self. if argc is -2, the function will -receive 2 arguments, self and args, where args is a Ruby array of -the method arguments. + Defines a method for the class. func is the function pointer. argc + is the number of arguments. if argc is -1, the function will receive + 3 arguments: argc, argv, and self. if argc is -2, the function will + receive 2 arguments, self and args, where args is a Ruby array of + the method arguments. - rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc) +rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc) :: -Defines a private method for the class. Arguments are same as -rb_define_method(). + Defines a private method for the class. Arguments are same as + rb_define_method(). - rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc) +rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc) :: -Defines a singleton method. Arguments are same as rb_define_method(). + Defines a singleton method. Arguments are same as rb_define_method(). - rb_scan_args(int argc, VALUE *argv, const char *fmt, ...) +rb_scan_args(int argc, VALUE *argv, const char *fmt, ...) :: -Retrieve argument from argc and argv to given VALUE references -according to the format string. The format can be described in ABNF -as follows: + Retrieve argument from argc and argv to given VALUE references + according to the format string. The format can be described in ABNF + as follows: --- -scan-arg-spec := param-arg-spec [option-hash-arg-spec] [block-arg-spec] + scan-arg-spec := param-arg-spec [option-hash-arg-spec] [block-arg-spec] -param-arg-spec := pre-arg-spec [post-arg-spec] / post-arg-spec / pre-opt-post-arg-spec -pre-arg-spec := num-of-leading-mandatory-args [num-of-optional-args] -post-arg-spec := sym-for-variable-length-args [num-of-trailing-mandatory-args] -pre-opt-post-arg-spec := num-of-leading-mandatory-args num-of-optional-args num-of-trailing-mandatory-args -option-hash-arg-spec := sym-for-option-hash-arg -block-arg-spec := sym-for-block-arg + param-arg-spec := pre-arg-spec [post-arg-spec] / post-arg-spec / + pre-opt-post-arg-spec + pre-arg-spec := num-of-leading-mandatory-args [num-of-optional-args] + post-arg-spec := sym-for-variable-length-args + [num-of-trailing-mandatory-args] + pre-opt-post-arg-spec := num-of-leading-mandatory-args num-of-optional-args + num-of-trailing-mandatory-args + option-hash-arg-spec := sym-for-option-hash-arg + block-arg-spec := sym-for-block-arg -num-of-leading-mandatory-args := DIGIT ; The number of leading - ; mandatory arguments -num-of-optional-args := DIGIT ; The number of optional - ; arguments -sym-for-variable-length-args := "*" ; Indicates that variable - ; length arguments are - ; captured as a ruby array -num-of-trailing-mandatory-args := DIGIT ; The number of trailing - ; mandatory arguments -sym-for-option-hash-arg := ":" ; Indicates that an option - ; hash is captured if the last - ; argument is a hash or can be - ; converted to a hash with - ; #to_hash. When the last - ; argument is nil, it is - ; captured if it is not - ; ambiguous to take it as - ; empty option hash; i.e. '*' - ; is not specified and - ; arguments are given more - ; than sufficient. -sym-for-block-arg := "&" ; Indicates that an iterator - ; block should be captured if - ; given --- + num-of-leading-mandatory-args := DIGIT ; The number of leading + ; mandatory arguments + num-of-optional-args := DIGIT ; The number of optional + ; arguments + sym-for-variable-length-args := "*" ; Indicates that variable + ; length arguments are + ; captured as a ruby array + num-of-trailing-mandatory-args := DIGIT ; The number of trailing + ; mandatory arguments + sym-for-option-hash-arg := ":" ; Indicates that an option + ; hash is captured if the last + ; argument is a hash or can be + ; converted to a hash with + ; #to_hash. When the last + ; argument is nil, it is + ; captured if it is not + ; ambiguous to take it as + ; empty option hash; i.e. '*' + ; is not specified and + ; arguments are given more + ; than sufficient. + sym-for-block-arg := "&" ; Indicates that an iterator + ; block should be captured if + ; given -For example, "12" means that the method requires at least one -argument, and at most receives three (1+2) arguments. So, the format -string must be followed by three variable references, which are to be -assigned to captured arguments. For omitted arguments, variables are -set to Qnil. NULL can be put in place of a variable reference, which -means the corresponding captured argument(s) should be just dropped. + For example, "12" means that the method requires at least one + argument, and at most receives three (1+2) arguments. So, the format + string must be followed by three variable references, which are to be + assigned to captured arguments. For omitted arguments, variables are + set to Qnil. NULL can be put in place of a variable reference, which + means the corresponding captured argument(s) should be just dropped. -The number of given arguments, excluding an option hash or iterator -block, is returned. + The number of given arguments, excluding an option hash or iterator + block, is returned. -** Invoking Ruby method +== Invoking Ruby method - VALUE rb_funcall(VALUE recv, ID mid, int narg, ...) +VALUE rb_funcall(VALUE recv, ID mid, int narg, ...) :: -Invokes a method. To retrieve mid from a method name, use rb_intern(). + Invokes a method. To retrieve mid from a method name, use rb_intern(). - VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv) +VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv) :: -Invokes a method, passing arguments by an array of values. + Invokes a method, passing arguments by an array of values. - VALUE rb_eval_string(const char *str) +VALUE rb_eval_string(const char *str) :: -Compiles and executes the string as a Ruby program. + Compiles and executes the string as a Ruby program. - ID rb_intern(const char *name) +ID rb_intern(const char *name) :: -Returns ID corresponding to the name. + Returns ID corresponding to the name. - char *rb_id2name(ID id) +char *rb_id2name(ID id) :: -Returns the name corresponding ID. + Returns the name corresponding ID. - char *rb_class2name(VALUE klass) +char *rb_class2name(VALUE klass) :: -Returns the name of the class. + Returns the name of the class. - int rb_respond_to(VALUE object, ID id) +int rb_respond_to(VALUE object, ID id) :: -Returns true if the object responds to the message specified by 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) +VALUE rb_iv_get(VALUE obj, const char *name) :: -Retrieve the value of the instance variable. If the name is not -prefixed by `@', that variable shall be inaccessible from Ruby. + Retrieve the value of the instance variable. If the name is not + prefixed by `@', that variable shall be inaccessible from Ruby. - VALUE rb_iv_set(VALUE obj, const char *name, VALUE val) +VALUE rb_iv_set(VALUE obj, const char *name, VALUE val) :: -Sets the value of the instance variable. + 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) +VALUE rb_block_call(VALUE recv, ID mid, int argc, VALUE * argv, VALUE (*func) (ANYARGS), VALUE data2) :: -Calls a method on the recv, with the method name specified by the -symbol mid, with argc arguments in argv, supplying func as the -block. When func is called as the block, it will receive the value -from yield as the first argument, and data2 as the second argument. -When yielded with multiple values (in C, rb_yield_values(), -rb_yield_values2() and rb_yield_splat()), data2 is packed as an Array, -whereas yielded values can be gotten via argc/argv of the third/fourth -arguments. + Calls a method on the recv, with the method name specified by the + symbol mid, with argc arguments in argv, supplying func as the + block. When func is called as the block, it will receive the value + from yield as the first argument, and data2 as the second argument. + When yielded with multiple values (in C, rb_yield_values(), + rb_yield_values2() and rb_yield_splat()), data2 is packed as an Array, + whereas yielded values can be gotten via argc/argv of the third/fourth + arguments. - [OBSOLETE] VALUE rb_iterate(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2) +[OBSOLETE] VALUE rb_iterate(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2) :: -Calls the function func1, supplying func2 as the block. func1 will be -called with the argument arg1. func2 receives the value from yield as -the first argument, arg2 as the second argument. + Calls the function func1, supplying func2 as the block. func1 will be + called with the argument arg1. func2 receives the value from yield as + the first argument, arg2 as the second argument. -When rb_iterate is used in 1.9, func1 has to call some Ruby-level method. -This function is obsolete since 1.9; use rb_block_call instead. + When rb_iterate is used in 1.9, func1 has to call some Ruby-level method. + This function is obsolete since 1.9; use rb_block_call instead. - VALUE rb_yield(VALUE val) +VALUE rb_yield(VALUE val) :: -Evaluates the block with value val. + Evaluates the block with value val. - VALUE rb_rescue(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) +VALUE rb_rescue(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) :: -Calls the function func1, with arg1 as the argument. If an exception -occurs during func1, it calls func2 with arg2 as the argument. The -return value of rb_rescue() is the return value from func1 if no -exception occurs, from func2 otherwise. + Calls the function func1, with arg1 as the argument. If an exception + occurs during func1, it calls func2 with arg2 as the argument. The + return value of rb_rescue() is the return value from func1 if no + exception occurs, from func2 otherwise. - VALUE rb_ensure(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) +VALUE rb_ensure(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) :: -Calls the function func1 with arg1 as the argument, then calls func2 -with arg2 if execution terminated. The return value from -rb_ensure() is that of func1 when no exception occured. + Calls the function func1 with arg1 as the argument, then calls func2 + with arg2 if execution terminated. The return value from + rb_ensure() is that of func1 when no exception occured. - VALUE rb_protect(VALUE (*func) (VALUE), VALUE arg, int *state) +VALUE rb_protect(VALUE (*func) (VALUE), VALUE arg, int *state) :: -Calls the function func with arg as the argument. If no exception -occured during func, it returns the result of func and *state is zero. -Otherwise, it returns Qnil and sets *state to nonzero. If state is -NULL, it is not set in both cases. -You have to clear the error info with rb_set_errinfo(Qnil) when -ignoring the caught exception. + Calls the function func with arg as the argument. If no exception + occured during func, it returns the result of func and *state is zero. + Otherwise, it returns Qnil and sets *state to nonzero. If state is + NULL, it is not set in both cases. + You have to clear the error info with rb_set_errinfo(Qnil) when + ignoring the caught exception. - void rb_jump_tag(int state) +void rb_jump_tag(int state) :: -Continues the exception caught by rb_protect() and rb_eval_string_protect(). -state must be the returned value from those functions. This function -never return to the caller. + Continues the exception caught by rb_protect() and rb_eval_string_protect(). + state must be the returned value from those functions. This function + never return to the caller. - void rb_iter_break() +void rb_iter_break() :: -Exits from the current innermost block. This function never return to -the caller. + Exits from the current innermost block. This function never return to + the caller. - void rb_iter_break_value(VALUE value) +void rb_iter_break_value(VALUE value) :: -Exits from the current innermost block with the value. The block will -return the given argument value. This function never return to the -caller. + Exits from the current innermost block with the value. The block will + return the given argument value. This function never return to the + caller. -** Exceptions and Errors +== Exceptions and Errors - void rb_warn(const char *fmt, ...) +void rb_warn(const char *fmt, ...) :: -Prints a warning message according to a printf-like format. + Prints a warning message according to a printf-like format. - void rb_warning(const char *fmt, ...) +void rb_warning(const char *fmt, ...) :: -Prints a warning message according to a printf-like format, if -$VERBOSE is true. + Prints a warning message according to a printf-like format, if + $VERBOSE is true. -void rb_raise(rb_eRuntimeError, const char *fmt, ...) +void rb_raise(rb_eRuntimeError, const char *fmt, ...) :: -Raises RuntimeError. The fmt is a format string just like printf(). + Raises RuntimeError. The fmt is a format string just like printf(). - void rb_raise(VALUE exception, const char *fmt, ...) +void rb_raise(VALUE exception, const char *fmt, ...) :: -Raises a class exception. The fmt is a format string just like printf(). + Raises a class exception. The fmt is a format string just like printf(). - void rb_fatal(const char *fmt, ...) +void rb_fatal(const char *fmt, ...) :: -Raises a fatal error, terminates the interpreter. No exception handling -will be done for fatal errors, but ensure blocks will be executed. + Raises a fatal error, terminates the interpreter. No exception handling + will be done for fatal errors, but ensure blocks will be executed. - void rb_bug(const char *fmt, ...) +void rb_bug(const char *fmt, ...) :: -Terminates the interpreter immediately. This function should be -called under the situation caused by the bug in the interpreter. No -exception handling nor ensure execution will be done. + Terminates the interpreter immediately. This function should be + called under the situation caused by the bug in the interpreter. No + exception handling nor ensure execution will be done. -** Initialize and Start the Interpreter +== Initialize and Start the Interpreter The embedding API functions are below (not needed for extension libraries): - void ruby_init() +void ruby_init() :: -Initializes the interpreter. + Initializes the interpreter. - void ruby_options(int argc, char **argv) +void ruby_options(int argc, char **argv) :: -Process command line arguments for the interpreter. + Process command line arguments for the interpreter. - void ruby_run() +void ruby_run() :: -Starts execution of the interpreter. + Starts execution of the interpreter. - void ruby_script(char *name) +void ruby_script(char *name) :: -Specifies the name of the script ($0). + 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) + void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, + VALUE data) Adds a hook function for the specified interpreter events. -events should be Or'ed value of: - - RUBY_EVENT_LINE - RUBY_EVENT_CLASS - RUBY_EVENT_END - RUBY_EVENT_CALL - RUBY_EVENT_RETURN - RUBY_EVENT_C_CALL - RUBY_EVENT_C_RETURN - RUBY_EVENT_RAISE - RUBY_EVENT_ALL +events should be OR'ed value of: + + RUBY_EVENT_LINE + RUBY_EVENT_CLASS + RUBY_EVENT_END + RUBY_EVENT_CALL + RUBY_EVENT_RETURN + RUBY_EVENT_C_CALL + RUBY_EVENT_C_RETURN + RUBY_EVENT_RAISE + RUBY_EVENT_ALL The definition of rb_event_hook_func_t is below: - typedef void (*rb_event_hook_func_t)(rb_event_t event, VALUE data, - VALUE self, ID id, VALUE klass) + typedef void (*rb_event_hook_func_t)(rb_event_t event, VALUE data, + VALUE self, ID id, VALUE klass) The third argument `data' to rb_add_event_hook() is passed to the hook function as the second argument, which was the pointer to the current NODE in 1.8. See RB_EVENT_HOOKS_HAVE_CALLBACK_DATA below. - int rb_remove_event_hook(rb_event_hook_func_t func) + int rb_remove_event_hook(rb_event_hook_func_t func) Removes the specified hook function. -** Macros for the Compatibilities +== Macros for Compatibility Some macros to check API compatibilities are available by default. - NORETURN_STYLE_NEW +NORETURN_STYLE_NEW :: -Means that NORETURN macro is functional style instead of prefix. + Means that NORETURN macro is functional style instead of prefix. - HAVE_RB_DEFINE_ALLOC_FUNC +HAVE_RB_DEFINE_ALLOC_FUNC :: -Means that function rb_define_alloc_func() is provided, that means the -allocation framework is used. This is same as the result of -have_func("rb_define_alloc_func", "ruby.h"). + Means that function rb_define_alloc_func() is provided, that means the + allocation framework is used. This is same as the result of + have_func("rb_define_alloc_func", "ruby.h"). - HAVE_RB_REG_NEW_STR +HAVE_RB_REG_NEW_STR :: -Means that function rb_reg_new_str() is provided, that creates Regexp -object from String object. This is same as the result of -have_func("rb_reg_new_str", "ruby.h"). + Means that function rb_reg_new_str() is provided, that creates Regexp + object from String object. This is same as the result of + have_func("rb_reg_new_str", "ruby.h"). - HAVE_RB_IO_T +HAVE_RB_IO_T :: -Means that type rb_io_t is provided. + Means that type rb_io_t is provided. - USE_SYMBOL_AS_METHOD_NAME +USE_SYMBOL_AS_METHOD_NAME :: -Means that Symbols will be returned as method names, e.g., -Module#methods, #singleton_methods and so on. + Means that Symbols will be returned as method names, e.g., + Module#methods, #singleton_methods and so on. - HAVE_RUBY_*_H +HAVE_RUBY_*_H :: -Defined in ruby.h and means correspoinding header is available. For -instance, when HAVE_RUBY_ST_H is defined you should use ruby/st.h not -mere st.h. + Defined in ruby.h and means corresponding header is available. For + instance, when HAVE_RUBY_ST_H is defined you should use ruby/st.h not + mere st.h. - RB_EVENT_HOOKS_HAVE_CALLBACK_DATA +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. + Means that rb_add_event_hook() takes the third argument `data', to be + passed to the given event hook function. /* * Local variables: diff --git a/README.EXT.ja b/README.EXT.ja index 4ae8b50221..7b008b952e 100644 --- a/README.EXT.ja +++ b/README.EXT.ja @@ -1,8 +1,8 @@ -.\" README.EXT.ja - -*- Text -*- created at: Mon Aug 7 16:45:54 JST 1995 +# README.EXT.ja - -*- RDoc -*- created at: Mon Aug 7 16:45:54 JST 1995 Rubyの拡張ライブラリの作り方を説明します. -1.基礎知識 += 基礎知識 Cの変数には型があり,データには型がありません.ですから,た とえばポインタをintの変数に代入すると,その値は整数として取 @@ -17,47 +17,47 @@ RubyのデータはVALUEというCの型で表現されます.VALUE型のデ VALUEからCにとって意味のあるデータを取り出すためには - (1) VALUEのデータタイプを知る - (2) VALUEをCのデータに変換する +1. VALUEのデータタイプを知る +2. VALUEをCのデータに変換する の両方が必要です.(1)を忘れると間違ったデータの変換が行われ て,最悪プログラムがcore dumpします. -1.1 データタイプ +== データタイプ Rubyにはユーザが使う可能性のある以下のタイプがあります. - T_NIL nil - T_OBJECT 通常のオブジェクト - T_CLASS クラス - T_MODULE モジュール - T_FLOAT 浮動小数点数 - T_STRING 文字列 - T_REGEXP 正規表現 - T_ARRAY 配列 - T_HASH 連想配列 - T_STRUCT (Rubyの)構造体 - T_BIGNUM 多倍長整数 - T_FIXNUM Fixnum(31bitまたは63bit長整数) - T_COMPLEX 複素数 - T_RATIONAL 有理数 - T_FILE 入出力 - T_TRUE 真 - T_FALSE 偽 - T_DATA データ - T_SYMBOL シンボル +T_NIL :: nil +T_OBJECT :: 通常のオブジェクト +T_CLASS :: クラス +T_MODULE :: モジュール +T_FLOAT :: 浮動小数点数 +T_STRING :: 文字列 +T_REGEXP :: 正規表現 +T_ARRAY :: 配列 +T_HASH :: 連想配列 +T_STRUCT :: (Rubyの)構造体 +T_BIGNUM :: 多倍長整数 +T_FIXNUM :: Fixnum(31bitまたは63bit長整数) +T_COMPLEX :: 複素数 +T_RATIONAL :: 有理数 +T_FILE :: 入出力 +T_TRUE :: 真 +T_FALSE :: 偽 +T_DATA :: データ +T_SYMBOL :: シンボル その他に内部で利用されている以下のタイプがあります. - T_ICLASS - T_MATCH - T_UNDEF - T_NODE - T_ZOMBIE + T_ICLASS + T_MATCH + T_UNDEF + T_NODE + T_ZOMBIE ほとんどのタイプはCの構造体で実装されています. -1.2 VALUEのデータタイプをチェックする +== VALUEのデータタイプをチェックする ruby.hではTYPE()というマクロが定義されていて,VALUEのデータ タイプを知ることが出来ます.TYPE()マクロは上で紹介したT_XXXX @@ -94,7 +94,7 @@ FIXNUMとNILに関してはより高速な判別マクロが用意されてい FIXNUM_P(obj) NIL_P(obj) -1.3 VALUEをCのデータに変換する +== VALUEをCのデータに変換する データタイプがT_NIL,T_FALSE,T_TRUEである時,データはそれぞ れnil,false,trueです.このデータタイプのオブジェクトはひと @@ -153,17 +153,17 @@ Rubyの構造体を直接アクセスする時に気をつけなければなら ないことです.直接変更した場合,オブジェクトの内容の整合性が とれなくなって,思わぬバグの原因になります. -1.4 CのデータをVALUEに変換する +== CのデータをVALUEに変換する VALUEの実際の構造は - * FIXNUMの場合 +FIXNUMの場合 :: - 1bit左シフトして,LSBを立てる. + 1bit左シフトして,LSBを立てる. - * その他のポインタの場合 +その他のポインタの場合 :: - そのままVALUEにキャストする. + そのままVALUEにキャストする. となっています.よって,LSBをチェックすればVALUEがFIXNUMかど うかわかるわけです(ポインタのLSBが立っていないことを仮定して @@ -179,14 +179,14 @@ FIXNUMに関しては変換マクロを経由する必要があります.Cの からVALUEに変換するマクロは以下のものがあります.必要に応じ て使い分けてください. - INT2FIX() もとの整数が31bitまたは63bit以内に収まる自信 - がある時 - INT2NUM() 任意の整数からVALUEへ +INT2FIX() :: もとの整数が31bitまたは63bit以内に収まる自信 + がある時 +INT2NUM() :: 任意の整数からVALUEへ INT2NUM()は整数がFIXNUMの範囲に収まらない場合,Bignumに変換 してくれます(が,少し遅い). -1.5 Rubyのデータを操作する +== Rubyのデータを操作する 先程も述べた通り,Rubyの構造体をアクセスする時に内容の更新を 行うことは勧められません.で,Rubyのデータを操作する時には @@ -195,147 +195,147 @@ Rubyが用意している関数を用いてください. ここではもっとも使われるであろう文字列と配列の生成/操作を行 い関数をあげます(全部ではないです). - 文字列に対する関数 +=== 文字列に対する関数 - rb_str_new(const char *ptr, long len) +rb_str_new(const char *ptr, long len) :: - 新しいRubyの文字列を生成する. + 新しいRubyの文字列を生成する. - rb_str_new2(const char *ptr) - rb_str_new_cstr(const char *ptr) +rb_str_new2(const char *ptr) +rb_str_new_cstr(const char *ptr) - Cの文字列からRubyの文字列を生成する.この関数の機能は - rb_str_new(ptr, strlen(ptr))と同等である. + Cの文字列からRubyの文字列を生成する.この関数の機能は + rb_str_new(ptr, strlen(ptr))と同等である. - rb_tainted_str_new(const char *ptr, long len) +rb_tainted_str_new(const char *ptr, long len) - 汚染マークが付加された新しいRubyの文字列を生成する.外部 - からのデータに基づく文字列には汚染マークが付加されるべき - である. + 汚染マークが付加された新しいRubyの文字列を生成する.外部 + からのデータに基づく文字列には汚染マークが付加されるべき + である. - rb_tainted_str_new2(const char *ptr) - rb_tainted_str_new_cstr(const char *ptr) +rb_tainted_str_new2(const char *ptr) +rb_tainted_str_new_cstr(const char *ptr) - Cの文字列から汚染マークが付加されたRubyの文字列を生成する. + Cの文字列から汚染マークが付加されたRubyの文字列を生成する. - rb_sprintf(const char *format, ...) - rb_vsprintf(const char *format, va_list ap) +rb_sprintf(const char *format, ...) +rb_vsprintf(const char *format, va_list ap) - Cの文字列formatと続く引数をprintf(3)のフォーマットにしたがって - 整形し,Rubyの文字列を生成する. + Cの文字列formatと続く引数をprintf(3)のフォーマットにしたがって + 整形し,Rubyの文字列を生成する. - rb_str_cat(VALUE str, const char *ptr, long len) +rb_str_cat(VALUE str, const char *ptr, long len) - Rubyの文字列strにlenバイトの文字列ptrを追加する. + Rubyの文字列strにlenバイトの文字列ptrを追加する. - rb_str_cat2(VALUE str, const char* ptr) +rb_str_cat2(VALUE str, const char* ptr) - Rubyの文字列strにCの文字列ptrを追加する.この関数の機能は - rb_str_cat(str, ptr, strlen(ptr))と同等である. + Rubyの文字列strにCの文字列ptrを追加する.この関数の機能は + rb_str_cat(str, ptr, strlen(ptr))と同等である. - rb_str_catf(VALUE str, const char* format, ...) - rb_str_vcatf(VALUE str, const char* format, va_list ap) +rb_str_catf(VALUE str, const char* format, ...) +rb_str_vcatf(VALUE str, const char* format, va_list ap) - Cの文字列formatと続く引数をprintf(3)のフォーマットにしたがって - 整形し,Rubyの文字列strに追加する.この関数の機能は,それぞれ - rb_str_cat2(str, rb_sprintf(format, ...)) や - rb_str_cat2(str, rb_vsprintf(format, ap)) と同等である. + Cの文字列formatと続く引数をprintf(3)のフォーマットにしたがって + 整形し,Rubyの文字列strに追加する.この関数の機能は,それぞれ + rb_str_cat2(str, rb_sprintf(format, ...)) や + rb_str_cat2(str, rb_vsprintf(format, ap)) と同等である. - rb_enc_str_new(const char *ptr, long len, rb_encoding *enc) +rb_enc_str_new(const char *ptr, long len, rb_encoding *enc) - 指定されたエンコーディングでRubyの文字列を生成する. + 指定されたエンコーディングでRubyの文字列を生成する. - rb_usascii_str_new(const char *ptr, long len) - rb_usascii_str_new_cstr(const char *ptr) +rb_usascii_str_new(const char *ptr, long len) +rb_usascii_str_new_cstr(const char *ptr) - エンコーディングがUS-ASCIIのRubyの文字列を生成する. + エンコーディングがUS-ASCIIのRubyの文字列を生成する. - rb_str_resize(VALUE str, long len) +rb_str_resize(VALUE str, long len) - Rubyの文字列のサイズをlenバイトに変更する.strの長さは前 - 以てセットされていなければならない.lenが元の長さよりも短 - い時は,lenバイトを越えた部分の内容は捨てられる.lenが元 - の長さよりも長い時は,元の長さを越えた部分の内容は保存さ - れないでゴミになるだろう.この関数の呼び出しによって - RSTRING_PTR(str)が変更されるかもしれないことに注意. + Rubyの文字列のサイズをlenバイトに変更する.strの長さは前 + 以てセットされていなければならない.lenが元の長さよりも短 + い時は,lenバイトを越えた部分の内容は捨てられる.lenが元 + の長さよりも長い時は,元の長さを越えた部分の内容は保存さ + れないでゴミになるだろう.この関数の呼び出しによって + RSTRING_PTR(str)が変更されるかもしれないことに注意. - rb_str_set_len(VALUE str, long len) +rb_str_set_len(VALUE str, long len) - Rubyの文字列のサイズをlenバイトにセットする.strが変更可 - 能でなければ例外が発生する.RSTRING_LEN(str)とは無関係に, - lenバイトまでの内容は保存される.lenはstrの容量を越えてい - てはならない. + Rubyの文字列のサイズをlenバイトにセットする.strが変更可 + 能でなければ例外が発生する.RSTRING_LEN(str)とは無関係に, + lenバイトまでの内容は保存される.lenはstrの容量を越えてい + てはならない. - 配列に対する関数 +== 配列に対する関数 - rb_ary_new() +rb_ary_new() - 要素が0の配列を生成する. + 要素が0の配列を生成する. - rb_ary_new2(long len) +rb_ary_new2(long len) - 要素が0の配列を生成する.len要素分の領域をあらかじめ割り - 当てておく. + 要素が0の配列を生成する.len要素分の領域をあらかじめ割り + 当てておく. - rb_ary_new3(long n, ...) +rb_ary_new3(long n, ...) - 引数で指定したn要素を含む配列を生成する. + 引数で指定したn要素を含む配列を生成する. - rb_ary_new4(long n, VALUE *elts) +rb_ary_new4(long n, VALUE *elts) - 配列で与えたn要素の配列を生成する. + 配列で与えたn要素の配列を生成する. - rb_ary_to_ary(VALUE obj) +rb_ary_to_ary(VALUE obj) - オブジェクトを配列に変換する. - Object#to_aryと同等である. + オブジェクトを配列に変換する. + Object#to_aryと同等である. - 他にも配列を操作する関数が多数ある. これらは - 引数aryに配列を渡さなければならない. さもないと - コアを吐く. +他にも配列を操作する関数が多数ある. これらは +引数aryに配列を渡さなければならない. さもないと +コアを吐く. - rb_ary_aref(argc, VALUE *argv, VALUE ary) +rb_ary_aref(argc, VALUE *argv, VALUE ary) - Array#[]と同等. + Array#[]と同等. - rb_ary_entry(VALUE ary, long offset) +rb_ary_entry(VALUE ary, long offset) - ary[offset] + ary[offset] - rb_ary_subseq(VALUE ary, long beg, long len) +rb_ary_subseq(VALUE ary, long beg, long len) - ary[beg, len] + ary[beg, len] - rb_ary_push(VALUE ary, VALUE val) - rb_ary_pop(VALUE ary) - rb_ary_shift(VALUE ary) - rb_ary_unshift(VALUE ary, VALUE val) +rb_ary_push(VALUE ary, VALUE val) +rb_ary_pop(VALUE ary) +rb_ary_shift(VALUE ary) +rb_ary_unshift(VALUE ary, VALUE val) - rb_ary_cat(VALUE ary, const VALUE *ptr, long len) +rb_ary_cat(VALUE ary, const VALUE *ptr, long len) - 配列aryにptrからlen個のオブジェクトを追加する. + 配列aryにptrからlen個のオブジェクトを追加する. -2.Rubyの機能を使う += Rubyの機能を使う 原理的にRubyで書けることはCでも書けます.RubyそのものがCで記 述されているんですから,当然といえば当然なんですけど.ここで はRubyの拡張に使うことが多いだろうと予測される機能を中心に紹 介します. -2.1 Rubyに機能を追加する +== Rubyに機能を追加する Rubyで提供されている関数を使えばRubyインタプリタに新しい機能 を追加することができます.Rubyでは以下の機能を追加する関数が 提供されています. - * クラス,モジュール - * メソッド,特異メソッドなど - * 定数 +* クラス,モジュール +* メソッド,特異メソッドなど +* 定数 では順に紹介します. -2.1.1 クラス/モジュール定義 +=== クラス/モジュール定義 クラスやモジュールを定義するためには,以下の関数を使います. @@ -352,15 +352,15 @@ Rubyで提供されている関数を使えばRubyインタプリタに新しい VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super) VALUE rb_define_module_under(VALUE outer, const char *name) -2.1.2 メソッド/特異メソッド定義 +=== メソッド/特異メソッド定義 メソッドや特異メソッドを定義するには以下の関数を使います. void rb_define_method(VALUE klass, const char *name, - VALUE (*func)(), int argc) + VALUE (*func)(), int argc) void rb_define_singleton_method(VALUE object, const char *name, - VALUE (*func)(), int argc) + VALUE (*func)(), int argc) 念のため説明すると「特異メソッド」とは,その特定のオブジェク @@ -437,7 +437,7 @@ funcはクラスを引数として受け取って,新しく割り当てられ ソースなどを含まない,できるだけ「空」のままにしておいたほう がよいでしょう. -2.1.3 定数定義 +=== 定数定義 拡張ライブラリが必要な定数はあらかじめ定義しておいた方が良い でしょう.定数を定義する関数は二つあります. @@ -448,7 +448,7 @@ funcはクラスを引数として受け取って,新しく割り当てられ 前者は特定のクラス/モジュールに属する定数を定義するもの,後 者はグローバルな定数を定義するものです. -2.2 Rubyの機能をCから呼び出す +== Rubyの機能をCから呼び出す 既に『1.5 Rubyのデータを操作する』で一部紹介したような関数を 使えば,Rubyの機能を実現している関数を直接呼び出すことが出来 @@ -459,7 +459,7 @@ funcはクラスを引数として受け取って,新しく割り当てられ それ以外にもRubyの機能を呼び出す方法はいくつかあります. -2.2.1 Rubyのプログラムをevalする +=== Rubyのプログラムをevalする CからRubyの機能を呼び出すもっとも簡単な方法として,文字列で 与えられたRubyのプログラムを評価する以下の関数があります. @@ -477,8 +477,7 @@ CからRubyの機能を呼び出すもっとも簡単な方法として,文字 この関数はエラーが発生するとnilを返します.そして,成功時には *stateはゼロに,さもなくば非ゼロになります. - -2.2.2 IDまたはシンボル +=== IDまたはシンボル Cから文字列を経由せずにRubyのメソッドを呼び出すこともできま す.その前に,Rubyインタプリタ内でメソッドや変数名を指定する @@ -487,7 +486,9 @@ Cから文字列を経由せずにRubyのメソッドを呼び出すこともで IDとは変数名,メソッド名を表す整数です.Rubyの中では :識別子 + または + :"任意の文字列" でアクセスできます.Cからこの整数を得るためには関数 @@ -509,7 +510,7 @@ IDとは変数名,メソッド名を表す整数です.Rubyの中では 返した場合は常に文字列です.第三の関数はRubyの文字列ではなく NUL終端されたCの文字列を使います. -2.2.3 CからRubyのメソッドを呼び出す +=== CからRubyのメソッドを呼び出す Cから文字列を経由せずにRubyのメソッドを呼び出すためには以下 の関数を使います. @@ -524,7 +525,7 @@ Cから文字列を経由せずにRubyのメソッドを呼び出すためには applyには引数としてRubyの配列を与えます. -2.2.4 変数/定数を参照/更新する +=== 変数/定数を参照/更新する Cから関数を使って参照・更新できるのは,定数,インスタンス変 数です.大域変数は一部のものはCの大域変数としてアクセスでき @@ -545,24 +546,24 @@ idはrb_intern()で得られるものを使ってください. 定数を新しく定義するためには『2.1.3 定数定義』で紹介さ れている関数を使ってください. -3.RubyとCとの情報共有 += RubyとCとの情報共有 C言語とRubyの間で情報を共有する方法について解説します. -3.1 Cから参照できるRubyの定数 +== Cから参照できるRubyの定数 以下のRubyの定数はCのレベルから参照できます. Qtrue Qfalse - 真偽値.QfalseはC言語でも偽とみなされます(つまり0). +真偽値.QfalseはC言語でも偽とみなされます(つまり0). Qnil - C言語から見た「nil」. +C言語から見た「nil」. -3.2 CとRubyで共有される大域変数 +== CとRubyで共有される大域変数 CとRubyで大域変数を使って情報を共有できます.共有できる大域 変数にはいくつかの種類があります.そのなかでもっとも良く使わ @@ -613,7 +614,7 @@ getterとsetterの仕様は以下の通りです. (*getter)(ID id); (*setter)(VALUE val, ID id); -3.3 CのデータをRubyオブジェクトにする +== CのデータをRubyオブジェクトにする Cの世界で定義されたデータ(構造体)をRubyのオブジェクトとして 取り扱いたい場合がありえます.このような場合には,Dataという @@ -664,13 +665,13 @@ Cの構造体へのポインタは変数svalに代入されます. これらのDataの使い方はちょっと分かりにくいので,後で説明する 例題を参照してください. -4.例題 - dbmパッケージを作る += 例題 - dbmパッケージを作る ここまでの説明でとりあえず拡張ライブラリは作れるはずです. Rubyのextディレクトリにすでに含まれているdbmライブラリを例に して段階的に説明します. -(1) ディレクトリを作る +== ディレクトリを作る % mkdir ext/dbm @@ -680,14 +681,14 @@ Ruby 1.1からは任意のディレクトリでダイナミックライブラリ ライブラリ用のディレクトリを作る必要があります.名前は適当に 選んで構いません. -(2) 設計する +== 設計する まあ,当然なんですけど,どういう機能を実現するかどうかまず設 計する必要があります.どんなクラスをつくるか,そのクラスには どんなメソッドがあるか,クラスが提供する定数などについて設計 します. -(3) Cコードを書く +== Cコードを書く 拡張ライブラリ本体となるC言語のソースを書きます.C言語のソー スがひとつの時には「ライブラリ名.c」を選ぶと良いでしょう.C @@ -704,44 +705,40 @@ Rubyは拡張ライブラリをロードする時に「Init_ライブラリ名 です.この関数の中でクラス,モジュール,メソッド,定数などの 定義を行います.dbm.cから一部引用します. --- -void -Init_dbm(void) -{ - /* DBMクラスを定義する */ - cDBM = rb_define_class("DBM", rb_cObject); - /* DBMはEnumerateモジュールをインクルードする */ - rb_include_module(cDBM, rb_mEnumerable); - - /* DBMクラスのクラスメソッドopen(): 引数はCの配列で受ける */ - rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1); - - /* DBMクラスのメソッドclose(): 引数はなし */ - rb_define_method(cDBM, "close", fdbm_close, 0); - /* DBMクラスのメソッド[]: 引数は1個 */ - rb_define_method(cDBM, "[]", fdbm_fetch, 1); - : - - /* DBMデータを格納するインスタンス変数名のためのID */ - id_dbm = rb_intern("dbm"); -} --- + void + Init_dbm(void) + { + /* DBMクラスを定義する */ + cDBM = rb_define_class("DBM", rb_cObject); + /* DBMはEnumerateモジュールをインクルードする */ + rb_include_module(cDBM, rb_mEnumerable); + + /* DBMクラスのクラスメソッドopen(): 引数はCの配列で受ける */ + rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1); + + /* DBMクラスのメソッドclose(): 引数はなし */ + rb_define_method(cDBM, "close", fdbm_close, 0); + /* DBMクラスのメソッド[]: 引数は1個 */ + rb_define_method(cDBM, "[]", fdbm_fetch, 1); + + /* ... */ + + /* DBMデータを格納するインスタンス変数名のためのID */ + id_dbm = rb_intern("dbm"); + } DBMライブラリはdbmのデータと対応するオブジェクトになるはずで すから,Cの世界のdbmをRubyの世界に取り込む必要があります. - dbm.cではData_Make_Structを以下のように使っています. --- -struct dbmdata { - int di_size; - DBM *di_dbm; -}; - - -obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp); --- + struct dbmdata { + int di_size; + DBM *di_dbm; + }; + + + obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp); ここではdbmstruct構造体へのポインタをDataにカプセル化してい ます.DBM*を直接カプセル化しないのはclose()した時の処理を考 @@ -750,12 +747,10 @@ obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp); Dataオブジェクトからdbmstruct構造体のポインタを取り出すため に以下のマクロを使っています. --- -#define GetDBM(obj, dbmp) {\ - Data_Get_Struct(obj, struct dbmdata, dbmp);\ - if (dbmp->di_dbm == 0) closed_dbm();\ -} --- + #define GetDBM(obj, dbmp) {\ + Data_Get_Struct(obj, struct dbmdata, dbmp);\ + if (dbmp->di_dbm == 0) closed_dbm();\ + } ちょっと複雑なマクロですが,要するにdbmdata構造体のポインタ の取り出しと,closeされているかどうかのチェックをまとめてい @@ -766,13 +761,11 @@ DBMクラスにはたくさんメソッドがありますが,分類すると3 してはdeleteメソッドがあります.deleteメソッドを実装している fdbm_delete()はこのようになっています. --- -static VALUE -fdbm_delete(VALUE obj, VALUE keystr) -{ - : -} --- + static VALUE + fdbm_delete(VALUE obj, VALUE keystr) + { + /* ... */ + } 引数の数が固定のタイプは第1引数がself,第2引数以降がメソッド の引数となります. @@ -782,17 +775,17 @@ fdbm_delete(VALUE obj, VALUE keystr) はDBMのクラスメソッドであるopen()です.これを実装している関 数fdbm_s_open()はこうなっています. --- -static VALUE -fdbm_s_open(int argc, VALUE *argv, VALUE klass) -{ - : - if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) { - mode = 0666; /* default value */ - } - : -} --- + static VALUE + fdbm_s_open(int argc, VALUE *argv, VALUE klass) + { + /* ... */ + + if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) { + mode = 0666; /* default value */ + } + + /* ... */ + } このタイプの関数は第1引数が与えられた引数の数,第2引数が与え られた引数の入っている配列になります.selfは第3引数として与 @@ -807,17 +800,15 @@ fdbm_s_open(int argc, VALUE *argv, VALUE klass) 引数をRubyの配列として受け取るメソッドの例には Thread#initializeがあります.実装はこうです. --- -static VALUE -thread_initialize(VALUE thread, VALUE args) -{ - : -} --- + static VALUE + thread_initialize(VALUE thread, VALUE args) + { + /* ... */ + } 第1引数はself,第2引数はRubyの配列です. -** 注意事項 +*注意事項* Rubyと共有はしないがRubyのオブジェクトを格納する可能性のある Cの大域変数は以下の関数を使ってRubyインタプリタに変数の存在 @@ -825,7 +816,7 @@ Cの大域変数は以下の関数を使ってRubyインタプリタに変数の void rb_global_variable(VALUE *var) -(4) extconf.rbを用意する +== extconf.rbを用意する Makefileを作る場合の雛型になるextconf.rbというファイルを作り ます.extconf.rbはライブラリのコンパイルに必要な条件のチェッ @@ -856,7 +847,7 @@ Makefileを作る場合の雛型になるextconf.rbというファイルを作 パイルしない時にはcreate_makefileを呼ばなければMakefileは生 成されず,コンパイルも行われません. -(5) dependを用意する +== dependを用意する もし,ディレクトリにdependというファイルが存在すれば, Makefileが依存関係をチェックしてくれます. @@ -865,7 +856,7 @@ Makefileが依存関係をチェックしてくれます. などで作ることが出来ます.あって損は無いでしょう. -(6) Makefileを生成する +== Makefileを生成する Makefileを実際に生成するためには @@ -887,7 +878,7 @@ vendor_ruby ディレクトリにインストールする場合には ディレクトリをext以下に用意した場合にはRuby全体のmakeの時に 自動的にMakefileが生成されますので,このステップは不要です. -(7) makeする +== makeする 動的リンクライブラリを生成する場合にはその場でmakeしてくださ い.必要であれば make install でインストールされます. @@ -905,41 +896,42 @@ extconf.rbを書き換えるなどしてMakefileの再生成が必要な時は を作り,そこに 拡張子 .rb のファイルを置いておけば同時にイン ストールされます. -(8) デバッグ +== デバッグ まあ,デバッグしないと動かないでしょうね.ext/Setupにディレ クトリ名を書くと静的にリンクするのでデバッガが使えるようにな ります.その分コンパイルが遅くなりますけど. -(9) できあがり +== できあがり 後はこっそり使うなり,広く公開するなり,売るなり,ご自由にお 使いください.Rubyの作者は拡張ライブラリに関して一切の権利を 主張しません. -Appendix A. Rubyのソースコードの分類 += Appendix A. Rubyのソースコードの分類 Rubyのソースはいくつかに分類することが出来ます.このうちクラ スライブラリの部分は基本的に拡張ライブラリと同じ作り方になっ ています.これらのソースは今までの説明でほとんど理解できると 思います. -Ruby言語のコア +== Ruby言語のコア + +class.c :: クラスとモジュール +error.c :: 例外クラスと例外機構 +gc.c :: 記憶領域管理 +load.c :: ライブラリのロード +object.c :: オブジェクト +variable.c :: 変数と定数 - class.c : クラスとモジュール - error.c : 例外クラスと例外機構 - gc.c : 記憶領域管理 - load.c : ライブラリのロード - object.c : オブジェクト - variable.c : 変数と定数 +== Rubyの構文解析器 -Rubyの構文解析器 - parse.y : 字句解析器と構文定義 - -> parse.c : 自動生成 - keywords : 予約語 - -> lex.c : 自動生成 + parse.y : 字句解析器と構文定義 + -> parse.c : 自動生成 + keywords : 予約語 + -> lex.c : 自動生成 -Rubyの評価器 (通称YARV) +== Rubyの評価器 (通称YARV) compile.c eval.c eval_error.c @@ -964,7 +956,8 @@ Rubyの評価器 (通称YARV) -> opt*.inc : 自動生成 -> vm.inc : 自動生成 -正規表現エンジン (鬼車) +== 正規表現エンジン (鬼車) + regex.c regcomp.c regenc.c @@ -973,15 +966,15 @@ Rubyの評価器 (通称YARV) regparse.c regsyntax.c -ユーティリティ関数 +== ユーティリティ関数 - debug.c : Cデバッガ用のデバッグシンボル - dln.c : 動的ローディング - st.c : 汎用ハッシュ表 - strftime.c : 時刻整形 - util.c : その他のユーティリティ +debug.c :: Cデバッガ用のデバッグシンボル +dln.c :: 動的ローディング +st.c :: 汎用ハッシュ表 +strftime.c :: 時刻整形 +util.c :: その他のユーティリティ -Rubyコマンドの実装 +== Rubyコマンドの実装 dmyext.c dmydln.c @@ -995,81 +988,80 @@ Rubyコマンドの実装 gem_prelude.rb prelude.rb -クラスライブラリ - - array.c : Array - bignum.c : Bignum - compar.c : Comparable - complex.c : Complex - cont.c : Fiber, Continuation - dir.c : Dir - enum.c : Enumerable - enumerator.c : Enumerator - file.c : File - hash.c : Hash - io.c : IO - marshal.c : Marshal - math.c : Math - numeric.c : Numeric, Integer, Fixnum, Float - pack.c : Array#pack, String#unpack - proc.c : Binding, Proc - process.c : Process - random.c : 乱数 - range.c : Range - rational.c : Rational - re.c : Regexp, MatchData - signal.c : Signal - sprintf.c : - string.c : String - struct.c : Struct - time.c : Time - - defs/known_errors.def : 例外クラス Errno::* - -> known_errors.inc : 自動生成 - -多言語化 - encoding.c : Encoding - transcode.c : Encoding::Converter - enc/*.c : エンコーディングクラス群 - enc/trans/* : コードポイント対応表 - -gorubyコマンドの実装 +== クラスライブラリ + +array.c :: Array +bignum.c :: Bignum +compar.c :: Comparable +complex.c :: Complex +cont.c :: Fiber, Continuation +dir.c :: Dir +enum.c :: Enumerable +enumerator.c :: Enumerator +file.c :: File +hash.c :: Hash +io.c :: IO +marshal.c :: Marshal +math.c :: Math +numeric.c :: Numeric, Integer, Fixnum, Float +pack.c :: Array#pack, String#unpack +proc.c :: Binding, Proc +process.c :: Process +random.c :: 乱数 +range.c :: Range +rational.c :: Rational +re.c :: Regexp, MatchData +signal.c :: Signal +sprintf.c :: String#sprintf +string.c :: String +struct.c :: Struct +time.c :: Time +defs/known_errors.def :: 例外クラス Errno::* +-> known_errors.inc :: 自動生成 + +== 多言語化 + +encoding.c :: Encoding +transcode.c :: Encoding::Converter +enc/*.c :: エンコーディングクラス群 +enc/trans/* :: コードポイント対応表 + +== gorubyコマンドの実装 goruby.c golf_prelude.rb : goruby固有のライブラリ -> golf_prelude.c : 自動生成 - -Appendix B. 拡張用関数リファレンス += Appendix B. 拡張用関数リファレンス C言語からRubyの機能を利用するAPIは以下の通りである. -** 型 +== 型 -VALUE +VALUE :: Rubyオブジェクトを表現する型.必要に応じてキャストして用いる. 組み込み型を表現するCの型はruby.hに記述してあるRで始まる構造 体である.VALUE型をこれらにキャストするためにRで始まる構造体 名を全て大文字にした名前のマクロが用意されている. -** 変数・定数 +== 変数・定数 -Qnil +Qnil :: 定数: nilオブジェクト -Qtrue +Qtrue :: 定数: trueオブジェクト(真のデフォルト値) -Qfalse +Qfalse :: 定数: falseオブジェクト -** Cデータのカプセル化 +== Cデータのカプセル化 -Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval) +Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval) :: Cの任意のポインタをカプセル化したRubyオブジェクトを返す.こ のポインタがRubyからアクセスされなくなった時,freeで指定した @@ -1077,94 +1069,92 @@ Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval) ジェクトを指している場合,markに指定する関数でマークする必要 がある. -Data_Make_Struct(klass, type, mark, free, sval) +Data_Make_Struct(klass, type, mark, free, sval) :: type型のメモリをmallocし,変数svalに代入した後,それをカプセ ル化したデータを返すマクロ. -Data_Get_Struct(data, type, sval) +Data_Get_Struct(data, type, sval) :: dataからtype型のポインタを取り出し変数svalに代入するマクロ. -** 型チェック - -TYPE(value) -FIXNUM_P(value) -NIL_P(value) -void Check_Type(VALUE value, int type) -void Check_SafeStr(VALUE value) - -** 型変換 - -FIX2INT(value), INT2FIX(i) -FIX2LONG(value), LONG2FIX(l) -NUM2INT(value), INT2NUM(i) -NUM2UINT(value), UINT2NUM(ui) -NUM2LONG(value), LONG2NUM(l) -NUM2ULONG(value), ULONG2NUM(ul) -NUM2LL(value), LL2NUM(ll) -NUM2ULL(value), ULL2NUM(ull) -NUM2OFFT(value), OFFT2NUM(off) -NUM2SIZET(value), SIZET2NUM(size) -NUM2SSIZET(value), SSIZET2NUM(ssize) -NUM2DBL(value) -rb_float_new(f) -StringValue(value) -StringValuePtr(value) -StringValueCStr(value) -rb_str_new2(s) - -** クラス/モジュール定義 - -VALUE rb_define_class(const char *name, VALUE super) +== 型チェック + + TYPE(value) + FIXNUM_P(value) + NIL_P(value) + void Check_Type(VALUE value, int type) + SafeStringValue(value) + +== 型変換 + + FIX2INT(value), INT2FIX(i) + FIX2LONG(value), LONG2FIX(l) + NUM2INT(value), INT2NUM(i) + NUM2UINT(value), UINT2NUM(ui) + NUM2LONG(value), LONG2NUM(l) + NUM2ULONG(value), ULONG2NUM(ul) + NUM2LL(value), LL2NUM(ll) + NUM2ULL(value), ULL2NUM(ull) + NUM2OFFT(value), OFFT2NUM(off) + NUM2SIZET(value), SIZET2NUM(size) + NUM2SSIZET(value), SSIZET2NUM(ssize) + NUM2DBL(value) + rb_float_new(f) + StringValue(value) + StringValuePtr(value) + StringValueCStr(value) + rb_str_new2(s) + +== クラス/モジュール定義 + +VALUE rb_define_class(const char *name, VALUE super) :: superのサブクラスとして新しいRubyクラスを定義する. -VALUE rb_define_class_under(VALUE module, const char *name, VALUE super) +VALUE rb_define_class_under(VALUE module, const char *name, VALUE super) :: superのサブクラスとして新しいRubyクラスを定義し,moduleの 定数として定義する. -VALUE rb_define_module(const char *name) +VALUE rb_define_module(const char *name) :: 新しいRubyモジュールを定義する. -VALUE rb_define_module_under(VALUE module, const char *name) +VALUE rb_define_module_under(VALUE module, const char *name) :: 新しいRubyモジュールを定義し,moduleの定数として定義する. -void rb_include_module(VALUE klass, VALUE module) +void rb_include_module(VALUE klass, VALUE module) :: モジュールをインクルードする.classがすでにmoduleをインク ルードしている時には何もしない(多重インクルードの禁止). -void rb_extend_object(VALUE object, VALUE module) +void rb_extend_object(VALUE object, VALUE module) :: オブジェクトをモジュール(で定義されているメソッド)で拡張する. -** 大域変数定義 +== 大域変数定義 -void rb_define_variable(const char *name, VALUE *var) +void rb_define_variable(const char *name, VALUE *var) :: RubyとCとで共有するグローバル変数を定義する.変数名が`$'で 始まらない時には自動的に追加される.nameとしてRubyの識別子 として許されない文字(例えば` ')を含む場合にはRubyプログラ ムからは見えなくなる. -void rb_define_readonly_variable(const char *name, VALUE *var) +void rb_define_readonly_variable(const char *name, VALUE *var) :: RubyとCとで共有するread onlyのグローバル変数を定義する. read onlyであること以外はrb_define_variable()と同じ. -void rb_define_virtual_variable(const char *name, - VALUE (*getter)(), void (*setter)()) +void rb_define_virtual_variable(const char *name, VALUE (*getter)(), void (*setter)()) :: 関数によって実現されるRuby変数を定義する.変数が参照された 時にはgetterが,変数に値がセットされた時にはsetterが呼ばれ る. -void rb_define_hooked_variable(const char *name, VALUE *var, - VALUE (*getter)(), void (*setter)()) +void rb_define_hooked_variable(const char *name, VALUE *var, VALUE (*getter)(), void (*setter)()) :: 関数によってhookのつけられたグローバル変数を定義する.変数 が参照された時にはgetterが,関数に値がセットされた時には @@ -1176,23 +1166,23 @@ void rb_global_variable(VALUE *var) GCのため,Rubyプログラムからはアクセスされないが, Rubyオブ ジェクトを含む大域変数をマークする. -** 定数 +== 定数 -void rb_define_const(VALUE klass, const char *name, VALUE val) +void rb_define_const(VALUE klass, const char *name, VALUE val) :: 定数を定義する. -void rb_define_global_const(const char *name, VALUE val) +void rb_define_global_const(const char *name, VALUE val) :: 大域定数を定義する. - rb_define_const(rb_cObject, name, val) + rb_define_const(rb_cObject, name, val) と同じ意味. -** メソッド定義 +== メソッド定義 -rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc) +rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc) :: メソッドを定義する.argcはselfを除く引数の数.argcが-1の時, 関数には引数の数(selfを含まない)を第1引数, 引数の配列を第2 @@ -1200,47 +1190,48 @@ rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc) 第1引数がself, 第2引数がargs(argsは引数を含むRubyの配列)と いう形式で与えられる. -rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc) +rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc) :: privateメソッドを定義する.引数はrb_define_method()と同じ. -rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc) +rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc) :: 特異メソッドを定義する.引数はrb_define_method()と同じ. -rb_scan_args(int argc, VALUE *argv, const char *fmt, ...) +rb_scan_args(int argc, VALUE *argv, const char *fmt, ...) :: argc, argv形式で与えられた指定されたフォーマットに従って引 数を分解し,続くVALUEへの参照にセットします.このフォーマッ トは,ABNFで記述すると以下の通りです. --- -scan-arg-spec := param-arg-spec [option-hash-arg-spec] [block-arg-spec] - -param-arg-spec := pre-arg-spec [post-arg-spec] / post-arg-spec / pre-opt-post-arg-spec -pre-arg-spec := num-of-leading-mandatory-args [num-of-optional-args] -post-arg-spec := sym-for-variable-length-args [num-of-trailing-mandatory-args] -pre-opt-post-arg-spec := num-of-leading-mandatory-args num-of-optional-args num-of-trailing-mandatory-args -option-hash-arg-spec := sym-for-option-hash-arg -block-arg-spec := sym-for-block-arg - -num-of-leading-mandatory-args := DIGIT ; 先頭に置かれる省略不能な引数の数 -num-of-optional-args := DIGIT ; 続いて置かれる省略可能な引数の数 -sym-for-variable-length-args := "*" ; 続いて置かれる可変長引数を - ; Rubyの配列で取得するための指定 -num-of-trailing-mandatory-args := DIGIT ; 終端に置かれる省略不能な引数の数 -sym-for-option-hash-arg := ":" ; オプションハッシュを取得する - ; ための指定; 省略不能な引数の - ; 数よりも多くの引数が指定され, - ; 最後の引数がハッシュ(または - ; #to_hashで変換可能)の場合に - ; 取得される.最後の引数がnilの - ; 場合,可変長引数指定がなく, - ; 省略不能引数の数よりも多くの - ; 引数が指定された場合に取得される -sym-for-block-arg := "&" ; イテレータブロックを取得するための - ; 指定 --- + scan-arg-spec := param-arg-spec [option-hash-arg-spec] [block-arg-spec] + + param-arg-spec := pre-arg-spec [post-arg-spec] / post-arg-spec / + pre-opt-post-arg-spec + pre-arg-spec := num-of-leading-mandatory-args [num-of-optional-args] + post-arg-spec := sym-for-variable-length-args + [num-of-trailing-mandatory-args] + pre-opt-post-arg-spec := num-of-leading-mandatory-args num-of-optional-args + num-of-trailing-mandatory-args + option-hash-arg-spec := sym-for-option-hash-arg + block-arg-spec := sym-for-block-arg + + num-of-leading-mandatory-args := DIGIT ; 先頭に置かれる省略不能な引数の数 + num-of-optional-args := DIGIT ; 続いて置かれる省略可能な引数の数 + sym-for-variable-length-args := "*" ; 続いて置かれる可変長引数を + ; Rubyの配列で取得するための指定 + num-of-trailing-mandatory-args := DIGIT ; 終端に置かれる省略不能な引数の数 + sym-for-option-hash-arg := ":" ; オプションハッシュを取得する + ; ための指定; 省略不能な引数の + ; 数よりも多くの引数が指定され, + ; 最後の引数がハッシュ(または + ; #to_hashで変換可能)の場合に + ; 取得される.最後の引数がnilの + ; 場合,可変長引数指定がなく, + ; 省略不能引数の数よりも多くの + ; 引数が指定された場合に取得される + sym-for-block-arg := "&" ; イテレータブロックを取得するための + ; 指定 フォーマットが"12"の場合,引数は最低1つで,3つ(1+2)まで許さ れるという意味になります.従って,フォーマット文字列に続い @@ -1253,14 +1244,14 @@ sym-for-block-arg := "&" ; イテレータブロックを取得 返り値は与えられた引数の数です.オプションハッシュおよびイ テレータブロックは数えません. -** Rubyメソッド呼び出し +== Rubyメソッド呼び出し -VALUE rb_funcall(VALUE recv, ID mid, int narg, ...) +VALUE rb_funcall(VALUE recv, ID mid, int narg, ...) :: メソッド呼び出し.文字列からmidを得るためにはrb_intern()を 使う. -VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv) +VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv) :: メソッド呼び出し.引数をargc, argv形式で渡す. @@ -1268,40 +1259,39 @@ VALUE rb_eval_string(const char *str) 文字列をRubyスクリプトとしてコンパイル・実行する. -ID rb_intern(const char *name) +ID rb_intern(const char *name) :: 文字列に対応するIDを返す. -char *rb_id2name(ID id) +char *rb_id2name(ID id) :: IDに対応する文字列を返す(デバッグ用). -char *rb_class2name(VALUE klass) +char *rb_class2name(VALUE klass) :: クラスの名前を返す(デバッグ用).クラスが名前を持たない時に は, 祖先を遡って名前を持つクラスの名前を返す. -int rb_respond_to(VALUE obj, ID id) +int rb_respond_to(VALUE obj, ID id) :: objがidで示されるメソッドを持つかどうかを返す. -** インスタンス変数 +== インスタンス変数 -VALUE rb_iv_get(VALUE obj, const char *name) +VALUE rb_iv_get(VALUE obj, const char *name) :: objのインスタンス変数の値を得る.`@'で始まらないインスタン ス変数は Rubyプログラムからアクセスできない「隠れた」イン スタンス変数になる.定数は大文字の名前を持つクラス(または モジュール)のインスタンス変数として実装されている. -VALUE rb_iv_set(VALUE obj, const char *name, VALUE val) +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) +VALUE rb_block_call(VALUE obj, ID mid, int argc, VALUE * argv, VALUE (*func) (ANYARGS), VALUE data2) :: funcをブロックとして設定し,objをレシーバ,argcとargvを引数 としてmidメソッドを呼び出す.funcは第一引数にyieldされた値, @@ -1310,34 +1300,34 @@ VALUE rb_block_call(VALUE obj, ID mid, int argc, VALUE * argv, data2はArrayとしてパックされている.第三, 第四引数のargcと argvによってyieldされた値を取り出すことができる. -[OBSOLETE] VALUE rb_iterate(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) +[OBSOLETE] VALUE rb_iterate(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) :: func2をブロックとして設定し, func1をイテレータとして呼ぶ. func1には arg1が引数として渡され, func2には第1引数にイテレー タから与えられた値, 第2引数にarg2が渡される. - + 1.9でrb_iterateを使う場合は, func1の中でRubyレベルのメソッド を呼び出さなければならない. 1.9でobsoleteとなった. 代わりにrb_block_callが用意された. -VALUE rb_yield(VALUE val) +VALUE rb_yield(VALUE val) :: valを値としてイテレータブロックを呼び出す. -VALUE rb_rescue(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) +VALUE rb_rescue(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) :: 関数func1をarg1を引数に呼び出す.func1の実行中に例外が発生 した時には func2をarg2を引数として呼ぶ.戻り値は例外が発生 しなかった時はfunc1の戻り値, 例外が発生した時にはfunc2の戻 り値である. -VALUE rb_ensure(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) +VALUE rb_ensure(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2) :: 関数func1をarg1を引数として実行し, 実行終了後(たとえ例外が 発生しても) func2をarg2を引数として実行する.戻り値はfunc1 の戻り値である(例外が発生した時は戻らない). -VALUE rb_protect(VALUE (*func) (VALUE), VALUE arg, int *state) +VALUE rb_protect(VALUE (*func) (VALUE), VALUE arg, int *state) :: 関数funcをargを引数として実行し, 例外が発生しなければその戻 り値を返す.例外が発生した場合は, *stateに非0をセットして @@ -1345,225 +1335,226 @@ VALUE rb_protect(VALUE (*func) (VALUE), VALUE arg, int *state) rb_jump_tag()を呼ばずに捕捉した例外を無視する場合には, rb_set_errinfo(Qnil)でエラー情報をクリアしなければならない. -void rb_jump_tag(int state) +void rb_jump_tag(int state) :: rb_protect()やrb_eval_string_protect()で捕捉された例外を再 送する.stateはそれらの関数から返された値でなければならない. この関数は直接の呼び出し元に戻らない. -void rb_iter_break() +void rb_iter_break() :: 現在の最も内側のブロックを終了する.この関数は直接の呼び出 し元に戻らない. -void rb_iter_break_value(VALUE value) +void rb_iter_break_value(VALUE value) :: 現在の最も内側のブロックをvalueで終了する.ブロックは引数で 与えられたvalueを返す.この関数は直接の呼び出し元に戻らない. -** 例外・エラー +== 例外・エラー -void rb_warning(const char *fmt, ...) +void rb_warning(const char *fmt, ...) :: rb_verbose時に標準エラー出力に警告情報を表示する.引数は printf()と同じ. -void rb_raise(rb_eRuntimeError, const char *fmt, ...) +void rb_raise(rb_eRuntimeError, const char *fmt, ...) :: RuntimeError例外を発生させる.引数はprintf()と同じ. -void rb_raise(VALUE exception, const char *fmt, ...) +void rb_raise(VALUE exception, const char *fmt, ...) :: exceptionで指定した例外を発生させる.fmt以下の引数は printf()と同じ. -void rb_fatal(const char *fmt, ...) +void rb_fatal(const char *fmt, ...) :: 致命的例外を発生させる.通常の例外処理は行なわれず, インター プリタが終了する(ただしensureで指定されたコードは終了前に 実行される). -void rb_bug(const char *fmt, ...) +void rb_bug(const char *fmt, ...) :: インタープリタなどプログラムのバグでしか発生するはずのない 状況の時呼ぶ.インタープリタはコアダンプし直ちに終了する. 例外処理は一切行なわれない. -** Rubyの初期化・実行 +== Rubyの初期化・実行 Rubyをアプリケーションに埋め込む場合には以下のインタフェース を使う.通常の拡張ライブラリには必要ない. -void ruby_init() +void ruby_init() :: Rubyインタプリタの初期化を行なう. -void ruby_options(int argc, char **argv) +void ruby_options(int argc, char **argv) :: Rubyインタプリタのコマンドライン引数の処理を行なう. -void ruby_run() +void ruby_run() :: Rubyインタプリタを実行する. -void ruby_script(char *name) +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) + void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, + VALUE data) 指定されたインタプリタのイベントに対するフック関数を追加します. eventsは以下の値のorでなければなりません: - RUBY_EVENT_LINE - RUBY_EVENT_CLASS - RUBY_EVENT_END - RUBY_EVENT_CALL - RUBY_EVENT_RETURN - RUBY_EVENT_C_CALL - RUBY_EVENT_C_RETURN - RUBY_EVENT_RAISE - RUBY_EVENT_ALL + RUBY_EVENT_LINE + RUBY_EVENT_CLASS + RUBY_EVENT_END + RUBY_EVENT_CALL + RUBY_EVENT_RETURN + RUBY_EVENT_C_CALL + RUBY_EVENT_C_RETURN + RUBY_EVENT_RAISE + RUBY_EVENT_ALL rb_event_hook_func_tの定義は以下の通りです: - typedef void (*rb_event_hook_func_t)(rb_event_t event, VALUE data, - VALUE self, ID id, VALUE klass) + typedef void (*rb_event_hook_func_t)(rb_event_t event, VALUE data, + VALUE self, ID id, VALUE klass) rb_add_event_hook() の第3引数 data は,フック関数の第2引数と して渡されます.これは1.8では現在のNODEへのポインタでした.以 下の RB_EVENT_HOOKS_HAVE_CALLBACK_DATA も参照してください. - int rb_remove_event_hook(rb_event_hook_func_t func) + int rb_remove_event_hook(rb_event_hook_func_t func) 指定されたフック関数を削除します. -** 互換性のためのマクロ +== 互換性のためのマクロ APIの互換性をチェックするために以下のマクロがデフォルトで定義されています. -NORETURN_STYLE_NEW +NORETURN_STYLE_NEW :: NORETURN マクロが関数型マクロとして定義されていることを意味する. -HAVE_RB_DEFINE_ALLOC_FUNC +HAVE_RB_DEFINE_ALLOC_FUNC :: rb_define_alloc_func() 関数が提供されていること,つまり allocation framework が使われることを意味する. have_func("rb_define_alloc_func", "ruby.h") の結果と同じ. -HAVE_RB_REG_NEW_STR +HAVE_RB_REG_NEW_STR :: StringオブジェクトからRegexpオブジェクトを作る rb_reg_new_str() 関数が提供されていることを意味する. have_func("rb_reg_new_str", "ruby.h"). の結果と同じ. -HAVE_RB_IO_T +HAVE_RB_IO_T :: rb_io_t 型が提供されていることを意味する. -USE_SYMBOL_AS_METHOD_NAME +USE_SYMBOL_AS_METHOD_NAME :: メソッド名を返すメソッド,Module#methods, #singleton_methods などがSymbolを返すことを意味する. -HAVE_RUBY_*_H +HAVE_RUBY_*_H :: ruby.h で定義されている.対応するヘッダが提供されていること を意味する.たとえば,HAVE_RUBY_ST_H が定義されている場合は 単なる st.h ではなく ruby/st.h を使用する. -RB_EVENT_HOOKS_HAVE_CALLBACK_DATA +RB_EVENT_HOOKS_HAVE_CALLBACK_DATA :: rb_add_event_hook() がフック関数に渡す data を第3引数として 受け取ることを意味する. -Appendix C. extconf.rbで使える関数たち += Appendix C. extconf.rbで使える関数たち extconf.rbの中では利用可能なコンパイル条件チェックの関数は以 下の通りである. -have_macro(macro, headers) +have_macro(macro, headers) :: ヘッダファイルheaderをインクルードしてマクロmacroが定義さ れているかどうかチェックする.マクロが定義されている時true を返す. -have_library(lib, func) +have_library(lib, func) :: 関数funcを定義しているライブラリlibの存在をチェックする. ライブラリが存在する時,trueを返す. -find_library(lib, func, path...) +find_library(lib, func, path...) :: 関数funcを定義しているライブラリlibの存在を -Lpath を追加 しながらチェックする.ライブラリが見付かった時,trueを返す. -have_func(func, header) +have_func(func, header) :: ヘッダファイルheaderをインクルードして関数funcの存在をチェ ックする.funcが標準ではリンクされないライブラリ内のもので ある時には先にhave_libraryでそのライブラリをチェックしてお く事.関数が存在する時trueを返す. -have_var(var, header) +have_var(var, header) :: ヘッダファイルheaderをインクルードして変数varの存在をチェッ クする.varが標準ではリンクされないライブラリ内のものであ る時には先にhave_libraryでそのライブラリをチェックしておく 事.変数が存在する時trueを返す. -have_header(header) +have_header(header) :: ヘッダファイルの存在をチェックする.ヘッダファイルが存在す る時trueを返す. -find_header(header, path...) +find_header(header, path...) :: ヘッダファイルheaderの存在を -Ipath を追加しながらチェック する.ヘッダファイルが見付かった時,trueを返す. -have_struct_member(type, member[, header[, opt]]) +have_struct_member(type, member[, header[, opt]]) :: ヘッダファイルheaderをインクルードして型typeにメンバmember が存在するかをチェックする.typeが定義されていて,memberを 持つする時trueを返す. -have_type(type, header, opt) +have_type(type, header, opt) :: ヘッダファイルheaderをインクルードして型typeが存在するかを チェックする.typeが定義されている時trueを返す. -check_sizeof(type, header) +check_sizeof(type, header) :: ヘッダファイルheaderをインクルードして型typeのchar単位サイ ズを調べる.typeが定義されている時そのサイズを返す.定義さ れていないときはnilを返す. -create_makefile(target[, target_prefix]) +create_makefile(target[, target_prefix]) :: 拡張ライブラリ用のMakefileを生成する.この関数を呼ばなけれ ばそのライブラリはコンパイルされない.targetはモジュール名 を表す. -find_executable(command, path) +find_executable(command, path) :: コマンドcommandをFile::PATH_SEPARATORで区切られたパス名の リストpathから探す.pathがnilまたは省略された場合は,環境 変数PATHの値を使用する.実行可能なコマンドが見つかった場合 はパスを含むファイル名,見つからなかった場合はnilを返す. -with_config(withval[, default=nil]) +with_config(withval[, default=nil]) :: コマンドライン上の--with-で指定されたオプション値 を得る. -enable_config(config, *defaults) -disable_config(config, *defaults) +enable_config(config, *defaults) :: +disable_config(config, *defaults) :: コマンドライン上の--enable-または --disable-で指定された真偽値を得る. @@ -1573,8 +1564,8 @@ disable_config(config, *defaults) いる場合は*defaultsをyieldした結果,ブロックなしなら *defaultsを返す. -dir_config(target[, default_dir]) -dir_config(target[, default_include, default_lib]) +dir_config(target[, default_dir]) :: +dir_config(target[, default_include, default_lib]) :: コマンドライン上の--with--dir, --with--include, --with--libのいずれかで指定されるディレクトリを @@ -1583,7 +1574,7 @@ dir_config(target[, default_include, default_lib]) と等価である.追加された include ディレクトリと lib ディレ クトリの配列を返す. ([include_dir, lib_dir]) -pkg_config(pkg) +pkg_config(pkg) :: pkg-configコマンドからパッケージpkgの情報を得る. pkg-configの実際のコマンド名は,--with-pkg-configコマンド -- cgit v1.2.3