1998-01-16 15:13:05 +03:00
|
|
|
.\" README.EXT - -*- Text -*- created at: Mon Aug 7 16:45:54 JST 1995
|
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
This document explains how to make extension libraries for Ruby.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
1. Basic knowledge
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
In C, variables have types and data do not have types. In contrast,
|
1999-01-20 07:59:39 +03:00
|
|
|
Ruby variables do not have static type and data themselves have
|
1998-01-16 15:13:05 +03:00
|
|
|
types. So, data need to be converted across the languages.
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Data in Ruby represented C type `VALUE'. Each VALUE data have its
|
1998-01-16 15:13:05 +03:00
|
|
|
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
|
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
Converting to wrong data type may cause serious problems.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
1.1 Data-types
|
|
|
|
|
|
|
|
Ruby interpreter has data-types as below:
|
|
|
|
|
|
|
|
T_NIL nil
|
1999-08-24 12:21:56 +04:00
|
|
|
T_OBJECT ordinary object
|
1998-01-16 15:13:05 +03:00
|
|
|
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)
|
1999-08-24 12:21:56 +04:00
|
|
|
T_HASH associative array
|
1999-01-20 07:59:39 +03:00
|
|
|
T_STRUCT (Ruby) structure
|
1998-01-16 15:13:05 +03:00
|
|
|
T_BIGNUM multi precision integer
|
|
|
|
T_TRUE true
|
|
|
|
T_FALSE false
|
|
|
|
T_DATA data
|
2000-10-05 13:57:04 +04:00
|
|
|
T_SYMBOL symbol
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Otherwise, there are several other types used internally:
|
|
|
|
|
|
|
|
T_ICLASS
|
|
|
|
T_MATCH
|
2000-10-05 13:57:04 +04:00
|
|
|
T_UNDEF
|
1998-01-16 15:13:05 +03:00
|
|
|
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 */
|
2000-03-06 07:15:42 +03:00
|
|
|
rb_raise(rb_eTypeError, "not valid value");
|
1998-01-16 15:13:05 +03:00
|
|
|
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
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
The data for type T_NIL, T_FALSE, T_TRUE are nil, true, false
|
|
|
|
respectively. They are singletons for the data type.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
The T_FIXNUM data is the 31bit length fixed integer (63bit length on
|
1999-08-24 12:21:56 +04:00
|
|
|
some machines), which can be convert to the C integer by using
|
1999-01-20 07:59:39 +03:00
|
|
|
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.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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".
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
1.4 Convert C data into VALUE
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
To convert C data to the values of Ruby:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
* FIXNUM
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
left shift 1 bit, and turn on LSB.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
* Other pointer values
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
cast to VALUE.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You can determine whether VALUE is pointer or not, by checking LSB.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Notice Ruby does not allow arbitrary pointer value to be VALUE. They
|
|
|
|
should be pointers to the structures which Ruby knows. The known
|
|
|
|
structures are defined in <ruby.h>.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
To convert C numbers to Ruby value, use these macros.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
INT2FIX() for integers within 31bits.
|
1999-08-13 09:45:20 +04:00
|
|
|
INT2NUM() for arbitrary sized integer.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
INT2NUM() converts integers into Bignums, if it is out of FIXNUM
|
|
|
|
range, but bit slower.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
1.5 Manipulate Ruby data
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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):
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
String functions
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_str_new(const char *ptr, long len)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Creates a new Ruby string.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_str_new2(const char *ptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Creates a new Ruby string from C string. This is equivalent to
|
|
|
|
rb_str_new(ptr, strlen(ptr)).
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_tainted_str_new(const char *ptr, long len)
|
2000-07-10 08:49:24 +04:00
|
|
|
|
|
|
|
Creates a new tainted Ruby string. Strings from external data
|
|
|
|
should be tainted.
|
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_tainted_str_new2(const char *ptr)
|
2000-07-10 08:49:24 +04:00
|
|
|
|
|
|
|
Creates a new tainted Ruby string from C string.
|
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_str_cat(VALUE str, const char *ptr, long len)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Appends len bytes data from ptr to the Ruby string.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Array functions
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_new()
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Creates an array with no element.
|
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_ary_new2(long len)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Creates an array with no element, with allocating internal buffer
|
|
|
|
for len elements.
|
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_ary_new3(long n, ...)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Creates an n-elements array from arguments.
|
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_ary_new4(long n, VALUE *elts)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Creates an n-elements array from C array.
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(VALUE ary, VALUE val)
|
|
|
|
rb_ary_pop(VALUE ary)
|
|
|
|
rb_ary_shift(VALUE ary)
|
|
|
|
rb_ary_unshift(VALUE ary, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Array operations. The first argument to each functions must be an
|
|
|
|
array. They may dump core if other types given.
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
2. Extend Ruby with C
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
2.1 Add new features to Ruby
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You can add new features (classes, methods, etc.) to the Ruby
|
|
|
|
interpreter. Ruby provides the API to define things below:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
* Classes, Modules
|
|
|
|
* Methods, Singleton Methods
|
|
|
|
* Constants
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
2.1.1 Class/module definition
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
To define class or module, use functions below:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_define_class(const char *name, VALUE super)
|
|
|
|
VALUE rb_define_module(const char *name)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
These functions return the newly created class or module. You may
|
1999-01-20 07:59:39 +03:00
|
|
|
want to save this reference into the variable to use later.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-03-06 07:15:42 +03:00
|
|
|
To define nested class or module, use functions below:
|
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
|
|
|
|
VALUE rb_define_module_under(VALUE outer, const char *name)
|
2000-03-06 07:15:42 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
2.1.2 Method/singleton method definition
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
To define methods or singleton methods, use functions below:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_method(VALUE klass, const char *name,
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE (*func)(), int argc)
|
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_singleton_method(VALUE object, const char *name,
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE (*func)(), int argc)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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. :-)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
If `argc' is negative, it specifies calling sequence, not number of
|
|
|
|
the arguments.
|
|
|
|
|
|
|
|
If argc is -1, the function will be called like:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE func(int argc, VALUE *argv, VALUE obj)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
where argc is the actual number of arguments, argv is the C array of
|
|
|
|
the arguments, and obj is the receiver.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_private_method(VALUE klass, const char *name,
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE (*func)(), int argc)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Math.sqrt(4)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
or
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
include Math
|
|
|
|
sqrt(4)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
To define module function
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_module_function(VALUE module, const char *name,
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE (*func)(), int argc)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Oh, in addition, function-like method, which is private method defined
|
|
|
|
in Kernel module, can be defined using:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_global_function(const char *name, VALUE (*func)(), int argc)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-10 08:49:24 +04:00
|
|
|
To define alias to the method,
|
|
|
|
|
|
|
|
void rb_define_alias(VALUE module, const char* new, const char* old);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
2.1.3 Constant definition
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
We have 2 functions to define constants:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_const(VALUE klass, const char *name, VALUE val)
|
|
|
|
void rb_define_global_const(const char *name, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
The former is to define constant under specified class/module. The
|
|
|
|
latter is to define global constant.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
2.2 Use Ruby features from C
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
There are several ways to invoke Ruby's features from C code.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
2.2.1 Evaluate Ruby Program in String
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Easiest way to call Ruby's function from C program is to evaluate the
|
|
|
|
string as Ruby program. This function will do the job.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_eval_string(const char *str)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Evaluation is done under current context, thus current local variables
|
|
|
|
of the innermost method (which is defined by Ruby) can be accessed.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
2.2.2 ID or Symbol
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
:Identifier
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
You can get the symbol value from string within C code, by using
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_intern(const char *name)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
2.2.3 Invoke Ruby method from C
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
To invoke methods directly, you can use the function below
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_funcall(VALUE recv, ID mid, int argc, ...)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
This function invokes the method of the recv, which name is specified
|
|
|
|
by the symbol mid.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
2.2.4 Accessing the variables and constants
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You can access class variables, and instance variables using access
|
|
|
|
functions. Also, global variables can be shared between both worlds.
|
|
|
|
There's no way to access Ruby's local variables.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
The functions to access/modify instance variables are below:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_ivar_get(VALUE obj, ID id)
|
|
|
|
VALUE rb_ivar_set(VALUE obj, ID id, VALUE val)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
id must be the symbol, which can be retrieved by rb_intern().
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
To access the constants of the class/module:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_const_get(VALUE obj, ID id)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
See 2.1.3 for defining new constant.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
3. Information sharing between Ruby and C
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
3.1 Ruby constant that C can be accessed from C
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Following Ruby constants can be referred from C.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Qtrue
|
|
|
|
Qfalse
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Boolean values. Qfalse is false in the C also (i.e. 0).
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Qnil
|
|
|
|
|
|
|
|
Ruby nil in C scope.
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
3.2 Global variables shared between C and Ruby
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Information can be shared between two worlds, using shared global
|
|
|
|
variables. To define them, you can use functions listed below:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_variable(const char *name, VALUE *var)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
This function defines the variable which is shared by the both world.
|
1999-08-24 12:21:56 +04:00
|
|
|
The value of the global variable pointed by `var', can be accessed
|
1999-08-13 09:45:20 +04:00
|
|
|
through Ruby's global variable named `name'.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You can define read-only (from Ruby, of course) variable by the
|
|
|
|
function below.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_readonly_variable(const char *name, VALUE *var)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You can defined hooked variables. The accessor functions (getter and
|
|
|
|
setter) are called on access to the hooked variables.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_hooked_variable(constchar *name, VALUE *var,
|
|
|
|
VALUE (*getter)(), void (*setter)())
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
If you need to supply either setter or getter, just supply 0 for the
|
|
|
|
hook you don't need. If both hooks are 0, rb_define_hooked_variable()
|
|
|
|
works just like rb_define_variable().
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_virtual_variable(const char *name,
|
|
|
|
VALUE (*getter)(), void (*setter)())
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
This function defines the Ruby global variable without corresponding C
|
|
|
|
variable. The value of the variable will be set/get only by hooks.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
3.3 Encapsulate C data into Ruby object
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
To wrapping and objectify the C pointer as Ruby object (so called
|
|
|
|
DATA), use Data_Wrap_Struct().
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-03-06 07:15:42 +03:00
|
|
|
Data_Wrap_Struct(klass, mark, free, ptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-03-06 07:15:42 +03:00
|
|
|
Data_Wrap_Struct() returns a created DATA object. The klass argument
|
1999-08-13 09:45:20 +04:00
|
|
|
is the class for the DATA object. The mark argument is the function
|
|
|
|
to mark Ruby objects pointed by this data. The free argument is the
|
|
|
|
function to free the pointer allocation. The functions, mark and
|
|
|
|
free, will be called from garbage collector.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You can allocate and wrap the structure in one step.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
Data_Make_Struct(klass, type, mark, free, sval)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
This macro returns an allocated Data object, wrapping the pointer to
|
|
|
|
the structure, which is also allocated. This macro works like:
|
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
(sval = ALLOC(type), Data_Wrap_Struct(klass, mark, free, sval))
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
Arguments, klass, mark, free, works like their counterpart of
|
1999-08-13 09:45:20 +04:00
|
|
|
Data_Wrap_Struct(). The pointer to allocated structure will be
|
|
|
|
assigned to sval, which should be the pointer to the type specified.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
To retrieve the C pointer from the Data object, use the macro
|
|
|
|
Data_Get_Struct().
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Data_Get_Struct(obj, type, sval)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
The pointer to the structure will be assigned to the variable sval.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
See example below for detail.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
4. Example - Creating dbm extension
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
OK, here's the example to make extension library. This is the
|
|
|
|
extension to access dbm. The full source is included in ext/
|
|
|
|
directory in the Ruby's source tree.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
(1) make the directory
|
|
|
|
|
|
|
|
% mkdir ext/dbm
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Make a directory for the extension library under ext directory.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
(2) create MANIFEST file
|
|
|
|
|
|
|
|
% cd ext/dbm
|
|
|
|
% touch MANIFEST
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
There should be MANIFEST file in the directory for the extension
|
|
|
|
library. Make empty file now.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
(3) design the library
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
You need to design the library features, before making it.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
(4) write C code.
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You need to write C code for your extension library. If your library
|
|
|
|
has only one source file, choosing ``LIBRARY.c'' as a file name is
|
1999-08-24 12:21:56 +04:00
|
|
|
preferred. On the other hand, in case your library has plural source
|
|
|
|
files, avoid choosing ``LIBRARY.c'' for a file name. It may conflict
|
1999-08-13 09:45:20 +04:00
|
|
|
with intermediate file ``LIBRARY.o'' on some platforms.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Ruby will execute the initializing function named ``Init_LIBRARY'' in
|
1999-08-24 12:21:56 +04:00
|
|
|
the library. For example, ``Init_dbm()'' will be executed when loading
|
1999-08-13 09:45:20 +04:00
|
|
|
the library.
|
|
|
|
|
|
|
|
Here's the example of an initializing function.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
--
|
|
|
|
Init_dbm()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
/* define DBM class */
|
|
|
|
cDBM = rb_define_class("DBM", rb_cObject);
|
|
|
|
/* DBM includes Enumerate module */
|
|
|
|
rb_include_module(cDBM, rb_mEnumerable);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
/* DBM has class method open(): arguments are received as C array */
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
/* DBM instance method close(): no args */
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_define_method(cDBM, "close", fdbm_close, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
/* DBM instance method []: 1 argument */
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_define_method(cDBM, "[]", fdbm_fetch, 1);
|
|
|
|
:
|
|
|
|
|
|
|
|
}
|
|
|
|
--
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
The dbm extension wrap dbm struct in C world using Data_Make_Struct.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
--
|
|
|
|
struct dbmdata {
|
|
|
|
int di_size;
|
|
|
|
DBM *di_dbm;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-03-06 07:15:42 +03:00
|
|
|
obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp);
|
1998-01-16 15:13:05 +03:00
|
|
|
--
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
This code wraps dbmdata structure into Ruby object. We avoid wrapping
|
|
|
|
DBM* directly, because we want to cache size information.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
To retrieve dbmdata structure from Ruby object, we define the macro below:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
--
|
|
|
|
#define GetDBM(obj, dbmp) {\
|
|
|
|
Data_Get_Struct(obj, struct dbmdata, dbmp);\
|
|
|
|
if (dbmp->di_dbm == 0) closed_dbm();\
|
|
|
|
}
|
|
|
|
--
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
This sort of complicated macro do the retrieving and close check for
|
|
|
|
the DBM.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
There are three kind of way to receiving method arguments. First, the
|
|
|
|
methods with fixed number of arguments receives arguments like this:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
--
|
|
|
|
static VALUE
|
|
|
|
fdbm_delete(obj, keystr)
|
|
|
|
VALUE obj, keystr;
|
|
|
|
{
|
|
|
|
:
|
|
|
|
}
|
|
|
|
--
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
The first argument of the C function is the self, the rest are the
|
|
|
|
arguments to the method.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
Second, the methods with arbitrary number of arguments receives
|
1999-08-13 09:45:20 +04:00
|
|
|
arguments like this:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
--
|
|
|
|
static VALUE
|
1999-08-24 12:21:56 +04:00
|
|
|
fdbm_s_open(argc, argv, klass)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1999-08-24 12:21:56 +04:00
|
|
|
VALUE klass;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
:
|
|
|
|
if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
|
|
|
|
mode = 0666; /* default value */
|
|
|
|
}
|
|
|
|
:
|
|
|
|
}
|
|
|
|
--
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
The first argument is the number of method arguments. the second
|
|
|
|
argument is the C array of the method arguments. And the third
|
|
|
|
argument is the receiver of the method.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You can use the function rb_scan_args() to check and retrieve the
|
1999-08-24 12:21:56 +04:00
|
|
|
arguments. For example "11" means, the method requires at least one
|
1999-08-13 09:45:20 +04:00
|
|
|
argument, and at most receives two arguments.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
The methods with arbitrary number of arguments can receives arguments
|
1999-08-13 09:45:20 +04:00
|
|
|
by Ruby's array, like this:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
--
|
|
|
|
static VALUE
|
|
|
|
fdbm_indexes(obj, args)
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE obj, args;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
:
|
|
|
|
}
|
|
|
|
--
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
The first argument is the receiver, the second one is the Ruby array
|
|
|
|
which contains the arguments to the method.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
** Notice
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
GC should know about global variables which refers Ruby's objects, but
|
|
|
|
not exported to the Ruby world. You need to protect them by
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
void rb_global_variable(VALUE *var)
|
|
|
|
|
|
|
|
(5) prepare extconf.rb
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
If there exists the file named extconf.rb, it will be executed to
|
|
|
|
generate Makefile. If not, compilation scheme try to generate
|
|
|
|
Makefile anyway.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
The extconf.rb is the file to check compilation condition etc. You
|
|
|
|
need to put
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
require 'mkmf'
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
at the top of the file. You can use the functions below to check the
|
1999-08-13 09:45:20 +04:00
|
|
|
condition.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
have_library(lib, func): check whether library containing function exists.
|
2000-07-07 07:20:53 +04:00
|
|
|
have_func(func, header): check whether function exists
|
1999-08-13 09:45:20 +04:00
|
|
|
have_header(header): check whether header file exists
|
|
|
|
create_makefile(target): generate Makefile
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
The value of variables below will affect Makefile.
|
|
|
|
|
|
|
|
$CFLAGS: included in CFLAGS make variable (such as -I)
|
|
|
|
$LDFLAGS: included in LDFLAGS make variable (such as -L)
|
|
|
|
|
|
|
|
If compilation condition is not fulfilled, you do not call
|
|
|
|
``create_makefile''. Makefile will not generated, compilation will
|
|
|
|
not be done.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
(6) prepare depend (optional)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
If the file named depend exists, Makefile will include that file to
|
|
|
|
check dependency. You can make this file by invoking
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-03-06 07:15:42 +03:00
|
|
|
% gcc -MM *.c > depend
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
It's no harm. Prepare it.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
(7) put file names into MANIFEST (optional)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
% find * -type f -print > MANIFEST
|
1998-01-16 15:13:05 +03:00
|
|
|
% vi MANIFEST
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Append file names into MANIFEST. The compilation scheme requires
|
|
|
|
MANIFEST only to be exist. But, you'd better take this step to
|
|
|
|
distinguish required files.
|
|
|
|
|
|
|
|
(8) generate Makefile
|
|
|
|
|
|
|
|
Try generate Makefile by:
|
|
|
|
|
|
|
|
ruby extconf.rb
|
|
|
|
|
|
|
|
You don't need this step, if you put extension library under ext
|
|
|
|
directory of the ruby source tree. In that case, compilation of the
|
|
|
|
interpreter will do this step for you.
|
|
|
|
|
|
|
|
(9) make
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Type
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
make
|
|
|
|
|
|
|
|
to compile your extension. You don't need this step neither, if you
|
|
|
|
put extension library under ext directory of the ruby source tree.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
(9) debug
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You may need to rb_debug the extension. The extensions can be linked
|
|
|
|
statically by adding directory name in the ext/Setup file, so that you
|
|
|
|
can inspect the extension with the debugger.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
(10) done, now you have the extension library
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
You can do anything you want with your library. The author of Ruby
|
|
|
|
will not claim any restriction about your code depending Ruby API.
|
|
|
|
Feel free to use, modify, distribute or sell your program.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Appendix A. Ruby source files overview
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ruby language core
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
class.c
|
|
|
|
error.c
|
|
|
|
eval.c
|
|
|
|
gc.c
|
|
|
|
object.c
|
|
|
|
parse.y
|
|
|
|
variable.c
|
|
|
|
|
|
|
|
utility functions
|
|
|
|
|
|
|
|
dln.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
|
2000-03-06 07:15:42 +03:00
|
|
|
marshal.c
|
1998-01-16 15:13:05 +03:00
|
|
|
math.c
|
|
|
|
numeric.c
|
|
|
|
pack.c
|
2000-03-06 07:15:42 +03:00
|
|
|
prec.c
|
1998-01-16 15:13:05 +03:00
|
|
|
process.c
|
|
|
|
random.c
|
|
|
|
range.c
|
|
|
|
re.c
|
|
|
|
signal.c
|
|
|
|
sprintf.c
|
|
|
|
string.c
|
|
|
|
struct.c
|
|
|
|
time.c
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Appendix B. Ruby extension API reference
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
** Types
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
The type for Ruby object. Actual structures are defined in ruby.h,
|
|
|
|
such as struct RString, etc. To refer the values in structures, use
|
|
|
|
casting macros like RSTRING(obj).
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
** Variables and constants
|
|
|
|
|
|
|
|
Qnil
|
|
|
|
|
|
|
|
const: nil object
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Qtrue
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
const: true object(default true value)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Qfalse
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
const: false object
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
** C pointer wrapping
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Wrap C pointer into Ruby object. If object has references to other
|
|
|
|
Ruby object, they should be marked by using mark function during GC
|
|
|
|
process. Otherwise, mark should be 0. When this object is no longer
|
|
|
|
referred by anywhere, the pointer will be discarded by free function.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
Data_Make_Struct(klass, type, mark, free, sval)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
This macro allocates memory using malloc(), assigns it to the variable
|
|
|
|
sval, and returns the DATA encapsulating the pointer to memory region.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
Data_Get_Struct(data, type, sval)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
This macro retrieves the pointer value from DATA, and assigns it to
|
|
|
|
the variable sval.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
** defining class/module
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_define_class(const char *name, VALUE super)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Defines new Ruby class as subclass of super.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_define_class_under(VALUE module, const char *name, VALUE super)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Creates new Ruby class as subclass of super, under the module's
|
|
|
|
namespace.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_define_module(const char *name)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Defines new Ruby module.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_define_module_under(VALUE module, const char *name, VALUE super)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
Defines new Ruby module, under the module's namespace.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
void rb_include_module(VALUE klass, VALUE module)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Includes module into class. If class already includes it, just
|
|
|
|
ignore.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
void rb_extend_object(VALUE object, VALUE module)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Extend the object with module's attribute.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
** Defining Global Variables
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_variable(const char *name, VALUE *var)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_readonly_variable(const char *name, VALUE *var)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Defines a read-only global variable. Works just like
|
|
|
|
rb_define_variable(), except defined variable is read-only.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_virtual_variable(const char *name,
|
2000-03-06 07:15:42 +03:00
|
|
|
VALUE (*getter)(), VALUE (*setter)())
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Defines a virtual variable, whose behavior is defined by pair of C
|
|
|
|
functions. The getter function is called when the variable is
|
2000-03-06 07:15:42 +03:00
|
|
|
referred. The setter function is called when the value is set to the
|
1999-01-20 07:59:39 +03:00
|
|
|
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.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_hooked_variable(const char *name, VALUE *var,
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE (*getter)(), VALUE (*setter)())
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
void rb_global_variable(VALUE *var)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Tells GC to protect these variables.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
** Constant Definition
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_const(VALUE klass, const char *name, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Defines a new constant under the class/module.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_define_global_const(const char *name, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
Defines global constant. This is just work as
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
rb_define_const(cKernal, name, val)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
** Method Definition
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Defines a method for the class. func is the function pointer. argc
|
|
|
|
is the number of arguments. if argc is -1, the function will receive
|
|
|
|
3 arguments argc, argv, and self. if argc is -2, the function will
|
|
|
|
receive 2 arguments, self and args, where args is the Ruby array of
|
|
|
|
the method arguments.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Defines a private method for the class. Arguments are same as
|
|
|
|
rb_define_method().
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Defines a singleton method. Arguments are same as rb_define_method().
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
rb_scan_args(int argc, VALUE *argv, const char *fmt, ...)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Retrieve argument from argc, argv. The fmt is the format string for
|
1999-08-24 12:21:56 +04:00
|
|
|
the arguments, such as "12" for 1 non-optional argument, 2 optional
|
|
|
|
arguments. If `*' appears at the end of fmt, it means the rest of
|
1999-08-13 09:45:20 +04:00
|
|
|
the arguments are assigned to corresponding variable, packed in
|
|
|
|
array.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
** Invoking Ruby method
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_funcall(VALUE recv, ID mid, int narg, ...)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Invokes the method. To retrieve mid from method name, use rb_intern().
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Invokes method, passing arguments by array of values.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_eval_string(const char *str)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Compiles and executes the string as Ruby program.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
ID rb_intern(const char *name)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Returns ID corresponding the name.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
char *rb_id2name(ID id)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Returns the name corresponding ID.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
char *rb_class2name(VALUE klass)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Returns the name of the class.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-03-06 07:15:42 +03:00
|
|
|
int rb_respond_to(VALUE object, ID id)
|
1999-10-12 08:53:36 +04:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
Returns true if the object responds to the message specified by id.
|
1999-10-12 08:53:36 +04:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
** Instance Variables
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_iv_get(VALUE obj, const char *name)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Retrieve the value of the instance variable. If the name is not
|
|
|
|
prefixed by `@', that variable shall be inaccessible from Ruby.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Sets the value of the instance variable.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
** Control Structure
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_iterate(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Calls the function func1, supplying func2 as the block. func1 will be
|
|
|
|
called with the argument arg1. func2 receives the value from yield as
|
|
|
|
the first argument, arg2 as the second argument.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_yield(VALUE val)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Evaluates the block with value val.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_rescue(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Calls the function func1, with arg1 as the argument. If exception
|
|
|
|
occurs during func1, it calls func2 with arg2 as the argument. The
|
|
|
|
return value of rb_rescue() is the return value from func1 if no
|
|
|
|
exception occurs, from func2 otherwise.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_ensure(VALUE (*func1)(), void *arg1, void (*func2)(), void *arg2)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Calls the function func1 with arg1 as the argument, then calls func2
|
|
|
|
with arg2, whenever execution terminated. The return value from
|
|
|
|
rb_ensure() is that of func1.
|
|
|
|
|
|
|
|
** Exceptions and Errors
|
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_warn(const char *fmt, ...)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Prints warning message according to the printf-like format.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_warning(const char *fmt, ...)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Prints warning message according to the printf-like format, if
|
|
|
|
$VERBOSE is true.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_raise(VALUE exception, const char *fmt, ...)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Raises an exception of class exception. The fmt is the format string
|
|
|
|
just like printf().
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_fatal(const char *fmt, ...)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Raises fatal error, terminates the interpreter. No exception handling
|
|
|
|
will be done for fatal error, but ensure blocks will be executed.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-05 13:57:04 +04:00
|
|
|
void rb_bug(const char *fmt, ...)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
Terminates the interpreter immediately. This function should be
|
1999-08-13 09:45:20 +04:00
|
|
|
called under the situation caused by the bug in the interpreter. No
|
|
|
|
exception handling nor ensure execution will be done.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
** Initialize and Starts the Interpreter
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
The embedding API are below (not needed for extension libraries):
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-10-04 08:51:08 +04:00
|
|
|
void ruby_init()
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Initializes the interpreter.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-10-04 08:51:08 +04:00
|
|
|
void ruby_options(int argc, char **argv)
|
|
|
|
|
|
|
|
Process command line arguments for the interpreter.
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void ruby_run()
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Starts execution of the interpreter.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
void ruby_script(char *name)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Specifies the name of the script ($0).
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-11-29 18:30:54 +03:00
|
|
|
Appendix C. Functions Available in extconf.rb
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
These functions are available in extconf.rb:
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
have_library(lib, func)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Checks whether library which contains specified function exists.
|
|
|
|
Returns true if the library exists.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-10 22:38:11 +04:00
|
|
|
find_library(lib, func, path...)
|
|
|
|
|
|
|
|
Checks whether library which contains specified function exists in
|
|
|
|
path. Returns true if the library exists.
|
|
|
|
|
2000-07-07 07:20:53 +04:00
|
|
|
have_func(func, header)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-07 07:20:53 +04:00
|
|
|
Checks whether func exists with header. Returns true if the function
|
|
|
|
exists. To check functions in the additional library, you need to
|
|
|
|
check that library first using have_library().
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
have_header(header)
|
|
|
|
|
2001-05-10 22:38:11 +04:00
|
|
|
Checks whether header exists. Returns true if the header file exists.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
create_makefile(target)
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
Generates the Makefile for the extension library. If you don't invoke
|
|
|
|
this method, the compilation will not be done.
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-10 22:38:11 +04:00
|
|
|
with_config(withval[, default=nil])
|
|
|
|
|
|
|
|
Parses the command line options and returns the value specified by
|
|
|
|
--with-<withval>.
|
|
|
|
|
|
|
|
dir_config(target[, default_dir])
|
|
|
|
dir_config(target[, default_include, default_lib])
|
|
|
|
|
|
|
|
Parses the command line options and adds the directories specified by
|
|
|
|
--with-<target>-dir, --with-<target>-include, and/or --with-<target>-lib
|
|
|
|
to $CFLAGS and/or $LDFLAGS. --with-<target>-dir=/path is equivalent to
|
|
|
|
--with-<target>-include=/path/include --with-<target>-lib=/path/lib.
|
|
|
|
Returns an array of the added directories ([include_dir, lib_dir]).
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
/*
|
|
|
|
* Local variables:
|
1999-08-13 09:45:20 +04:00
|
|
|
* fill-column: 70
|
1998-01-16 15:13:05 +03:00
|
|
|
* end:
|
|
|
|
*/
|