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