Commit 76f24d21 authored by Seba Gamboa's avatar Seba Gamboa

Testing yard generation

parent 2d29d140
......@@ -21,3 +21,5 @@ cscope.out
/bin
/build
/mruby-source-*.gem
doc
.yardoc
-
AUTHORS
MITL
CONTRIBUTING.md
guides/*.md
# C API Reference
This is a C API Reference.
The structure of this document will follow the directory structure of `include/` directory.
## Headers list
Header name|Features
-----------|--------
[mrbconf.h](../mrbconf/README.md)|Defines macros for mruby configurations.
[mruby.h](./mruby.h.md)|Main header of mruby C API. Include this first.
[mruby/array.h](./mruby/array.h.md)|`Array` class.
[mruby/class.h](./mruby/class.h.md)|`Class` class.
[mruby/compile.h](./mruby/compile.h.md)|mruby compiler.
[mruby/data.h](./mruby/data.h.md)|User defined object.
[mruby/debug.h](./mruby/debug.h.md)|Debugging.
[mruby/dump.h](./mruby/dump.h.md)|Dumping compiled mruby script.
[mruby/error.h](./mruby/error.h.md)|Error handling.
[mruby/gc.h](./mruby/gc.h.md)|Uncommon memory management stuffs.
[mruby/hash.h](./mruby/hash.h.md)|`Hash` class.
[mruby/irep.h](./mruby/irep.h.md)|Compiled mruby script.
[mruby/khash.h](./mruby/khash.h.md)|Defines of khash which is used in hash table of mruby.
[mruby/numeric.h](./mruby/numeric.h.md)|`Numeric` class and sub-classes of it.
[mruby/opode.h](./mruby/opcode.h.md)|Operation codes used in mruby VM.
[mruby/proc.h](./mruby/proc.h.md)|`Proc` class.
[mruby/range.h](./mruby/range.h.md)|`Range` class.
[mruby/re.h](./mruby/re.h.md)|`Regexp` class.
[mruby/string.h](./mruby/string.h.md)|`String` class.
[mruby/value.h](./mruby/value.h.md)|`mrb_value` functions and macros.
[mruby/variable.h](./mruby/variable.h.md)|Functions to access to mruby variables.
[mruby/version.h](./mruby/version.h.md)|Macros of mruby version.
# mruby.h
Basic header of mruby.
It includes **mrbconf.h**, **mruby/value.h**, **mruby/version.h** internally.
## `mrb_state` management
### mrb_open
```C
mrb_state* mrb_open();
```
Creates new `mrb_state`.
### mrb_allocf
```C
typedef void* (*mrb_allocf) (struct mrb_state *mrb, void *ptr, size_t s, void *ud);
```
Function pointer type of custom allocator used in `mrb_open_allocf`.
The function pointing it must behave similarly as `realloc` except:
* If `ptr` is `NULL` it must allocate new space.
* If `s` is `NULL`, `ptr` must be freed.
### mrb_open_allocf
```C
mrb_state* mrb_open_allocf(mrb_allocf f, void *ud);
```
Create new `mrb_state` with custom allocator.
`ud` will be passed to custom allocator `f`.
If user data isn't required just pass `NULL`.
Function pointer `f` must satisfy requirements of its type.
### mrb_close
```C
void mrb_close(mrb_state *mrb);
```
Deletes `mrb_state`.
## Method
### mrb_get_args
```C
int mrb_get_args(mrb_state *mrb, const char *format, ...);
```
Retrieve arguments from `mrb_state`.
When applicable, implicit conversions (such as `to_str`,
`to_ary`, `to_hash`) are applied to received arguments.
Use it inside a function pointed by `mrb_func_t`.
It returns the number of arguments retrieved.
`format` is a list of following format specifiers:
char|mruby type|retrieve types|note
:---:|----------|--------------|---
`o`|`Object`|`mrb_value`|Could be used to retrieve any type of argument
`C`|`Class`/`Module`|`mrb_value`|
`S`|`String`|`mrb_value`|when ! follows, the value may be nil
`A`|`Array`|`mrb_value`|when ! follows, the value may be nil
`H`|`Hash`|`mrb_value`|when ! follows, the value may be nil
`s`|`String`|`char*`, `mrb_int`|Receive two arguments; s! gives (NULL,0) for nil
`z`|`String`|`char*`|NUL terminated string; z! gives NULL for nil
`a`|`Array`|`mrb_value*`, `mrb_int`|Receive two arguments; a! gives (NULL,0) for nil
`f`|`Float`|`mrb_float`|
`i`|`Integer`|`mrb_int`|
`b`|boolean|`mrb_bool`|
`n`|`Symbol`|`mrb_sym`|
`&`|block|`mrb_value`|
`*`|rest arguments|`mrb_value*`, `mrb_int`|Receive the rest of arguments as an array.
<code>&#124;</code>|optional||After this spec following specs would be optional.
`?`|optional given|`mrb_bool`|True if preceding argument is given. Used to check optional argument is given.
The passing variadic arguments must be a pointer of retrieving type.
### mrb_define_class
```C
MRB_API struct RClass *mrb_define_class(mrb_state *, const char*, struct RClass*);
```
Defines a new class. If you're creating a gem it may look something like this:
```C
void mrb_example_gem_init(mrb_state* mrb) {
struct RClass *example_class;
example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class);
}
void mrb_example_gem_final(mrb_state* mrb) {
//free(TheAnimals);
}
```
### mrb_define_method
```C
MRB_API void mrb_define_method(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec);
```
Defines a global function in ruby. If you're creating a gem it may look something like this:
```C
mrb_value example_method(mrb_state* mrb, mrb_value self){
puts("Executing example command!");
return self;
}
void mrb_example_gem_init(mrb_state* mrb) {
mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE());
}
void mrb_example_gem_final(mrb_state* mrb) {
//free(TheAnimals);
}
```
Or maybe you want to create a class method for a class? It might look something like this:
```C
mrb_value example_method(mrb_state* mrb, mrb_value self){
puts("Examples are like pizza...");
return self;
}
void mrb_example_gem_init(mrb_state* mrb) {
struct RClass *example_class;
example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class);
mrb_define_method(mrb, example_class, "example_method", example_method, MRB_ARGS_NONE());
}
void mrb_example_gem_final(mrb_state* mrb) {
//free(TheAnimals);
}
```
### mrb_define_module
```C
MRB_API struct RClass *mrb_define_module(mrb_state *, const char*);
```
Defines a module. If you're creating a gem it may look something like this:
```C
mrb_value example_method(mrb_state* mrb, mrb_value self){
puts("Examples are like tacos...");
return self;
}
void mrb_example_gem_init(mrb_state* mrb) {
struct RClass *example_module;
example_module = mrb_define_module(mrb, "Example_Module");
}
void mrb_example_gem_final(mrb_state* mrb) {
//free(TheAnimals);
}
```
### mrb_define_module_function
```C
MRB_API void mrb_define_module_function(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec);
```
Defines a module function. If you're creating a gem it may look something like this:
```C
mrb_value example_method(mrb_state* mrb, mrb_value self){
puts("Examples are like hot wings...");
return self;
}
void mrb_example_gem_init(mrb_state* mrb) {
struct RClass *example_module;
example_module = mrb_define_module(mrb, "Example_Module");
mrb_define_module_function(mrb, example_module, "example_method", example_method, MRB_ARGS_NONE());
}
void mrb_example_gem_final(mrb_state* mrb) {
//free(TheAnimals);
}
```
### mrb_define_const
```C
MRB_API void mrb_define_const(mrb_state*, struct RClass*, const char *name, mrb_value);
```
Defines a constant. If you're creating a gem it may look something like this:
```C
mrb_value example_method(mrb_state* mrb, mrb_value self){
puts("Examples are like hot wings...");
return self;
}
void mrb_example_gem_init(mrb_state* mrb) {
mrb_define_const(mrb, mrb->kernel_module, "EXAPMLE_CONSTANT", mrb_fixnum_value(0x00000001));
}
void mrb_example_gem_final(mrb_state* mrb) {
//free(TheAnimals);
}
```
### mrb_str_new_cstr
```C
MRB_API mrb_value mrb_str_new_cstr(mrb_state*, const char*);
```
Turns a C string into a Ruby string value.
### mrb_value mrb_funcall
```C
MRB_API mrb_value mrb_funcall(mrb_state*, mrb_value, const char*, mrb_int,...);
```
Call existing ruby functions.
### mrb_ary_new
```C
mrb_value mrb_ary_new(mrb_state *mrb);
```
Initializes an array.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case we are declaring a variable new_ary of data type mrb_value. Then we are initializing it with the mrb_ary_new function which only takes an mruby state as an argument.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value new_ary; // Declare variable.
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb);
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, new_ary);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
### mrb_ary_push
```C
void mrb_ary_push(mrb_state*, mrb_value, mrb_value);
```
Pushes given value into an array.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case after initializing our array. We are declaring two variables with the mrb_int data type random_value1 & random_value2 and we initialize them 70 and 60 respectively. Then we use the mrb_ary_push function to push values those values into the array.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value new_ary; // Declare variable.
mrb_int random_value1 = 70; // Initialize variable
mrb_int random_value2 = 60; // Initialize variable
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb); // Initialize ruby array.
/* Pushes the fixnum value from random_value1 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value1));
/* Pushes the fixnum value from random_value2 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value2));
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, new_ary);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
[70, 60]
Array
```
## mrb_ary_pop
```C
mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
```
Pops the last element from the array.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case after initializing our array. We are declaring two variables with the mrb_int data type random_value1 & random_value2 and we initialize them 70 and 60 respectively. Then we use the mrb_ary_push function to push values those values into the array. Now here in the Ruby files we add another method
called pop_ary that will return the array alone(just to be clean) and you should see the last element gone.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value new_ary; // Declare variable.
mrb_int random_value1 = 70; // Initialize variable
mrb_int random_value2 = 60; // Initialize variable
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb); // Initialize ruby array.
/* Pushes the fixnum value from random_value1 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value1));
/* Pushes the fixnum value from random_value2 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value2));
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, new_ary);
mrb_ary_pop(mrb, new_ary); // Pops the last element of the array. In this case 60.
mrb_funcall(mrb, obj, "pop_ary", 1, new_ary); // Calls the method again to show the results.
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
def pop_ary(a)
puts a
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
[70, 60]
Array
[70]
```
## mrb_ary_ref
```C
mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n);
```
Returns a reference to an element of the array. Specified by the value given to mrb_int n.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case we're declaring a variable ary_ref with the data type of mrb_value. Then we assign mrb_ary_ref to it getting new_ary's value at index 1.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value ary_ref; // Declare variable.
mrb_value new_ary; // Declare variable.
mrb_int random_value1 = 70; // Initialize variable
mrb_int random_value2 = 60; // Initialize variable
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb); // Initialize ruby array.
/* Pushes the fixnum value from random_value1 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value1));
/* Pushes the fixnum value from random_value2 to the new_ary instance. */
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value2));
ary_ref = mrb_ary_ref(mrb, new_ary, 1); // Gets the value of new_ary's second element at index 1.
mrb_value obj = mrb_load_file(mrb,fp);
/* Passing the value from ary_ref to the method method_name.*/
mrb_funcall(mrb, obj, "method_name", 1, ary_ref);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
60
Fixnum
```
### mrb_ary_set
```C
void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
```
Sets a value to an index.
#### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument and what class the passed in value is. In this case we're declaring a variable ary_ref with the data type of mrb_value. Then we assign mrb_ary_ref to it getting new_ary's value at index 1.
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/array.h" // Needs the array header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_value new_ary;
mrb_value ary_obj;
mrb_int random_value1 = 70;
mrb_int random_value2 = 60;
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
new_ary = mrb_ary_new(mrb);
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value1));
mrb_ary_push(mrb, new_ary, mrb_fixnum_value(random_value2));
/* Sets the fixnum value of 7 to the second index of the array.*/
mrb_ary_set(mrb, new_ary, 2, mrb_fixnum_value(7));
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "before_after", 1, new_ary);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
def before_after(a)
puts a
end
end
Example_Class.new
```
#### Result
After compiling you should get these results.
```Ruby
[70, 60, 7]
```
This diff is collapsed.
#### mrb_range_new
```C
mrb_value mrb_range_new(mrb_state*, mrb_value, mrb_value, mrb_bool);
```
Initializes a Range. The first mrb_value being the beginning value and second being the ending value.
The third parameter is an mrb_bool value that represents the inclusion or exclusion of the last value.
If the third parameter is 0 then it includes the last value in the range. If the third parameter is 1
then it excludes the last value in the range.
C code
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/range.h" // Needs the range header.
#include "mruby/compile.h"
int main(int argc, char *argv[])
{
mrb_int beg = 0;
mrb_int end = 2;
mrb_bool exclude = 1;
mrb_value range_obj;
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
range_obj = mrb_range_new(mrb, mrb_fixnum_value(beg), mrb_fixnum_value(end), exclude);
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, range_obj);
fclose(fp);
mrb_close(mrb);
return 0;
}
```
Ruby code
```Ruby
class Example_Class
def method_name(a)
puts a
puts a.class
end
end
Example_Class.new
```
This returns the following:
```Ruby
0...2
Range
```
### Macros
#### REGEXP_CLASS
A string with the name of the REGEXP class.
## Macros
### mrb_str_ptr(s)
Returns a pointer from a Ruby string.
## Functions
### mrb_str_plus
```C
mrb_value mrb_str_plus(mrb_state*, mrb_value, mrb_value);
```
Adds to strings together.
### mrb_ptr_to_str
```C
mrb_value mrb_ptr_to_str(mrb_state *, void*);
```
Converts pointer into a Ruby string.
### mrb_obj_as_string
```C
mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
```
Returns an object as a Ruby string.
### mrb_str_resize
```C
mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
```
Resizes the string's length.
### mrb_str_substr
```C
mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
```
Returns a sub string.
### mrb_string_type
```C
mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
```
Returns a Ruby string type.
### mrb_str_new_cstr
```C
const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
```
Returns a Ruby string as a C string.
### mrb_str_dup
```C
mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
```
Duplicates a string object.
### mrb_str_intern
```C
mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
```
Returns a symbol from a passed in string.
### mrb_str_to_str
```C
mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str);
```
Returns a converted string type.
### mrb_str_equal
```C
mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
```
Returns true if the strings match and false if the strings don't match.
### mrb_str_cat
```C
mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
```
Returns a concated string comprised of a Ruby string and a C string.
### mrb_str_cat_cstr
```C
mrb_value mrb_str_cat_str(mrb_state *mrb, mrb_value str, mrb_value str2);
```
Returns a concated string comprised of a Ruby string and a C string(A shorter alternative to mrb_str_cat).
### mrb_str_append
```C
mrb_value mrb_str_append(mrb_state *mrb, mrb_value str1, mrb_value str2);
```
Adds str2 to the end of str1.
### mrb_str_cmp
```C
int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
```
Returns 0 if both Ruby strings are equal.
Returns a value < 0 if Ruby str1 is less than Ruby str2.
Returns a value > 0 if Ruby str2 is greater than Ruby str1.
### mrb_str_to_cstr
```C
char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
```
Returns a C string from a Ruby string.
### mrb_str_inspect
```C
mrb_str_inspect(mrb_state *mrb, mrb_value str);
```
Returns a printable version of str, surrounded by quote marks, with special characters escaped.
### mrb_float_value
```C
static inline mrb_value mrb_float_value(struct mrb_state *mrb, mrb_float f)
```
Returns a float in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in mrb_float f = 0.09. Alternatively
double i = 0.09 could also be used.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
#include "mruby/string.h"
int
main(void)
{
mrb_float f = 0.09;// or double i = 0.09;
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_float_value(mrb, f));
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### mrb_fixnum_value
```C
static inline mrb_value mrb_fixnum_value(mrb_int i)
```
Returns a fixnum in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in mrb_int i = 99. Alternativly int i = 99
could also be used.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
int
main(void)
{
mrb_int i = 99; // or int i = 99;
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_fixnum_value(i));
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### mrb_nil_value
```C
static inline mrb_value mrb_nil_value(void)
```
Returns nil in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in nothing and we will get NillClass.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
int
main(void)
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_nil_value());
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### mrb_false_value
```C
static inline mrb_value mrb_false_value(void)
```
Returns false in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in nothing and we will get FalseClass.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
int
main(void)
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_false_value());
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### mrb_true_value
```C
static inline mrb_value mrb_true_value(void)
```
Returns true in Ruby.
##### Example
In this example we read from a Ruby file inside C. The Ruby code will print what you pass as an argument
and what class the passed in value is. In this case we are passing in nothing and we will get TrueClass.
example.c
```C
#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
int
main(void)
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
FILE *fp = fopen("test.rb","r");
mrb_value obj = mrb_load_file(mrb,fp);
mrb_funcall(mrb, obj, "method_name", 1, mrb_true_value());
fclose(fp);
mrb_close(mrb);
return 0;
}
```
test.rb
```Ruby
class My_Class
def method_name(s)
puts s
puts s.class
end
end
a = My_Class.new
```
### Macros
#### MRUBY_RUBY_VERSION
The version of Ruby used by mruby.
#### MRUBY_RUBY_ENGINE
Ruby engine.
#### MRUBY_VERSION
The mruby version.
#### MRUBY_RELEASE_MAJOR
Major release version.
#### MRUBY_RELEASE_MINOR
Minor release version.
#### MRUBY_RELEASE_NO
Release number.
#### MRUBY_RELEASE_DATE
Release date as a string.
#### MRUBY_RELEASE_YEAR
Release year.
#### MRUBY_RELEASE_MONTH
Release month.
#### MRUBY_RELEASE_DAY
Release day.
#### MRUBY_BIRTH_YEAR
The year mruby was first created.
#### MRUBY_AUTHOR
Mruby's authors.
#### MRB_STRINGIZE0(expr)
A passed in expression.
#### MRB_STRINGIZE(expr)
Passes in an expression to MRB_STRINGIZE0.
#### MRUBY_DESCRIPTION
mruby's version, and release date.
#### MRUBY_COPYRIGHT
mruby's copyright information.
This diff is collapsed.
# Language
mruby is an implementation of the Ruby programming language.
These documents are describing the language features and libraries
which are provided together with mruby.
## Built-In Class and Modules
see *doc/language/Core.md*
#!/usr/bin/env ruby
require 'pty'
c_dir = File.dirname(__FILE__)
MRUBY_ROOT = File.expand_path("#{c_dir}/../..")
DOC_DIR = File.expand_path(c_dir)
cmd = "ruby #{DOC_DIR}/mrbdoc/mrbdoc.rb #{MRUBY_ROOT} #{DOC_DIR} false"
IO.popen(cmd, "r+") do |io|
io.close_write
while line = io.gets
puts line
end
end
class MRBDoc
SRC_DIR = 'src'
MRBLIB_DIR = 'mrblib'
def analyze_code dir, &block
@mrb_files = {}
@dir = File.expand_path(dir)
block.call "MRBDOC\tanalyze #{@dir}"
analyze(dir) do |progress|
block.call progress
end
end
def each_file(&block); @mrb_files.each {|k,v| block.call k,v}; end
def find_c_func(c_func_name)
each_file do |file_name, file|
c_func = file.c_funcs(c_func_name)
return c_func unless c_func.nil?
end
{}
end
def find_c_file(rb_obj_name, c_func_name)
last_file_name_match = ''
each_file do |file_name, file|
c_func = file.c_funcs(c_func_name)
if c_func and file.rb_class(rb_obj_name) or file.rb_module(rb_obj_name)
return file_name
elsif c_func
last_file_name_match = file_name
end
end
last_file_name_match
end
def find_c_file_by_class(name)
each_file do |file_name, file|
rb_class = file.rb_class(name)
return file_name unless rb_class.nil?
end
'nil'
end
def find_c_file_by_module(name)
each_file do |file_name, file|
rb_module = file.rb_module(name)
return file_name unless rb_module.nil?
end
'nil'
end
private
def analyze dir, &block
collect_all_files dir, &block
end
def collect_all_files dir, &block
l = lambda {|f| block.call " - #{f.name}"}
collect_files(src_code_dir(dir), /\.c$/, &l)
collect_files(mrb_code_dir(dir), /\.rb$/, &l)
end
def collect_files dir, rxp, &block
Dir.foreach(dir) do |file|
next unless file =~ rxp
file_path = "#{dir}/#{file}"
mrb_file = MRBFile.new "#{file_path}"
@mrb_files["#{file_path}"] = mrb_file
block.call mrb_file
end
end
def src_code_dir dir; File.expand_path SRC_DIR, dir; end
def mrb_code_dir dir; File.expand_path MRBLIB_DIR, dir; end
end
class MRBFile
attr_reader :name
attr_reader :file
def initialize mrb_file
@file = mrb_file
@name = File.basename file
@c_funcs = {}
@rb_class_c_def = {}
@rb_method_c_def = {}
@rb_class_method_c_def = {}
@rb_module_c_def = {}
@last_line = nil
@assignments = {}
@assignments['mrb->object_class'] = 'Object'
@assignments['mrb->kernel_module'] = 'Kernel'
@assignments['mrb->module_class'] = 'Module'
@assignments['mrb->nil_class'] = 'NilClass'
@assignments['mrb->true_class'] = 'TrueClass'
@assignments['mrb->class_class'] = 'Class'
analyze
end
def each_class &block
@rb_class_c_def.each do |class_name, class_hsh|
block.call class_name, class_hsh
end
end
def each_method name, &block
@rb_method_c_def.each do |met_name, met_hsh|
met_name_tmp = met_name.sub /^#{name}_/, ''
block.call met_name_tmp, met_hsh if met_hsh[:rb_class] == name
end
end
def each_class_method name, &block
@rb_class_method_c_def.each do |met_name, met_hsh|
met_name_tmp = met_name.sub /^#{name}_/, ''
block.call met_name_tmp, met_hsh if met_hsh[:rb_class] == name
end
end
def each_module &block
@rb_module_c_def.each do |module_name, module_hsh|
block.call module_name, module_hsh
end
end
def each_core_object &block
each_class {|n| block.call n}
each_module {|n| block.call n}
end
def c_funcs c_func_name; @c_funcs[c_func_name]; end
def rb_class rb_class_name; @rb_class_c_def[rb_class_name]; end
def rb_module rb_module_name; @rb_module_c_def[rb_module_name]; end
private
def analyze
File.open(file).each_line.each_with_index do |line, idx|
line_no = idx.succ
if c_file?
analyze_c_line line, line_no
elsif rb_file?
analyze_rb_line line, line_no
else
raise ArgumentError.new "#{file} is a not supported file type"
end
@last_line = line.strip
end
end
def c_file?; (name =~ /\.c$/); end
def rb_file?; (name =~ /\.rb$/); end
RXP_C_VAR = /\s*([^\s]*?)\s*?/
RXP_C_STR = /\s*?\"(.*?)\"\s*?/
#RXP_C_ISO = /\s*\;\s*[\/\*]*\s*.*?([15\.]{0,3}[0-9\.]*)\s*[\\\\\*]*/
RXP_C_ISO = /\s*;\s*[\/\*]*[\sa-zA-Z]*([\d\.]*)[\sa-zA-Z]*[\*\/]*/
def analyze_c_line line, line_no
case line.strip
when /^([a-zA-Z\_][a-zA-Z\_0-9]*?)\((.*?)\)\s*?$/
# assuming c method definition
@c_funcs[$1] = {:line_no => line_no, :args => $2, :return => @last_line}
when /mrb_define_class\(.*?\,#{RXP_C_STR}\,#{RXP_C_VAR}\)#{RXP_C_ISO}/
# assuming ruby class definition in c
class_name = $1.clone
iso = $3.clone
iso.strip!
@rb_class_c_def[class_name] = {:c_object => $2, :iso => iso}
assigns = line.split '='
if assigns.size > 1
assigns[0..-2].each do |v|
@assignments[v.strip] = class_name
end
end
when /mrb_define_module\(.*?\,#{RXP_C_STR}\)#{RXP_C_ISO}/
# assuming ruby class definition in c
module_name = $1.clone
iso = $2.clone
iso.strip!
@rb_module_c_def[module_name] = {:iso => iso}
assigns = line.split '='
if assigns.size > 1
assigns[0..-2].each do |v|
@assignments[v.strip] = module_name
end
end
when /mrb_define_method\(.*?\,#{RXP_C_VAR}\,#{RXP_C_STR}\,#{RXP_C_VAR}\,#{RXP_C_VAR}\)#{RXP_C_ISO}/
# assuming ruby method definition in c
name = $1.clone
name = resolve_obj(name)
iso = $5.clone
iso.strip!
@rb_method_c_def["#{name}_#{$2}"] = {:c_func => $3, :args => $4, :rb_class => name, :iso => iso}
when /mrb_define_class_method\(.*?\,#{RXP_C_VAR}\,#{RXP_C_STR}\,#{RXP_C_VAR}\,#{RXP_C_VAR}\)#{RXP_C_ISO}/
# assuming ruby class method definition in c
class_name = $1.clone
class_name = resolve_obj(class_name)
iso = $5.clone
iso.strip!
@rb_class_method_c_def["#{class_name}_#{$2}"] = {:c_func => $3, :args => $4, :rb_class => class_name, :iso => iso}
when /mrb_name_class\(.*?\,#{RXP_C_VAR}\,\s*mrb_intern\(.*?,#{RXP_C_STR}\)\)#{RXP_C_ISO}/
class_name = $2.clone
iso = $3.clone
iso.strip!
@rb_class_c_def[class_name] = {:c_object => $1, :iso => iso}
@assignments[$1] = class_name
when /mrb_include_module\(.*?\,#{RXP_C_VAR}\,\s*mrb_class_get\(.*?\,#{RXP_C_STR}\)\)/
class_name = resolve_obj($1)
mod = $2.clone
@rb_class_c_def[class_name][:include] = [] unless @rb_class_c_def[class_name].has_key? :include
@rb_class_c_def[class_name][:include] << mod
end
end
def analyze_rb_line line, line_no
end
def resolve_obj c_var
@assignments[c_var]
end
end
class MRBDoc
def write_documentation dir, cfg, &block
block.call "MRBDOC\twrite to #{File.expand_path(dir)}"
write(dir, cfg) do |progress|
block.call progress
end
end
private
def write dir, cfg
File.open(File.expand_path('Core.md', dir), 'wb+') do |io|
print_core_classes(io, cfg)
print_core_modules(io, cfg)
end
end
def get_core_list id
core_list = {}
each_file do |file_path, mrb_file|
mrb_file.send(id) do |name, cls_hsh|
core_list[name] = {:data => cls_hsh, :methods => {}, :class_methods => {}}
mrb_file.each_method name do |met_name, met_hsh|
core_list[name][:methods][met_name] = met_hsh
end
mrb_file.each_class_method name do |met_name, met_hsh|
core_list[name][:class_methods][met_name] = met_hsh
end
end
end
core_list
end
def print_core_classes(io, cfg)
core_list = get_core_list :each_class
io.puts "# Core Classes\n\n"
core_list.sort.each do |name, hsh|
file = find_c_file_by_class(name)
file = file.split("#{@dir}/")[1]
iso = hsh[:data][:iso]
iso = 'n/a' if iso.nil? or iso == ''
mixins = hsh[:data][:include].join(', ') unless hsh[:data][:include].nil?
mixins = 'n/a' if mixins.nil? or mixins == ''
io.puts <<CLASS
## #{name}
ISO Code | Mixins | Source File
--- | --- | ---
#{iso} | #{mixins} | #{file}
CLASS
print_class_methods(io, hsh, cfg)
print_methods(io, hsh, cfg)
end
end
def print_core_modules(io, cfg)
core_list = get_core_list :each_module
io.puts "# Core Modules\n\n"
core_list.sort.each do |name, hsh|
file = find_c_file_by_module(name)
file = file.split("#{@dir}/")[1]
iso = hsh[:data][:iso]
iso = 'n/a' if iso.nil? or iso == ''
io.puts <<CLASS
## #{name}
ISO Code | Source File
--- | ---
#{iso} | #{file}
CLASS
print_class_methods(io, hsh, cfg)
print_methods(io, hsh, cfg)
end
end
def print_methods(io, hsh, cfg)
return unless hsh[:methods].size > 0
io.puts "### Methods\n\n"
hsh[:methods].sort.each do |met_name, met_hsh|
print_method(io, met_name, met_hsh, cfg)
end
end
def print_class_methods(io, hsh, cfg)
return unless hsh[:class_methods].size > 0
io.puts "### Class Methods\n\n"
hsh[:class_methods].sort.each do |met_name, met_hsh|
print_method(io, met_name, met_hsh, cfg)
end
end
def print_method(io, met_name, met_hsh, cfg)
if cfg[:print_line_no]
line_no_head = ' | Line'
line_no = " | #{find_c_func(met_hsh[:c_func])[:line_no]}"
else
line_no, line_no_head = '', ''
end
file = find_c_file(met_hsh[:rb_class], met_hsh[:c_func])
file = file.split("#{@dir}/")[1]
iso = met_hsh[:iso]
iso = 'n/a' if iso.nil? or iso == ''
io.puts <<METHOD
#### #{met_name}
ISO Code | Source File | C Function#{line_no_head}
--- | --- | ---
#{iso} | #{file} | #{met_hsh[:c_func]}#{line_no}
METHOD
end
end
#!/usr/bin/env ruby
$: << File.dirname(__FILE__) + '/lib'
require 'mrbdoc_analyze'
require 'mrbdoc_docu'
MRUBY_ROOT = ARGV[0]
DOC_ROOT = ARGV[1]
_WRITE_LINE_NO = ARGV[2]
STDOUT.sync = true
raise ArgumentError.new 'mruby root missing!' if MRUBY_ROOT.nil?
raise ArgumentError.new 'doc root missing!' if DOC_ROOT.nil?
if _WRITE_LINE_NO.nil?
WRITE_LINE_NO = true
else
case _WRITE_LINE_NO
when 'true'
WRITE_LINE_NO = true
when 'false'
WRITE_LINE_NO = false
else
raise ArgumentError.new 'Line no parameter has to be false or true!'
end
end
mrbdoc = MRBDoc.new
mrbdoc.analyze_code MRUBY_ROOT do |progress|
puts progress
end
cfg = {:print_line_no => WRITE_LINE_NO}
mrbdoc.write_documentation DOC_ROOT, cfg do |progress|
puts progress
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment