.\" README.EXT - -*- Text -*- created at: Mon Aug 7 16:45:54 JST 1995 This document explains how to make extention modules for Ruby. 1��Basic knowledge In C, variables have types and data do not have types. In contrast, Ruby variables do not have static type and data themselves have types. So, data need to be converted across the languages. Data in Ruby represented C type `VALUE'. Each VALUE data have its data-type. To retrieve an C data from the VALUE, you need to: (1) Identify VALUE's data type (2) Convert VALUE into C data Converting to wrong data type may cause serious promblems. 1.1 Data-types Ruby interpreter has data-types as below: T_NIL nil T_OBJECT ordinaly object T_CLASS class T_MODULE module T_FLOAT floating point number T_STRING string T_REGEXP regular expression T_ARRAY array T_FIXNUM Fixnum(31bit integer) T_HASH assosiative array T_STRUCT (Ruby) structure T_BIGNUM multi precision integer T_TRUE true T_FALSE false T_DATA data Otherwise, there are several other types used internally: T_ICLASS T_MATCH T_VARMAP T_SCOPE T_NODE Most of the types are represented by C structures. 1.2 Check Data Type of the VALUE The macro TYPE() defined in ruby.h shows data-type of the VALUE. TYPE() returns the constant number T_XXXX described above. To handle data-types, the code will be like: switch (TYPE(obj)) { case T_FIXNUM: /* process Fixnum */ break; case T_STRING: /* process String */ break; case T_ARRAY: /* process Array */ break; default: /* raise exception */ Fail("not valid value"); break; } There is the data-type check function. void Check_Type(VALUE value, int type) It raises an exception, if the VALUE does not have the type specified. There are faster check-macros for fixnums and nil. FIXNUM_P(obj) NIL_P(obj) 1.3 Convert VALUE into C data The data for type T_NIL, T_FALSE, T_TRUE are nil, true, false respectively. They are singletons for the data type. The T_FIXNUM data is the 31bit length fixed integer (63bit length on some machines), which can be conver to the C integer by using FIX2INT() macro. There also be NUM2INT() which converts any Ruby numbers into C integer. The NUM2INT() macro includes type check, so the exception will be raised if conversion failed. Other data types have corresponding C structures, e.g. struct RArray for T_ARRAY etc. VALUE of the type which has corresponding structure can be cast to retrieve the pointer to the struct. The casting macro RXXXX for each data type like RARRAY(obj). see "ruby.h". For example, `RSTRING(size)->len' is the way to get the size of the Ruby String object. The allocated region can be accessed by `RSTRING(str)->ptr'. For arrays, `RARRAY(ary)->len' and `RARRAY(ary)->ptr' respectively. Notice: Do not change the value of the structure directly, unless you are responsible about the result. It will be the cause of interesting bugs. 1.4 Convert C data into VALUE VALUE�μºݤι�¤�� * FIXNUM�ξ�� 1bit�����եȤ��ơ�LSB��Ω�Ƥ롥 * ����¾�Υݥ��󥿤ξ�� ���Τޤ�VALUE�˥��㥹�Ȥ��롥 �ȤʤäƤ��ޤ�����äơ�LSB������å������VALUE��FIXNUM���� �����狼��櫓�Ǥ�(�ݥ��󥿤�LSB��Ω�äƤ��ʤ����Ȥ��ꤷ�� ����)�� �Ǥ����顤FIXNUM�ʳ���Ruby�Υ��֥������Ȥι�¤�Τ�ñ��VALUE �˥��㥹�Ȥ��������VALUE���Ѵ�����ޤ�����������Ǥ�դι�¤ �Τ�VALUE�˥��㥹�Ƚ����櫓�ǤϤ���ޤ��󡥥��㥹�Ȥ���� ��Ruby���ΤäƤ��빽¤��(ruby.h���������Ƥ���struct RXxxx �Τ��)�����ˤ��Ƥ����Ƥ��������� FIXNUM�˴ؤ��Ƥ��Ѵ��ޥ������ͳ����ɬ�פ�����ޤ���C������ ����VALUE���Ѵ�����ޥ����ϰʲ��Τ�Τ�����ޤ���ɬ�פ˱��� �ƻȤ�ʬ���Ƥ��������� INT2FIX() ��Ȥ�������31bit����˼��ޤ�� INT2NUM() Ǥ�դ���������VALUE�� INT2NUM()��������FIXNUM���ϰϤ˼��ޤ�ʤ���硤Bignum���Ѵ� ���Ƥ���ޤ�(���������٤�)�� 1.5 Manipulate Ruby data As I already told, it is not recommended to modify object's internal structure. To manipulate objects, use functions supplied by Ruby interpreter. Useful functions are listed below (not all): String funtions rb_str_new(char *ptr, int len) Creates a new Ruby string. rb_str_new2(char *ptr) Creates a new Ruby string from C string. This is equivalent to rb_str_new(ptr, strlen(ptr)). rb_str_cat(VALUE str, char *ptr, int len) Appends len bytes data from ptr to the Ruby string. Array functions rb_ary_new() Creates an array with no element. rb_ary_new2(int len) Creates an array with no element, with allocating internal buffer for len elements. rb_ary_new3(int n, ...) Creates an n-elements array from arguments. rb_ary_new4(int n, VALUE *elts) Creates an n-elements array from C array. 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_entry(VALUE ary, int idx) Array operations. The first argument to each functions must be an array. They may dump core if other types given. 2. Extend Ruby with C ����Ū��Ruby�ǽ񤱤뤳�Ȥ�C�Ǥ�񤱤ޤ���Ruby���Τ�Τ�C�ǵ� �Ҥ���Ƥ����Ǥ����顤�����Ȥ����������ʤ�Ǥ����ɡ������� ��Ruby�γ�ĥ�˻Ȥ����Ȥ�¿����������ͽ¬����뵡ǽ���濴�˾� �𤷤ޤ��� 2.1 Add new features to Ruby Ruby���󶡤���Ƥ���ؿ���Ȥ���Ruby���󥿥ץ꥿�˿�������ǽ ���ɲä��뤳�Ȥ��Ǥ��ޤ���Ruby�Ǥϰʲ��ε�ǽ���ɲä���ؿ��� �󶡤���Ƥ��ޤ��� * Classes, Modules * Methods, Singleton Methods * Constants �ǤϽ�˾Ҳ𤷤ޤ��� 2.1.1 Class/module definition To define class or module, use functions below: VALUE rb_define_class(char *name, VALUE super) VALUE rb_define_module(char *name) These functions return the newly created class ot module. You may want to save this reference into the variable to use later. 2.1.2 Method/singleton method definition To define methods or singleton methods, use functions below: void rb_define_method(VALUE class, char *name, VALUE (*func)(), int argc) void rb_define_singleton_method(VALUE object, char *name, VALUE (*func)(), int argc) The `argc' represents the number of the arguments to the C function, which must be less than 17. But I believe you don't need that much. :-) If `argc' is negative, it specifies calling sequence, not number of the arguments. If argc is -1, the function will be called like: VALUE func(int argc, VALUE *argv, VALUE obj) where argc is the actual number of arguments, argv is the C array of the arguments, and obj is the receiver. if argc is -2, the arguments are passed in Ruby array. The function will be called like: VALUE func(VALUE obj, VALUE args) where obj is the receiver, and args is the Ruby array containing actual arguments. There're two more functions to define method. One is to define private method: void rb_define_private_method(VALUE class, char *name, VALUE (*func)(), int argc) The other is to define module function, which is private AND singleton method of the module. For example, sqrt is the module function defined in Math module. It can be call in the form like: Math.sqrt(4) or include Math sqrt(4) To define module function void rb_define_module_function(VALUE module, char *name, VALUE (*func)(), int argc) Oh, in addition, function-like method, which is private method defined in Kernel module, can be defined using: void rb_define_global_function(char *name, VALUE (*func)(), int argc) 2.1.3 Constant definition We have 2 functions to define constants: void rb_define_const(VALUE class, char *name, VALUE val) void rb_define_global_const(char *name, VALUE val) The former is to define constant under specified class/module. The latter is to define global constant. 2.2 Use Ruby features from C There are several ways to invoke Ruby's features from C code. 2.2.1 Evaluate Ruby Program in String Easiest way to call Ruby's function from C program is to evaluate the string as Ruby program. This function will do the job. VALUE rb_eval_string(char *str) Evaluation is done under current context, thus current local variables of the innermost method (which is defined by Ruby) can be accessed. 2.2.2 ID or Symbol You can invoke methods directly, without parsing the string. First I need to explain about symbols (which data type is ID). ID is the integer number to represent Ruby's identifiers such as variable names. It can be accessed from Ruby in the form like: :Identifier You can get the symbol value from string within C code, by using rb_intern(char *name) In addition, the symbols for one character operators (e.g +) is the code for that character. 2.2.3 Invoke Ruby method from C To invoke methods directly, you can use the function below VALUE rb_funcall(VALUE recv, ID mid, int argc, ...) This function invokes the method of the recv, which name is specified by the symbol mid. 2.2.4 Accessing the variables and constants C����ؿ���Ȥäƻ��ȡ������Ǥ���Τϡ����饹��������󥹥� ���ѿ��Ǥ�������ѿ��ϰ����Τ�Τ�C������ѿ��Ȥ��ƥ����� ���Ǥ��ޤ������������ѿ��򻲾Ȥ�����ˡ�ϸ������Ƥ��ޤ��� The functions to access/modify instance variables are below: VALUE rb_ivar_get(VALUE obj, ID id) VALUE rb_ivar_set(VALUE obj, ID id, VALUE val) id must be the symbol, which can be retrieved by rb_intern(). To access the constants of the class/module: VALUE rb_const_get(VALUE obj, ID id) See 2.1.3 for defining new constant. 3. Informatin sharing between Ruby and C C�����Ruby�δ֤Ǿ����ͭ������ˡ�ˤĤ��Ʋ��⤷�ޤ��� 3.1 Ruby constant that C���黲�ȤǤ���Ruby����� Following Ruby constants can be referred from C. Qtrue Qfalse Boolean values. Qfalse is false in the C also (i.e. 0). Qnil Ruby nil in C scope. 3.2 Global variables shared between C and Ruby C��Ruby������ѿ���Ȥäƾ����ͭ�Ǥ��ޤ�����ͭ�Ǥ������ �ѿ��ˤϤ����Ĥ��μ��ब����ޤ������Τʤ��Ǥ�äȤ��ɤ��Ȥ� ���Ȼפ���Τ�rb_define_variable()�Ǥ��� void rb_define_variable(char *name, VALUE *var) ���δؿ���Ruby��C�ȤǶ�ͭ��������ѿ���������ޤ����ѿ�̾�� `$'�ǻϤޤ�ʤ����ˤϼ�ưŪ���ɲä���ޤ��������ѿ����ͤ��� ������ȼ�ưŪ��Ruby���б������ѿ����ͤ��Ѥ��ޤ��� �ޤ�Ruby¦����Ϲ����Ǥ��ʤ��ѿ��⤢��ޤ�������read only�� �ѿ��ϰʲ��δؿ���������ޤ��� void rb_define_readonly_variable(char *name, VALUE *var) ������ѿ���¾��hook��Ĥ�������ѿ�������Ǥ��ޤ���hook�դ� ������ѿ��ϰʲ��δؿ����Ѥ���������ޤ���hook�դ�����ѿ��� �ͤλ��Ȥ������hook�ǹԤ�ɬ�פ�����ޤ��� void rb_define_hooked_variable(char *name, VALUE *var, VALUE (*getter)(), VALUE (*setter)()) ���δؿ���C�δؿ��ˤ�ä�hook�ΤĤ���줿����ѿ���������� �����ѿ������Ȥ��줿���ˤϴؿ�getter�����ѿ����ͤ����åȤ��� �����ˤϴؿ�setter���ƤФ�롥hook����ꤷ�ʤ�����getter�� setter��0����ꤷ�ޤ��� # getter��setter��0�ʤ��rb_define_variable()��Ʊ���ˤʤ롥 ���줫�顤C�δؿ��ˤ�äƼ¸������Ruby������ѿ���������� �ؿ�������ޤ��� void rb_define_virtual_variable(char *name, VALUE (*getter)(), VALUE (*setter)()) ���δؿ��ˤ�ä�������줿Ruby������ѿ������Ȥ��줿���ˤ� getter�����ѿ����ͤ����åȤ��줿���ˤ�setter���ƤФ�ޤ��� The prototypes of the getter and setter functions are as following: (*getter)(ID id, void *data, struct global_entry* entry); (*setter)(VALUE val, ID id, void *data, struct global_entry* entry); 3.3 Encapsulate C data into Ruby object C��������������줿�ǡ���(��¤��)��Ruby�Υ��֥������ȤȤ��� ��갷��������礬���ꤨ�ޤ������Τ褦�ʾ��ˤϡ�Data�Ȥ��� Ruby���֥������Ȥ�C�ι�¤��(�ؤΥݥ���)�򤯤�ळ�Ȥ�Ruby ���֥������ȤȤ��Ƽ�갷����褦�ˤʤ�ޤ��� Data���֥������Ȥ��������ƹ�¤�Τ�Ruby���֥������Ȥ˥��ץ��� �����뤿��ˤϡ��ʲ��Υޥ�����Ȥ��ޤ��� Data_Wrap_Struct(class,mark,free,ptr) ���Υޥ���������ͤ��������줿Data���֥������ȤǤ��� class�Ϥ���Data���֥������ȤΥ��饹�Ǥ���ptr�ϥ��ץ��벽���� C�ι�¤�ΤؤΥݥ��󥿤Ǥ���mark�Ϥ��ι�¤�Τ�Ruby�Υ��֥��� ���Ȥؤλ��Ȥ�������˻Ȥ��ؿ��Ǥ������Τ褦�ʻ��Ȥ�ޤޤʤ� ���ˤ�0����ꤷ�ޤ��� # ���Τ褦�ʻ��Ȥϴ�����ޤ��� free�Ϥ��ι�¤�Τ��⤦���פˤʤä����˸ƤФ��ؿ��Ǥ������� �ؿ��������١������쥯������ƤФ�ޤ��� C�ι�¤�Τγ�����Data���֥������Ȥ�������Ʊ���˹Ԥ��ޥ����� ���ưʲ��Τ�Τ��󶡤���Ƥ��ޤ��� Data_Make_Struct(class, type, mark, free, sval) ���Υޥ���������ͤ��������줿Data���֥������ȤǤ��� class, mark, free��Data_Wrap_Struct��Ʊ��Ư���򤷤ޤ���type �ϳ�����Ƥ�C��¤�Τη��Ǥ���������Ƥ�줿��¤�Τ��ѿ�sval ����������ޤ��������ѿ��η��� (type*) �Ǥ���ɬ�פ�����ޤ��� Data���֥������Ȥ���ݥ��󥿤���Ф��Τϰʲ��Υޥ������Ѥ� �ޤ��� Data_Get_Struct(obj, type, sval) C�ι�¤�ΤؤΥݥ��󥿤��ѿ�sval����������ޤ��� ������Data�λȤ����Ϥ���ä�ʬ����ˤ����Τǡ������������ ����򻲾Ȥ��Ƥ��������� 4��Example - Create dbm module �����ޤǤ������ǤȤꤢ������ĥ�饤�֥��Ϻ���Ϥ��Ǥ��� Ruby��ext�ǥ��쥯�ȥ�ˤ��Ǥ˴ޤޤ�Ƥ���dbm�⥸�塼������ �����ʳ�Ū���������ޤ��� (1) make the directory % mkdir ext/dbm Make a directory for the extension library under ext directory. (2) create MANIFEST file % cd ext/dbm % touch MANIFEST There should be MANIFEST file in the directory for the extension library. Make empty file now. (3) design the library You need to design the library features, before making it. (4) write C code. ��ĥ�饤�֥�����ΤȤʤ�C����Υ�������񤭤ޤ���C����Υ��� �����ҤȤĤλ��ˤϡ֥⥸�塼��̾.c�פ����֤��ɤ��Ǥ��礦��C ����Υ�������ʣ���ξ��ˤϵդˡ֥⥸�塼��̾.c�פȤ����ե� ����̾���򤱤�ɬ�פ�����ޤ������֥������ȥե�����ȥ⥸�塼 �������������Ū�����������֥⥸�塼��̾.o�פȤ����ե����� �Ȥ����ͤ��뤫��Ǥ��� Ruby�ϳ�ĥ�饤�֥�������ɤ�����ˡ�Init_�⥸�塼��̾�פ� �����ؿ���ưŪ�˼¹Ԥ��ޤ���dbm�⥸�塼��ξ���Init_dbm�� �Ǥ������δؿ�����ǥ��饹���⥸�塼�롤�᥽�åɡ�����ʤɤ� �����Ԥ��ޤ���dbm.c����������Ѥ��ޤ��� -- Init_dbm() { /* 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); : } -- DBM�⥸�塼���dbm�Υǡ������б����륪�֥������Ȥˤʤ�Ϥ��� �����顤C��������dbm��Ruby�������˼�����ɬ�פ�����ޤ��� dbm.c�Ǥ�Data_Make_Struct��ʲ��Τ褦�˻ȤäƤ��ޤ��� -- struct dbmdata { int di_size; DBM *di_dbm; }; obj = Data_Make_Struct(class,struct dbmdata,0,free_dbm,dbmp); -- �����Ǥ�dbmstruct��¤�ΤؤΥݥ��󥿤�Data�˥��ץ��벽���Ƥ� �ޤ���DBM*��ľ�ܥ��ץ��벽���ʤ��Τ�close()�������ν������ ���ƤΤ��ȤǤ��� Data���֥������Ȥ���dbmstruct��¤�ΤΥݥ��󥿤���Ф����� �˰ʲ��Υޥ�����ȤäƤ��ޤ��� -- #define GetDBM(obj, dbmp) {\ Data_Get_Struct(obj, struct dbmdata, dbmp);\ if (dbmp->di_dbm == 0) closed_dbm();\ } -- ����ä�ʣ���ʥޥ����Ǥ������פ����dbmdata��¤�ΤΥݥ��� �μ��Ф��ȡ�close����Ƥ��뤫�ɤ����Υ����å���ޤȤ�Ƥ� ������Ǥ��� DBM���饹�ˤϤ�������᥽�åɤ�����ޤ�����ʬ�ह���3����� �����μ�����������ޤ����ҤȤĤϰ����ο�������Τ�Τǡ���� ���Ƥ�delete�᥽�åɤ�����ޤ���delete�᥽�åɤ�������Ƥ��� fdbm_delete()�Ϥ��Τ褦�ˤʤäƤ��ޤ��� -- static VALUE fdbm_delete(obj, keystr) VALUE obj, keystr; { : } -- �����ο�������Υ����פ���1������self����2�����ʹߤ��᥽�å� �ΰ����Ȥʤ�ޤ��� �����ο�������Τ�Τ�C������Ǽ������Τ�Ruby������Ǽ��� ���ΤȤ�����ޤ���dbm�⥸�塼�����ǡ�C������Ǽ������� ��DBM�Υ��饹�᥽�åɤǤ���open()�Ǥ��������������Ƥ���� ��fdbm_s_open()�Ϥ����ʤäƤ��ޤ��� -- static VALUE fdbm_s_open(argc, argv, class) int argc; VALUE *argv; VALUE class; { : if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) { mode = 0666; /* default value */ } : } -- ���Υ����פδؿ�����1������Ϳ����줿�����ο�����2������Ϳ�� ��줿���������äƤ�������ˤʤ�ޤ���self����3�����Ȥ���Ϳ �����ޤ��� ���������Ϳ����줿��������Ϥ��뤿��δؿ���open()�Ǥ�Ȥ� ��Ƥ���rb_scan_args()�Ǥ�����3�����˻��ꤷ���ե����ޥåȤ� ��������4�ѿ��ʹߤ˻��ꤷ���ѿ����ͤ��������Ƥ���ޤ������� �ե����ޥåȤϡ���1ʸ���ܤ���ά�Ǥ��ʤ������ο�����2ʸ���ܤ� ��ά�Ǥ�������ο�����3ʸ���ܤ��б�������̵꤬�����ޤ�ΰ� �������뤫�ɤ����򼨤�"*"�Ǥ���2ʸ���ܤ�3ʸ���ܤϾ�ά�Ǥ��� ����dbm.c����Ǥϡ��ե����ޥåȤ�"11"�Ǥ����顤�����Ϻ���1�� �ǡ�2�Ĥޤǵ������Ȥ�����̣�ˤʤ�ޤ�����ά����Ƥ������ �ѿ����ͤ�nil(C����Υ�٥�Ǥ�Qnil)�ˤʤ�ޤ��� Ruby������ǰ������������Τ�indexes������ޤ��������Ϥ� ���Ǥ��� -- static VALUE fdbm_indexes(obj, args) VALUE obj; struct RArray *args; { : } -- ��1������self����2������Ruby������Ǥ��������Ǥϥ��㥹�Ȥ� �餹���� struct RArray* �Ǽ����Ƥ��ޤ�����VALUE�Ǥ�Ʊ������ �Ǥ��� ** Notice GC should know about global variables which refers Ruby's objects, but not exported to the Ruby world. You need to protect them by void rb_global_variable(VALUE *var) (5) prepare extconf.rb �⤷�ǥ��쥯�ȥ�ˡ�extconf.rb�פȤ����ե����뤬¸�ߤ���С� make���˼¹Ԥ���ޤ����ʤ����Ŭ����Makefile����������ޤ��� extconf.rb�ϥ⥸�塼��Υ���ѥ����ɬ�פʾ��Υ����å��ʤ� ��Ԥ����Ȥ���Ū�Ǥ���extconf.rb����Ǥϰʲ���Ruby�ؿ���Ȥ� ���Ȥ�����ޤ��� have_library(lib, func): �饤�֥���¸�ߥ����å� have_func(func): �ؿ���¸�ߥ����å� have_header(header): �إå��ե������¸�ߥ����å� create_makefile(target): Makefile������ �ʲ����ѿ���Ȥ����Ȥ��Ǥ��ޤ��� $CFLAGS: ����ѥ�������ɲ�Ū�˻��ꤹ��ե饰(-I�ʤ�) $LDFLAGS: ��󥯻����ɲ�Ū�˻��ꤹ��ե饰(-L�ʤ�) �⥸�塼��򥳥�ѥ��뤹���郎·��ʤ������Υ⥸�塼��ϥ� ��ѥ��뤷�ʤ����ˤ�create_makefile��ƤФʤ����Makefile�� �������줺������ѥ����Ԥ��ޤ��� (6) prepare depend (optional) �⤷���ǥ��쥯�ȥ��depend�Ȥ����ե����뤬¸�ߤ���С� Makefile����¸�ط�������å����Ƥ���ޤ��� % gcc -MM *.c > depend �ʤɤǺ�뤳�Ȥ�����ޤ������ä�»��̵���Ǥ��礦�� (7) MANIFEST�ե�����˥ե�����̾������� % ls > MANIFEST % vi MANIFEST *.o, *~�ʤ���ɬ�פʥե�����ʳ���MANIFEST���ɲä��Ƥ����ޤ��� make���ˤ�MANIFEST�����Ƥϻ��Ȥ��ޤ���Τǡ����ΤޤޤǤ����� �ϵ����ޤ��󤬡��ѥå������󥰤λ��˻��Ȥ��뤳�Ȥ�����Τȡ� ɬ�פʥե��������̤Ǥ���Τǡ��Ѱդ��Ƥ����������ɤ��Ǥ��� ���� (8) make Ruby�Υǥ��쥯�ȥ��make��¹Ԥ����Makefile����������make�� ɬ�פˤ�äƤϤ��Υ⥸�塼���Ruby�ؤΥ�󥯤ޤǼ�ưŪ�˼¹� ���Ƥ���ޤ���extconf.rb��񤭴�����ʤɤ���Makefile�κ����� ��ɬ�פʻ��Ϥޤ�Ruby�ǥ��쥯�ȥ��make���Ƥ��������� (9) debug You may need to rb_debug the module. The modules can be linked statically by adding directory name in the ext/Setup file, so that you can inspect the module by the debugger. (10) done, now you have the extension library ��Ϥ��ä���Ȥ��ʤꡤ������������ʤꡤ���ʤꡤ����ͳ�ˤ� �Ȥ�����������Ruby�κ�Ԥϳ�ĥ�饤�֥��˴ؤ��ư��ڤθ����� ��ĥ���ޤ��� Appendix A. Ruby�Υ����������ɤ�ʬ�� Ruby�Υ������Ϥ����Ĥ���ʬ�ह�뤳�Ȥ�����ޤ������Τ������� ���饤�֥�����ʬ�ϴ���Ū�˳�ĥ�饤�֥���Ʊ��������ˤʤ� �Ƥ��ޤ��������Υ������Ϻ��ޤǤ������ǤۤȤ������Ǥ���� �פ��ޤ��� ruby language core class.c error.c eval.c gc.c object.c parse.y variable.c utility functions dln.c fnmatch.c glob.c regex.c st.c util.c ruby interpreter implementation dmyext.c inits.c main.c ruby.c version.c class library array.c bignum.c compar.c dir.c enum.c file.c hash.c io.c math.c numeric.c pack.c process.c random.c range.c re.c signal.c sprintf.c string.c struct.c time.c Appendix B. ��ĥ�Ѵؿ���ե���� C���줫��Ruby�ε�ǽ�����Ѥ���API�ϰʲ����̤�Ǥ��롥 ** �� VALUE Ruby���֥������Ȥ�ɽ�����뷿��ɬ�פ˱����ƥ��㥹�Ȥ����Ѥ��롥 �Ȥ߹��߷���ɽ������C�η���ruby.h�˵��Ҥ��Ƥ���R�ǻϤޤ빽¤ �ΤǤ��롥VALUE���򤳤��˥��㥹�Ȥ��뤿���R�ǻϤޤ빽¤�� ̾��������ʸ���ˤ���̾���Υޥ������Ѱդ���Ƥ��롥 ** Variables and constants Qnil const: nil object Qtrue const: Qtrue object(default true value) Qfalse const: Qfalse object ** C�ǡ����Υ��ץ��벽 Data_Wrap_Struct(VALUE class, void (*mark)(), void (*free)(), void *sval) C��Ǥ�դΥݥ��󥿤򥫥ץ��벽����Ruby���֥������Ȥ��֤����� �Υݥ��󥿤�Ruby���饢����������ʤ��ʤä�����free�ǻ��ꤷ�� �ؿ����ƤФ�롥�ޤ������Υݥ��󥿤λؤ��ǡ�����¾��Ruby���� �������Ȥ�ؤ��Ƥ����硤mark�˻��ꤹ��ؿ��ǥޡ�������ɬ�� �����롥 Data_Make_Struct(class, type, mark, free, sval) type���Υ����malloc�����ѿ�sval�����������塤����򥫥ץ� �벽�����ǡ������֤��ޥ����� Data_Get_Struct(data, type, sval) data����type���Υݥ��󥿤���Ф��ѿ�sval����������ޥ����� ** ���饹/�⥸�塼����� VALUE rb_define_class(char *name, VALUE super) super�Υ��֥��饹�Ȥ��ƿ�����Ruby���饹��������롥 VALUE rb_define_class_under(VALUE module, char *name, VALUE super) super�Υ��֥��饹�Ȥ��ƿ�����Ruby���饹���������module���� ���Ȥ���������롥 VALUE rb_define_module(char *name) ������Ruby�⥸�塼���������롥 VALUE rb_define_module_under(VALUE module, char *name, VALUE super) ������Ruby�⥸�塼����������module������Ȥ���������롥 void rb_include_module(VALUE class, VALUE module) �⥸�塼��򥤥󥯥롼�ɤ��롥class�����Ǥ�module�򥤥󥯥롼 �ɤ��Ƥ�����ˤϲ��⤷�ʤ�(¿�ť��󥯥롼�ɤζػ�)�� void rb_extend_object(VALUE object, VALUE module) ���֥������Ȥ�⥸�塼��(���������Ƥ���᥽�å�)�dz�ĥ���롥 ** Defining Global Variables void rb_define_variable(char *name, VALUE *var) Defines a global variable which is shared between C and Ruby. If name contains the character which is not allowed to be part of the symbol, it can't be seen from Ruby programs. void rb_define_readonly_variable(char *name, VALUE *var) Defines a read-only global variable. Works just like rb_define_variable(), except defined variable is read-only. void rb_define_virtual_variable(char *name, VALUE (*getter)(), VALUE (*setter)()) Defines a virtual variable, whose behavior is defined by pair of C functions. The getter function is called when the variable is referred. The setter function is called when the value is set to the variable. The prototype for getter/setter functions are: VALUE getter(ID id) void setter(VALUE val, ID id) The getter function must return the value for the access. void rb_define_hooked_variable(char *name, VALUE *var, VALUE (*getter)(), VALUE (*setter)()) Defines hooked variable. It's virtual variable with C variable. The getter is called as VALUE getter(ID id, VALUE *var) returning new value. The setter is called as void setter(VALUE val, ID id, VALUE *var) GC requires to mark the C global variables which hold Ruby values. void rb_global_variable(VALUE *var) Tells GC to protect these variables. ** Constant Definition void rb_define_const(VALUE klass, char *name, VALUE val) Defines a new constant under the class/module. void rb_define_global_const(char *name, VALUE val) Defines global contant. This is just work as rb_define_const(cKernal, name, val) ** Method Definition rb_define_method(VALUE class, char *name, VALUE (*func)(), int argc) �᥽�åɤ�������롥argc��self����������ο���argc��-1�λ�, �ؿ��ˤϰ����ο�(self��ޤޤʤ�)����1����, �������������2�� ���Ȥ��������Ϳ������(��3������self)��argc��-2�λ�, ��1�� ����self, ��2������args(args�ϰ�����ޤ�Ruby������)�Ȥ����� ����Ϳ�����롥 rb_define_private_method(VALUE class, char *name, VALUE (*func)(), int argc) private�᥽�åɤ�������롥������rb_define_method()��Ʊ���� rb_define_singleton_method(VALUE class, char *name, VALUE (*func)(), int argc) �ðۥ᥽�åɤ�������롥������rb_define_method()��Ʊ���� rb_scan_args(int atgc, VALUE *argv, char *fmt, ...) argc,argv������Ϳ����줿������ʬ�򤹤롥fmt��ɬ�ܰ����ο�, �ղð����ο�, �Ĥ�ΰ��������뤫����ꤹ��ʸ�����, "������ ��*"�Ȥ��������Ǥ��롥 2 ���ܤο�����"*"�Ϥ��줾���ά��ǽ�� ���롥ɬ�ܰ�������Ĥ�ʤ�����0����ꤹ�롥��3�����ʹߤ��� ���ؤΥݥ��󥿤�, �����������Ǥ������ѿ��˳�Ǽ����롥�ղð� �����б����������Ϳ�����Ƥ��ʤ������ѿ���Qnil���������� �롥 ** Ruby�᥽�åɸƤӽФ� VALUE rb_funcall(VALUE recv, ID mid, int narg, ...) �᥽�åɸƤӽФ���ʸ���󤫤�mid�����뤿��ˤ�rb_intern()��Ȥ��� VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv) �᥽�åɸƤӽФ���������argc,argv�������Ϥ��� VALUE rb_eval_string(char *str) ʸ�����Ruby�ȥ�����ץȤ��ƥ���ѥ��롦�¹Ԥ��롥 ID rb_intern(char *name) ʸ������б�����ID���֤��� char *rb_id2name(ID id) ID���б�����ʸ������֤�(�ǥХå���)�� char *rb_class2name(VALUE class) class��̾�����֤�(�ǥХå���)��class��̾��������ʤ����ˤ�, ������̤ä�̾������ĥ��饹��̾�����֤��� ** ���󥹥����ѿ� VALUE rb_iv_get(VALUE obj, char *name) obj�Υ��󥹥����ѿ����ͤ����롥`@'�ǻϤޤ�ʤ����󥹥��� �ѿ��� Ruby�ץ�����फ�饢�������Ǥ��ʤ��ֱ��줿�ץ��󥹥� ���ѿ��ˤʤ롥 VALUE rb_iv_set(VALUE obj, char *name, VALUE val) obj�Υ��󥹥����ѿ���val�˥��åȤ��롥 ** ���湽¤ VALUE rb_iterate(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2) func2��֥��å��Ȥ������ꤷ, func1�򥤥ƥ졼���Ȥ��ƸƤ֡� func1�ˤ� arg1�������Ȥ����Ϥ���, func2�ˤ���1�����˥��ƥ졼 ������Ϳ����줿��, ��2������arg2���Ϥ���롥 VALUE rb_yield(VALUE val) val���ͤȤ��ƥ��ƥ졼���֥��å���ƤӽФ��� VALUE rb_rescue(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2) �ؿ�func1��arg1������˸ƤӽФ���func1�μ¹�����㳰��ȯ���� �����ˤ� func2��arg2������Ȥ��ƸƤ֡�����ͤ��㳰��ȯ������ ���ä�����func1�������, �㳰��ȯ���������ˤ�func2������ͤ� ���롥 VALUE rb_ensure(VALUE (*func1)(), void *arg1, void (*func2)(), void *arg2) �ؿ�func1��arg1������Ȥ��Ƽ¹Ԥ�, �¹Խ�λ��(���Ȥ��㳰��ȯ �����Ƥ�) func2��arg2������Ȥ��Ƽ¹Ԥ��롥����ͤ�func1���� ���ͤǤ���(�㳰��ȯ�������������ʤ�)�� ** �㳰�����顼 void rb_warning(char *fmt, ...) rb_verbose����ɸ�२�顼���Ϥ˷ٹ�����ɽ�����롥������printf()��Ʊ���� void rb_raise(rb_eRuntimeError, char *fmt, ...) �㳰��ȯ�������롥������printf()��Ʊ���� void rb_fatal(char *fmt, ...) ��̿Ū�㳰��ȯ�������롥�̾���㳰�����ϹԤʤ�줺, ���󥿡� �ץ꥿����λ����(������ensure�ǻ��ꤵ�줿�����ɤϽ�λ���˼� �Ԥ����)�� void rb_bug(char *fmt, ...) ���󥿡��ץ꥿�ʤɥץ������ΥХ��Ǥ���ȯ������Ϥ��Τʤ��� ���λ��Ƥ֡����󥿡��ץ꥿�ϥ�������פ�ľ���˽�λ���롥�㳰 �����ϰ��ڹԤʤ��ʤ��� ** Initialize and Starts the Interpreter The embedding API are below (not needed for extension libraries): void ruby_init(int argc, char **argv, char **envp) Initializes the interpreter. void ruby_run() Starts execution of the interpreter. void ruby_script(char *name) Specifies the name of the script ($0). Appendix B. Functions Available in extconf.rb extconf.rb����Ǥ����Ѳ�ǽ�ʥ���ѥ���������å��δؿ��ϰ� �����̤�Ǥ��롥 have_library(lib, func) Checks whether library which contains specified function exists. Returns true if the library exists. have_func(func) Checks whether func exists. Returns true if the function exists. To check functions in the additional library, you need to check that library first using have_library(). have_header(header) Checks for the header files. Returns true if the header file exists. create_makefile(target) Generates the Makefile for the extension library. If you don't invoke this method, the compilation will not be done. /* * Local variables: * fill-column: 60 * end: */