home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ruby164.zip / rbemx164.zip / ruby / share / doc / ruby-1.6.4 / README.EXT < prev    next >
Text File  |  2001-06-18  |  28KB  |  1,027 lines

  1. .\" README.EXT -  -*- Text -*- created at: Mon Aug  7 16:45:54 JST 1995
  2.  
  3. This document explains how to make extension libraries for Ruby.
  4.  
  5. 1. Basic knowledge
  6.  
  7. In C, variables have types and data do not have types.  In contrast,
  8. Ruby variables do not have static type and data themselves have
  9. types.  So, data need to be converted across the languages.
  10.  
  11. Data in Ruby represented C type `VALUE'.  Each VALUE data have its
  12. data-type.
  13.  
  14. To retrieve an C data from the VALUE, you need to:
  15.  
  16.  (1) Identify VALUE's data type
  17.  (2) Convert VALUE into C data
  18.  
  19. Converting to wrong data type may cause serious problems.
  20.  
  21.  
  22. 1.1 Data-types
  23.  
  24. Ruby interpreter has data-types as below:
  25.  
  26.     T_NIL        nil
  27.     T_OBJECT    ordinary object
  28.     T_CLASS        class
  29.     T_MODULE    module
  30.     T_FLOAT        floating point number
  31.     T_STRING    string
  32.     T_REGEXP    regular expression
  33.     T_ARRAY        array
  34.     T_FIXNUM    Fixnum(31bit integer)
  35.     T_HASH        associative array
  36.     T_STRUCT    (Ruby) structure
  37.     T_BIGNUM    multi precision integer
  38.     T_TRUE        true
  39.     T_FALSE        false
  40.     T_DATA        data
  41.     T_SYMBOL        symbol
  42.  
  43. Otherwise, there are several other types used internally:
  44.  
  45.     T_ICLASS
  46.     T_MATCH
  47.     T_UNDEF
  48.     T_VARMAP
  49.     T_SCOPE
  50.     T_NODE
  51.  
  52. Most of the types are represented by C structures.
  53.  
  54. 1.2 Check Data Type of the VALUE
  55.  
  56. The macro TYPE() defined in ruby.h shows data-type of the VALUE.
  57. TYPE() returns the constant number T_XXXX described above.  To handle
  58. data-types, the code will be like:
  59.  
  60.   switch (TYPE(obj)) {
  61.     case T_FIXNUM:
  62.       /* process Fixnum */
  63.       break;
  64.     case T_STRING:
  65.       /* process String */
  66.       break;
  67.     case T_ARRAY:
  68.       /* process Array */
  69.       break;
  70.     default:
  71.       /* raise exception */
  72.       rb_raise(rb_eTypeError, "not valid value");
  73.       break;
  74.   }
  75.  
  76. There is the data-type check function.
  77.  
  78.   void Check_Type(VALUE value, int type)
  79.  
  80. It raises an exception, if the VALUE does not have the type specified.
  81.  
  82. There are faster check-macros for fixnums and nil.
  83.  
  84.   FIXNUM_P(obj)
  85.   NIL_P(obj)
  86.  
  87. 1.3 Convert VALUE into C data
  88.  
  89. The data for type T_NIL, T_FALSE, T_TRUE are nil, true, false
  90. respectively.  They are singletons for the data type.
  91.  
  92. The T_FIXNUM data is the 31bit length fixed integer (63bit length on
  93. some machines), which can be convert to the C integer by using
  94. FIX2INT() macro.  There also be NUM2INT() which converts any Ruby
  95. numbers into C integer.  The NUM2INT() macro includes type check, so
  96. the exception will be raised if conversion failed.
  97.  
  98. Other data types have corresponding C structures, e.g. struct RArray
  99. for T_ARRAY etc.  VALUE of the type which has corresponding structure
  100. can be cast to retrieve the pointer to the struct.  The casting macro
  101. RXXXX for each data type like RARRAY(obj).  see "ruby.h".
  102.  
  103. For example, `RSTRING(size)->len' is the way to get the size of the
  104. Ruby String object.  The allocated region can be accessed by
  105. `RSTRING(str)->ptr'.  For arrays, `RARRAY(ary)->len' and
  106. `RARRAY(ary)->ptr' respectively.
  107.  
  108. Notice: Do not change the value of the structure directly, unless you
  109. are responsible about the result.  It will be the cause of interesting
  110. bugs.
  111.  
  112. 1.4 Convert C data into VALUE
  113.  
  114. To convert C data to the values of Ruby:
  115.  
  116.   * FIXNUM
  117.  
  118.     left shift 1 bit, and turn on LSB.
  119.  
  120.   * Other pointer values
  121.  
  122.     cast to VALUE.
  123.  
  124. You can determine whether VALUE is pointer or not, by checking LSB.  
  125.  
  126. Notice Ruby does not allow arbitrary pointer value to be VALUE.  They
  127. should be pointers to the structures which Ruby knows.  The known
  128. structures are defined in <ruby.h>.
  129.  
  130. To convert C numbers to Ruby value, use these macros.
  131.  
  132.   INT2FIX()    for integers within 31bits.
  133.   INT2NUM()    for arbitrary sized integer.
  134.  
  135. INT2NUM() converts integers into Bignums, if it is out of FIXNUM
  136. range, but bit slower.
  137.  
  138. 1.5 Manipulate Ruby data
  139.  
  140. As I already told, it is not recommended to modify object's internal
  141. structure.  To manipulate objects, use functions supplied by Ruby
  142. interpreter.  Useful functions are listed below (not all):
  143.  
  144.  String functions
  145.  
  146.   rb_str_new(const char *ptr, long len)
  147.  
  148.     Creates a new Ruby string.
  149.  
  150.   rb_str_new2(const char *ptr)
  151.  
  152.     Creates a new Ruby string from C string.  This is equivalent to
  153.     rb_str_new(ptr, strlen(ptr)).
  154.  
  155.   rb_tainted_str_new(const char *ptr, long len)
  156.  
  157.     Creates a new tainted Ruby string.  Strings from external data
  158.     should be tainted.
  159.  
  160.   rb_tainted_str_new2(const char *ptr)
  161.  
  162.     Creates a new tainted Ruby string from C string.
  163.  
  164.   rb_str_cat(VALUE str, const char *ptr, long len)
  165.  
  166.     Appends len bytes data from ptr to the Ruby string.
  167.  
  168.  Array functions
  169.  
  170.   rb_ary_new()
  171.  
  172.     Creates an array with no element.
  173.  
  174.   rb_ary_new2(long len)
  175.  
  176.     Creates an array with no element, with allocating internal buffer
  177.     for len elements.
  178.  
  179.   rb_ary_new3(long n, ...)
  180.  
  181.     Creates an n-elements array from arguments.
  182.  
  183.   rb_ary_new4(long n, VALUE *elts)
  184.  
  185.     Creates an n-elements array from C array.
  186.  
  187.   rb_ary_push(VALUE ary, VALUE val)
  188.   rb_ary_pop(VALUE ary)
  189.   rb_ary_shift(VALUE ary)
  190.   rb_ary_unshift(VALUE ary, VALUE val)
  191.  
  192.     Array operations.  The first argument to each functions must be an 
  193.     array.  They may dump core if other types given.
  194.  
  195. 2. Extend Ruby with C
  196.  
  197. 2.1 Add new features to Ruby
  198.  
  199. You can add new features (classes, methods, etc.) to the Ruby
  200. interpreter.  Ruby provides the API to define things below:
  201.  
  202.  * Classes, Modules
  203.  * Methods, Singleton Methods
  204.  * Constants
  205.  
  206. 2.1.1 Class/module definition
  207.  
  208. To define class or module, use functions below:
  209.  
  210.   VALUE rb_define_class(const char *name, VALUE super)
  211.   VALUE rb_define_module(const char *name)
  212.  
  213. These functions return the newly created class or module.  You may
  214. want to save this reference into the variable to use later.
  215.  
  216. To define nested class or module, use functions below:
  217.  
  218.   VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
  219.   VALUE rb_define_module_under(VALUE outer, const char *name)
  220.  
  221. 2.1.2 Method/singleton method definition
  222.  
  223. To define methods or singleton methods, use functions below:
  224.  
  225.   void rb_define_method(VALUE klass, const char *name, 
  226.                 VALUE (*func)(), int argc)
  227.  
  228.   void rb_define_singleton_method(VALUE object, const char *name, 
  229.                       VALUE (*func)(), int argc)
  230.  
  231. The `argc' represents the number of the arguments to the C function,
  232. which must be less than 17.  But I believe you don't need that much. :-)
  233.  
  234. If `argc' is negative, it specifies calling sequence, not number of
  235. the arguments.  
  236.  
  237. If argc is -1, the function will be called like:
  238.  
  239.   VALUE func(int argc, VALUE *argv, VALUE obj)
  240.  
  241. where argc is the actual number of arguments, argv is the C array of
  242. the arguments, and obj is the receiver.
  243.  
  244. if argc is -2, the arguments are passed in Ruby array. The function
  245. will be called like:
  246.  
  247.   VALUE func(VALUE obj, VALUE args)
  248.  
  249. where obj is the receiver, and args is the Ruby array containing
  250. actual arguments.
  251.  
  252. There're two more functions to define method.  One is to define
  253. private method:
  254.  
  255.   void rb_define_private_method(VALUE klass, const char *name, 
  256.                     VALUE (*func)(), int argc)
  257.  
  258. The other is to define module function, which is private AND singleton
  259. method of the module.  For example, sqrt is the module function
  260. defined in Math module.  It can be call in the form like:
  261.  
  262.   Math.sqrt(4)
  263.  
  264. or
  265.  
  266.   include Math
  267.   sqrt(4)
  268.  
  269. To define module function
  270.  
  271.   void rb_define_module_function(VALUE module, const char *name, 
  272.                  VALUE (*func)(), int argc)
  273.  
  274. Oh, in addition, function-like method, which is private method defined
  275. in Kernel module, can be defined using:
  276.  
  277.   void rb_define_global_function(const char *name, VALUE (*func)(), int argc)
  278.  
  279. To define alias to the method,
  280.  
  281.   void rb_define_alias(VALUE module, const char* new, const char* old);
  282.  
  283. 2.1.3 Constant definition
  284.  
  285. We have 2 functions to define constants:
  286.  
  287.   void rb_define_const(VALUE klass, const char *name, VALUE val)
  288.   void rb_define_global_const(const char *name, VALUE val)
  289.  
  290. The former is to define constant under specified class/module.  The
  291. latter is to define global constant.
  292.  
  293. 2.2 Use Ruby features from C
  294.  
  295. There are several ways to invoke Ruby's features from C code.
  296.  
  297. 2.2.1 Evaluate Ruby Program in String
  298.  
  299. Easiest way to call Ruby's function from C program is to evaluate the
  300. string as Ruby program.  This function will do the job.
  301.  
  302.   VALUE rb_eval_string(const char *str)
  303.  
  304. Evaluation is done under current context, thus current local variables
  305. of the innermost method (which is defined by Ruby) can be accessed.
  306.  
  307. 2.2.2 ID or Symbol
  308.  
  309. You can invoke methods directly, without parsing the string.  First I
  310. need to explain about symbols (which data type is ID).  ID is the
  311. integer number to represent Ruby's identifiers such as variable names.
  312. It can be accessed from Ruby in the form like:
  313.  
  314.  :Identifier
  315.  
  316. You can get the symbol value from string within C code, by using
  317.  
  318.   rb_intern(const char *name)
  319.  
  320. 2.2.3 Invoke Ruby method from C
  321.  
  322. To invoke methods directly, you can use the function below
  323.  
  324.   VALUE rb_funcall(VALUE recv, ID mid, int argc, ...)
  325.  
  326. This function invokes the method of the recv, which name is specified
  327. by the symbol mid.
  328.  
  329. 2.2.4 Accessing the variables and constants
  330.  
  331. You can access class variables, and instance variables using access
  332. functions.  Also, global variables can be shared between both worlds.
  333. There's no way to access Ruby's local variables.
  334.  
  335. The functions to access/modify instance variables are below:
  336.  
  337.   VALUE rb_ivar_get(VALUE obj, ID id)
  338.   VALUE rb_ivar_set(VALUE obj, ID id, VALUE val)
  339.  
  340. id must be the symbol, which can be retrieved by rb_intern().
  341.  
  342. To access the constants of the class/module:
  343.  
  344.   VALUE rb_const_get(VALUE obj, ID id)
  345.  
  346. See 2.1.3 for defining new constant.
  347.  
  348. 3. Information sharing between Ruby and C
  349.  
  350. 3.1 Ruby constant that C can be accessed from C
  351.  
  352. Following Ruby constants can be referred from C.
  353.  
  354.   Qtrue
  355.   Qfalse
  356.  
  357. Boolean values.  Qfalse is false in the C also (i.e. 0).
  358.  
  359.   Qnil
  360.  
  361. Ruby nil in C scope.
  362.  
  363. 3.2 Global variables shared between C and Ruby
  364.  
  365. Information can be shared between two worlds, using shared global
  366. variables.  To define them, you can use functions listed below:
  367.  
  368.   void rb_define_variable(const char *name, VALUE *var)
  369.  
  370. This function defines the variable which is shared by the both world.
  371. The value of the global variable pointed by `var', can be accessed
  372. through Ruby's global variable named `name'.
  373.  
  374. You can define read-only (from Ruby, of course) variable by the
  375. function below.
  376.  
  377.   void rb_define_readonly_variable(const char *name, VALUE *var)
  378.  
  379. You can defined hooked variables.  The accessor functions (getter and
  380. setter) are called on access to the hooked variables.
  381.  
  382.   void rb_define_hooked_variable(constchar *name, VALUE *var,
  383.                  VALUE (*getter)(), void (*setter)())
  384.  
  385. If you need to supply either setter or getter, just supply 0 for the
  386. hook you don't need.  If both hooks are 0, rb_define_hooked_variable()
  387. works just like rb_define_variable().
  388.  
  389.   void rb_define_virtual_variable(const char *name,
  390.                   VALUE (*getter)(), void (*setter)())
  391.  
  392. This function defines the Ruby global variable without corresponding C
  393. variable.  The value of the variable will be set/get only by hooks.
  394.  
  395. The prototypes of the getter and setter functions are as following:
  396.  
  397.   (*getter)(ID id, void *data, struct global_entry* entry);
  398.   (*setter)(VALUE val, ID id, void *data, struct global_entry* entry);
  399.  
  400. 3.3 Encapsulate C data into Ruby object
  401.  
  402. To wrapping and objectify the C pointer as Ruby object (so called
  403. DATA), use Data_Wrap_Struct().
  404.  
  405.   Data_Wrap_Struct(klass, mark, free, ptr)
  406.  
  407. Data_Wrap_Struct() returns a created DATA object.  The klass argument
  408. is the class for the DATA object.  The mark argument is the function
  409. to mark Ruby objects pointed by this data.  The free argument is the
  410. function to free the pointer allocation.  The functions, mark and
  411. free, will be called from garbage collector.
  412.  
  413. You can allocate and wrap the structure in one step.
  414.  
  415.   Data_Make_Struct(klass, type, mark, free, sval)
  416.  
  417. This macro returns an allocated Data object, wrapping the pointer to
  418. the structure, which is also allocated.  This macro works like:
  419.  
  420.   (sval = ALLOC(type), Data_Wrap_Struct(klass, mark, free, sval))
  421.  
  422. Arguments, klass, mark, free, works like their counterpart of
  423. Data_Wrap_Struct().  The pointer to allocated structure will be
  424. assigned to sval, which should be the pointer to the type specified.
  425.  
  426. To retrieve the C pointer from the Data object, use the macro
  427. Data_Get_Struct().
  428.  
  429.   Data_Get_Struct(obj, type, sval)
  430.  
  431. The pointer to the structure will be assigned to the variable sval.
  432.  
  433. See example below for detail. 
  434.  
  435. 4. Example - Creating dbm extension
  436.  
  437. OK, here's the example to make extension library.  This is the
  438. extension to access dbm.  The full source is included in ext/
  439. directory in the Ruby's source tree.
  440.  
  441. (1) make the directory
  442.  
  443.   % mkdir ext/dbm
  444.  
  445. Make a directory for the extension library under ext directory.
  446.  
  447. (2) create MANIFEST file
  448.  
  449.   % cd ext/dbm
  450.   % touch MANIFEST
  451.  
  452. There should be MANIFEST file in the directory for the extension
  453. library.  Make empty file now.
  454.  
  455. (3) design the library
  456.  
  457. You need to design the library features, before making it.
  458.  
  459. (4) write C code.
  460.  
  461. You need to write C code for your extension library.  If your library
  462. has only one source file, choosing ``LIBRARY.c'' as a file name is
  463. preferred.  On the other hand, in case your library has plural source
  464. files, avoid choosing ``LIBRARY.c'' for a file name.  It may conflict
  465. with intermediate file ``LIBRARY.o'' on some platforms.
  466.  
  467. Ruby will execute the initializing function named ``Init_LIBRARY'' in
  468. the library.  For example, ``Init_dbm()'' will be executed when loading
  469. the library.
  470.  
  471. Here's the example of an initializing function.
  472.  
  473. --
  474. Init_dbm()
  475. {
  476.     /* define DBM class */
  477.     cDBM = rb_define_class("DBM", rb_cObject);
  478.     /* DBM includes Enumerate module */
  479.     rb_include_module(cDBM, rb_mEnumerable);
  480.  
  481.     /* DBM has class method open(): arguments are received as C array */
  482.     rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1);
  483.  
  484.     /* DBM instance method close(): no args */
  485.     rb_define_method(cDBM, "close", fdbm_close, 0);
  486.     /* DBM instance method []: 1 argument */
  487.     rb_define_method(cDBM, "[]", fdbm_fetch, 1);
  488.         :
  489.  
  490. }
  491. --
  492.  
  493. The dbm extension wrap dbm struct in C world using Data_Make_Struct.
  494.  
  495. --
  496. struct dbmdata {
  497.     int  di_size;
  498.     DBM *di_dbm;
  499. };
  500.  
  501.  
  502. obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp);
  503. --
  504.  
  505. This code wraps dbmdata structure into Ruby object.  We avoid wrapping
  506. DBM* directly, because we want to cache size information.
  507.  
  508. To retrieve dbmdata structure from Ruby object, we define the macro below:
  509.  
  510. --
  511. #define GetDBM(obj, dbmp) {\
  512.     Data_Get_Struct(obj, struct dbmdata, dbmp);\
  513.     if (dbmp->di_dbm == 0) closed_dbm();\
  514. }
  515. --
  516.  
  517. This sort of complicated macro do the retrieving and close check for
  518. the DBM.
  519.  
  520. There are three kind of way to receiving method arguments.  First, the
  521. methods with fixed number of arguments receives arguments like this:
  522.  
  523. --
  524. static VALUE
  525. fdbm_delete(obj, keystr)
  526.     VALUE obj, keystr;
  527. {
  528.     :
  529. }
  530. --
  531.  
  532. The first argument of the C function is the self, the rest are the
  533. arguments to the method.
  534.  
  535. Second, the methods with arbitrary number of arguments receives
  536. arguments like this:
  537.  
  538. --
  539. static VALUE
  540. fdbm_s_open(argc, argv, klass)
  541.     int argc;
  542.     VALUE *argv;
  543.     VALUE klass;
  544. {
  545.     :
  546.     if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
  547.     mode = 0666;        /* default value */
  548.     }
  549.     :
  550. }
  551. --
  552.  
  553. The first argument is the number of method arguments.  the second
  554. argument is the C array of the method arguments.  And the third
  555. argument is the receiver of the method.
  556.  
  557. You can use the function rb_scan_args() to check and retrieve the
  558. arguments.  For example "11" means, the method requires at least one
  559. argument, and at most receives two arguments.
  560.  
  561. The methods with arbitrary number of arguments can receives arguments
  562. by Ruby's array, like this:
  563.  
  564. --
  565. static VALUE
  566. fdbm_indexes(obj, args)
  567.     VALUE obj, args;
  568. {
  569.     :
  570. }
  571. --
  572.  
  573. The first argument is the receiver, the second one is the Ruby array
  574. which contains the arguments to the method.
  575.  
  576. ** Notice
  577.  
  578. GC should know about global variables which refers Ruby's objects, but
  579. not exported to the Ruby world.  You need to protect them by
  580.  
  581.   void rb_global_variable(VALUE *var)
  582.  
  583. (5) prepare extconf.rb
  584.  
  585. If there exists the file named extconf.rb, it will be executed to
  586. generate Makefile.  If not, compilation scheme try to generate
  587. Makefile anyway.
  588.  
  589. The extconf.rb is the file to check compilation condition etc.  You
  590. need to put
  591.  
  592.   require 'mkmf'
  593.  
  594. at the top of the file.  You can use the functions below to check the
  595. condition.
  596.  
  597.   have_library(lib, func): check whether library containing function exists.
  598.   have_func(func, header): check whether function exists
  599.   have_header(header): check whether header file exists
  600.   create_makefile(target): generate Makefile
  601.  
  602. The value of variables below will affect Makefile.
  603.  
  604.   $CFLAGS: included in CFLAGS make variable (such as -I)
  605.   $LDFLAGS: included in LDFLAGS make variable (such as -L)
  606.  
  607. If compilation condition is not fulfilled, you do not call
  608. ``create_makefile''.  Makefile will not generated, compilation will
  609. not be done.
  610.  
  611. (6) prepare depend (optional)
  612.  
  613. If the file named depend exists, Makefile will include that file to
  614. check dependency.  You can make this file by invoking
  615.  
  616.   % gcc -MM *.c > depend
  617.  
  618. It's no harm.  Prepare it.
  619.  
  620. (7) put file names into MANIFEST (optional)
  621.  
  622.   % find * -type f -print > MANIFEST
  623.   % vi MANIFEST
  624.  
  625. Append file names into MANIFEST.  The compilation scheme requires
  626. MANIFEST only to be exist.  But, you'd better take this step to
  627. distinguish required files.
  628.  
  629. (8) generate Makefile
  630.  
  631. Try generate Makefile by:
  632.  
  633.   ruby extconf.rb
  634.  
  635. You don't need this step, if you put extension library under ext
  636. directory of the ruby source tree.  In that case, compilation of the
  637. interpreter will do this step for you.
  638.  
  639. (9) make
  640.  
  641. Type
  642.  
  643.   make
  644.  
  645. to compile your extension.  You don't need this step neither, if you
  646. put extension library under ext directory of the ruby source tree.
  647.  
  648. (9) debug
  649.  
  650. You may need to rb_debug the extension.  The extensions can be linked
  651. statically by adding directory name in the ext/Setup file, so that you
  652. can inspect the extension with the debugger.
  653.  
  654. (10) done, now you have the extension library
  655.  
  656. You can do anything you want with your library.  The author of Ruby
  657. will not claim any restriction about your code depending Ruby API.
  658. Feel free to use, modify, distribute or sell your program.
  659.  
  660. Appendix A. Ruby source files overview
  661.  
  662. ruby language core
  663.  
  664.   class.c
  665.   error.c
  666.   eval.c
  667.   gc.c
  668.   object.c
  669.   parse.y
  670.   variable.c
  671.  
  672. utility functions
  673.  
  674.   dln.c
  675.   regex.c
  676.   st.c
  677.   util.c
  678.  
  679. ruby interpreter implementation
  680.  
  681.   dmyext.c
  682.   inits.c
  683.   main.c
  684.   ruby.c
  685.   version.c
  686.  
  687. class library
  688.  
  689.   array.c
  690.   bignum.c
  691.   compar.c
  692.   dir.c
  693.   enum.c
  694.   file.c
  695.   hash.c
  696.   io.c
  697.   marshal.c
  698.   math.c
  699.   numeric.c
  700.   pack.c
  701.   prec.c
  702.   process.c
  703.   random.c
  704.   range.c
  705.   re.c
  706.   signal.c
  707.   sprintf.c
  708.   string.c
  709.   struct.c
  710.   time.c
  711.  
  712. Appendix B. Ruby extension API reference
  713.  
  714. ** Types
  715.  
  716.  VALUE
  717.  
  718. The type for Ruby object.  Actual structures are defined in ruby.h,
  719. such as struct RString, etc.  To refer the values in structures, use
  720. casting macros like RSTRING(obj).
  721.  
  722. ** Variables and constants
  723.  
  724.  Qnil
  725.  
  726. const: nil object
  727.  
  728.  Qtrue
  729.  
  730. const: true object(default true value)
  731.  
  732.  Qfalse
  733.  
  734. const: false object
  735.  
  736. ** C pointer wrapping
  737.  
  738.  Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval)
  739.  
  740. Wrap C pointer into Ruby object.  If object has references to other
  741. Ruby object, they should be marked by using mark function during GC
  742. process.  Otherwise, mark should be 0.  When this object is no longer
  743. referred by anywhere, the pointer will be discarded by free function.
  744.  
  745.  Data_Make_Struct(klass, type, mark, free, sval)
  746.  
  747. This macro allocates memory using malloc(), assigns it to the variable
  748. sval, and returns the DATA encapsulating the pointer to memory region.
  749.  
  750.  Data_Get_Struct(data, type, sval)
  751.  
  752. This macro retrieves the pointer value from DATA, and assigns it to
  753. the variable sval. 
  754.  
  755. ** defining class/module
  756.  
  757.  VALUE rb_define_class(const char *name, VALUE super)
  758.  
  759. Defines new Ruby class as subclass of super.
  760.  
  761.  VALUE rb_define_class_under(VALUE module, const char *name, VALUE super)
  762.  
  763. Creates new Ruby class as subclass of super, under the module's
  764. namespace.
  765.  
  766.  VALUE rb_define_module(const char *name)
  767.  
  768. Defines new Ruby module.
  769.  
  770.  VALUE rb_define_module_under(VALUE module, const char *name, VALUE super)
  771.  
  772. Defines new Ruby module, under the module's namespace.
  773.  
  774.  void rb_include_module(VALUE klass, VALUE module)
  775.  
  776. Includes module into class.  If class already includes it, just
  777. ignore.
  778.  
  779.  void rb_extend_object(VALUE object, VALUE module)
  780.  
  781. Extend the object with module's attribute.
  782.  
  783. ** Defining Global Variables
  784.  
  785.  void rb_define_variable(const char *name, VALUE *var)
  786.  
  787. Defines a global variable which is shared between C and Ruby.  If name
  788. contains the character which is not allowed to be part of the symbol,
  789. it can't be seen from Ruby programs.
  790.  
  791.  void rb_define_readonly_variable(const char *name, VALUE *var)
  792.  
  793. Defines a read-only global variable.  Works just like
  794. rb_define_variable(), except defined variable is read-only.
  795.  
  796.  void rb_define_virtual_variable(const char *name,
  797.                  VALUE (*getter)(), VALUE (*setter)())
  798.  
  799. Defines a virtual variable, whose behavior is defined by pair of C
  800. functions.  The getter function is called when the variable is
  801. referred.  The setter function is called when the value is set to the
  802. variable.  The prototype for getter/setter functions are:
  803.  
  804.     VALUE getter(ID id)
  805.     void setter(VALUE val, ID id)
  806.  
  807. The getter function must return the value for the access.
  808.  
  809.  void rb_define_hooked_variable(const char *name, VALUE *var,
  810.                 VALUE (*getter)(), VALUE (*setter)())
  811.  
  812. Defines hooked variable.  It's virtual variable with C variable.  The
  813. getter is called as
  814.  
  815.     VALUE getter(ID id, VALUE *var)
  816.  
  817. returning new value.  The setter is called as
  818.  
  819.     void setter(VALUE val, ID id, VALUE *var)
  820.  
  821. GC requires to mark the C global variables which hold Ruby values.
  822.  
  823.  void rb_global_variable(VALUE *var)
  824.  
  825. Tells GC to protect these variables.
  826.  
  827. ** Constant Definition
  828.  
  829.  void rb_define_const(VALUE klass, const char *name, VALUE val)
  830.  
  831. Defines a new constant under the class/module.
  832.  
  833.  void rb_define_global_const(const char *name, VALUE val)
  834.  
  835. Defines global constant.  This is just work as
  836.  
  837.      rb_define_const(cKernal, name, val)
  838.  
  839. ** Method Definition
  840.  
  841.  rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
  842.  
  843. Defines a method for the class.  func is the function pointer.  argc
  844. is the number of arguments.  if argc is -1, the function will receive
  845. 3 arguments argc, argv, and self.  if argc is -2, the function will
  846. receive 2 arguments, self and args, where args is the Ruby array of
  847. the method arguments.
  848.  
  849.  rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
  850.  
  851. Defines a private method for the class.  Arguments are same as
  852. rb_define_method().
  853.  
  854.  rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
  855.  
  856. Defines a singleton method.  Arguments are same as rb_define_method().
  857.  
  858.  rb_scan_args(int argc, VALUE *argv, const char *fmt, ...)
  859.  
  860. Retrieve argument from argc, argv.  The fmt is the format string for
  861. the arguments, such as "12" for 1 non-optional argument, 2 optional
  862. arguments.  If `*' appears at the end of fmt, it means the rest of
  863. the arguments are assigned to corresponding variable, packed in
  864. array.
  865.  
  866. ** Invoking Ruby method
  867.  
  868.  VALUE rb_funcall(VALUE recv, ID mid, int narg, ...)
  869.  
  870. Invokes the method.  To retrieve mid from method name, use rb_intern().
  871.  
  872.  VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv)
  873.  
  874. Invokes method, passing arguments by array of values.
  875.  
  876.  VALUE rb_eval_string(const char *str)
  877.  
  878. Compiles and executes the string as Ruby program.
  879.  
  880.  ID rb_intern(const char *name)
  881.  
  882. Returns ID corresponding the name.
  883.  
  884.  char *rb_id2name(ID id)
  885.  
  886. Returns the name corresponding ID.
  887.  
  888.  char *rb_class2name(VALUE klass)
  889.  
  890. Returns the name of the class.
  891.  
  892.  int rb_respond_to(VALUE object, ID id)
  893.  
  894. Returns true if the object responds to the message specified by id.
  895.  
  896. ** Instance Variables
  897.  
  898.  VALUE rb_iv_get(VALUE obj, const char *name)
  899.  
  900. Retrieve the value of the instance variable.  If the name is not
  901. prefixed by `@', that variable shall be inaccessible from Ruby.
  902.  
  903.  VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
  904.  
  905. Sets the value of the instance variable.
  906.  
  907. ** Control Structure
  908.  
  909.  VALUE rb_iterate(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
  910.  
  911. Calls the function func1, supplying func2 as the block.  func1 will be
  912. called with the argument arg1.  func2 receives the value from yield as
  913. the first argument, arg2 as the second argument.
  914.  
  915.  VALUE rb_yield(VALUE val)
  916.  
  917. Evaluates the block with value val.
  918.  
  919.  VALUE rb_rescue(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
  920.  
  921. Calls the function func1, with arg1 as the argument.  If exception
  922. occurs during func1, it calls func2 with arg2 as the argument.  The
  923. return value of rb_rescue() is the return value from func1 if no
  924. exception occurs, from func2 otherwise.
  925.  
  926.  VALUE rb_ensure(VALUE (*func1)(), void *arg1, void (*func2)(), void *arg2)
  927.  
  928. Calls the function func1 with arg1 as the argument, then calls func2
  929. with arg2, whenever execution terminated.  The return value from
  930. rb_ensure() is that of func1.
  931.  
  932. ** Exceptions and Errors
  933.  
  934.  void rb_warn(const char *fmt, ...)
  935.  
  936. Prints warning message according to the printf-like format.
  937.  
  938.  void rb_warning(const char *fmt, ...)
  939.  
  940. Prints warning message according to the printf-like format, if
  941. $VERBOSE is true.
  942.  
  943.  void rb_raise(VALUE exception, const char *fmt, ...)
  944.  
  945. Raises an exception of class exception.  The fmt is the format string
  946. just like printf().
  947.  
  948.  void rb_fatal(const char *fmt, ...)
  949.  
  950. Raises fatal error, terminates the interpreter.  No exception handling
  951. will be done for fatal error, but ensure blocks will be executed.
  952.  
  953.  void rb_bug(const char *fmt, ...)
  954.  
  955. Terminates the interpreter immediately.  This function should be
  956. called under the situation caused by the bug in the interpreter.  No
  957. exception handling nor ensure execution will be done.
  958.  
  959. ** Initialize and Starts the Interpreter
  960.  
  961. The embedding API are below (not needed for extension libraries):
  962.  
  963.  void ruby_init()
  964.  
  965. Initializes the interpreter.
  966.  
  967.  void ruby_options(int argc, char **argv)
  968.  
  969. Process command line arguments for the interpreter.
  970.  
  971.  void ruby_run()
  972.  
  973. Starts execution of the interpreter.
  974.  
  975.  void ruby_script(char *name)
  976.  
  977. Specifies the name of the script ($0).
  978.  
  979. Appendix B. Functions Available in extconf.rb
  980.  
  981. These functions are available in extconf.rb:
  982.  
  983.  have_library(lib, func)
  984.  
  985. Checks whether library which contains specified function exists.
  986. Returns true if the library exists.
  987.  
  988.  find_library(lib, func, path...)
  989.  
  990. Checks whether library which contains specified function exists in
  991. path.  Returns true if the library exists.
  992.  
  993.  have_func(func, header)
  994.  
  995. Checks whether func exists with header.  Returns true if the function
  996. exists.  To check functions in the additional library, you need to
  997. check that library first using have_library().
  998.  
  999.  have_header(header)
  1000.  
  1001. Checks whether header exists.  Returns true if the header file exists.
  1002.  
  1003.  create_makefile(target)
  1004.  
  1005. Generates the Makefile for the extension library.  If you don't invoke
  1006. this method, the compilation will not be done.
  1007.  
  1008.  with_config(withval[, default=nil])
  1009.  
  1010. Parses the command line options and returns the value specified by
  1011. --with-<withval>.
  1012.  
  1013.  dir_config(target[, default_dir])
  1014.  dir_config(target[, default_include, default_lib])
  1015.  
  1016. Parses the command line options and adds the directories specified by
  1017. --with-<target>-dir, --with-<target>-include, and/or --with-<target>-lib
  1018. to $CFLAGS and/or $LDFLAGS.  --with-<target>-dir=/path is equivalent to
  1019. --with-<target>-include=/path/include --with-<target>-lib=/path/lib.
  1020. Returns an array of the added directories ([include_dir, lib_dir]).
  1021.  
  1022. /*
  1023.  * Local variables:
  1024.  * fill-column: 70
  1025.  * end:
  1026.  */
  1027.