Commit 02a6d866 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2984 from sagmor/yard

Simpler documentation
parents 0050bfe5 87e0840e
......@@ -21,3 +21,5 @@ cscope.out
/bin
/build
/mruby-source-*.gem
doc/api
.yardoc
--plugin mruby
--plugin coderay
--output-dir doc/api
-
AUTHORS
MITL
CONTRIBUTING.md
doc/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
......@@ -38,9 +38,7 @@
#include "mruby/version.h"
/**
* @file mruby.h
* @defgroup mruby MRuby C API
* @{
* MRuby C API entry point
*/
MRB_BEGIN_DECL
......@@ -56,13 +54,13 @@ struct mrb_irep;
struct mrb_state;
/**
* Function pointer type of custom allocator used in @ref mrb_open_allocf.
* Function pointer type of custom allocator used in @see 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.
*
* See @ref mrb_default_allocf for the default implementation.
* See @see mrb_default_allocf for the default implementation.
*/
typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t, void *ud);
......@@ -213,21 +211,25 @@ typedef mrb_value (*mrb_func_t)(mrb_state *mrb, mrb_value);
* Defines a new class.
*
* If you're creating a gem it may look something like this:
* <pre>
* 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);
* }
* </pre>
* !!!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);
* }
*
* @param mrb The current mruby state.
* @param name The name of the defined class
* @param super The new class parent
* @return Reference to the newly defined class
* @see mrb_define_class_under
*/
MRB_API struct RClass *mrb_define_class(mrb_state *mrb, const char *name, struct RClass *super);
MRB_API struct RClass *mrb_define_module(mrb_state *, const char*);
MRB_API mrb_value mrb_singleton_class(mrb_state*, mrb_value);
MRB_API void mrb_include_module(mrb_state*, struct RClass*, struct RClass*);
......@@ -238,6 +240,7 @@ MRB_API void mrb_prepend_module(mrb_state*, struct RClass*, struct RClass*);
*
* 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!");
......@@ -249,15 +252,10 @@ MRB_API void mrb_prepend_module(mrb_state*, struct RClass*, struct RClass*);
* mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE());
* }
*
* @param mrb
* The MRuby state reference.
* @param cla
* The class pointer where the method will be defined.
* @param func
* The function pointer to the method definition.
* @param aspec
* The method parameters declaration.
* See @ref mruby_mrb_aspec for details.
* @param mrb The MRuby state reference.
* @param cla The class pointer where the method will be defined.
* @param func The function pointer to the method definition.
* @param aspec The method parameters declaration.
*/
MRB_API void mrb_define_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec);
......@@ -271,20 +269,15 @@ MRB_API void mrb_undef_class_method(mrb_state*, struct RClass*, const char*);
/**
* Initialize a new object instace of c class.
*
* @param mrb
* The current mruby state.
* @param c
* Reference to the class of the new object.
* @param argc
* Number of arguments in argv
* @param argv
* Array of @ref mrb_value "mrb_values" to initialize the object
* @returns
* The newly initialized object
* @param mrb The current mruby state.
* @param c Reference to the class of the new object.
* @param argc Number of arguments in argv
* @param argv Array of mrb_value to initialize the object
* @return The newly initialized object
*/
MRB_API mrb_value mrb_obj_new(mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *argv);
/** See @ref mrb_obj_new */
/** @see mrb_obj_new */
MRB_INLINE mrb_value mrb_class_new_instance(mrb_state *mrb, mrb_int argc, const mrb_value *argv, struct RClass *c)
{
return mrb_obj_new(mrb,c,argc,argv);
......@@ -303,17 +296,20 @@ MRB_API mrb_value mrb_notimplement_m(mrb_state*, mrb_value);
MRB_API mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
MRB_API mrb_value mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method);
MRB_API mrb_bool mrb_obj_respond_to(mrb_state *mrb, struct RClass* c, mrb_sym mid);
MRB_API struct RClass * mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super);
MRB_API struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name);
/**
* @defgroup mruby_mrb_aspec Required arguments declaration helpers.
*
* Helper functions to declare arguments requirements on methods declared by
* \ref mrb_define_method and the like
* Defines a new class under a given module
*
* @{
* @param mrb The current mruby state.
* @param outer Reference to the module under which the new class will be defined
* @param name The name of the defined class
* @param super The new class parent
* @return Reference to the newly defined class
* @see mrb_define_class
*/
MRB_API struct RClass * mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super);
MRB_API struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name);
/**
* Function requires n arguments.
......@@ -365,8 +361,6 @@ MRB_API struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *o
*/
#define MRB_ARGS_NONE() ((mrb_aspec)0)
/** @} */
/**
* Format specifiers for \ref mrb_get_args function
*
......@@ -400,14 +394,10 @@ typedef const char *mrb_args_format;
* applied to received arguments.
* Use it inside a function pointed by mrb_func_t.
*
* @param mrb
* The current MRuby state.
* @param format
* is a list of format specifiers see @ref mrb_args_format
* @param ...
* The passing variadic arguments must be a pointer of retrieving type.
* @return
* the number of arguments retrieved.
* @param mrb The current MRuby state.
* @param format is a list of format specifiers see @ref mrb_args_format
* @param ... The passing variadic arguments must be a pointer of retrieving type.
* @return the number of arguments retrieved.
*/
MRB_API mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format, ...);
......@@ -469,7 +459,7 @@ char* mrb_locale_from_utf8(const char *p, size_t len);
/**
* Creates new mrb_state.
*
* @returns
* @return
* Pointer to the newly created mrb_state.
*/
MRB_API mrb_state* mrb_open(void);
......@@ -482,7 +472,7 @@ MRB_API mrb_state* mrb_open(void);
* @param ud
* User data will be passed to custom allocator f.
* If user data isn't required just pass NULL.
* @returns
* @return
* Pointer to the newly created mrb_state.
*/
MRB_API mrb_state* mrb_open_allocf(mrb_allocf f, void *ud);
......@@ -496,7 +486,7 @@ MRB_API mrb_state* mrb_open_allocf(mrb_allocf f, void *ud);
* @param ud
* User data will be passed to custom allocator f.
* If user data isn't required just pass NULL.
* @returns
* @return
* Pointer to the newly created mrb_state.
*/
MRB_API mrb_state* mrb_open_core(mrb_allocf f, void *ud);
......@@ -676,7 +666,6 @@ MRB_API void mrb_show_copyright(mrb_state *mrb);
MRB_API mrb_value mrb_format(mrb_state *mrb, const char *format, ...);
/** @} */
MRB_END_DECL
#endif /* MRUBY_H */
......@@ -9,11 +9,8 @@
#include "mruby/common.h"
/**
* @file mruby/array.h
* @defgroup mruby_array Array class
* @ingroup mruby
* @{
/*
* Array class
*/
MRB_BEGIN_DECL
......@@ -49,89 +46,74 @@ void mrb_ary_decref(mrb_state*, mrb_shared_array*);
MRB_API void mrb_ary_modify(mrb_state*, struct RArray*);
MRB_API mrb_value mrb_ary_new_capa(mrb_state*, mrb_int);
/**
/*
* Initializes a new array.
*
* Equivalent to:
*
* Array.new
*
* @param mrb
* The MRuby state reference.
* @returns
* The initialized array
* @param mrb The mruby state reference.
* @return The initialized array
*/
MRB_API mrb_value mrb_ary_new(mrb_state *mrb);
MRB_API mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals);
MRB_API mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr);
MRB_API void mrb_ary_concat(mrb_state*, mrb_value, mrb_value);
MRB_API mrb_value mrb_ary_splat(mrb_state*, mrb_value);
/**
/*
* Pushes value into array.
*
* Equivalent to:
*
* ary << value
*
* @param mrb
* The MRuby state reference.
* @param ary
* The array in which the value will be pushed
* @param value
* The value to be pushed into array
* @param mrb The mruby state reference.
* @param ary The array in which the value will be pushed
* @param value The value to be pushed into array
*/
MRB_API void mrb_ary_push(mrb_state *mrb, mrb_value array, mrb_value value);
/**
/*
* Pops the last element from the array.
*
* Equivalent to:
*
* ary.pop
*
* @param mrb
* The MRuby state reference.
* @param ary
* The array from which the value will be poped.
* @returns
* The poped value.
* @param mrb The mruby state reference.
* @param ary The array from which the value will be poped.
* @return The poped value.
*/
MRB_API mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
/**
/*
* Returns a reference to an element of the array on the given index.
*
* Equivalent to:
*
* ary[n]
*
* @param mrb
* The MRuby state reference.
* @param ary
* The target array.
* @param n
* The array index being referenced
* @returns
* The referenced value.
* @param mrb The mruby state reference.
* @param ary The target array.
* @param n The array index being referenced
* @return The referenced value.
*/
MRB_API mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n);
/**
/*
* Sets a value on an array at the given index
*
* Equivalent to:
*
* ary[n] = val
*
* @param mrb
* The MRuby state reference.
* @param ary
* The target array.
* @param n
* The array index being referenced.
* @param val
* The value being setted.
* @param mrb The mruby state reference.
* @param ary The target array.
* @param n The array index being referenced.
* @param val The value being setted.
*/
MRB_API void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
......@@ -152,7 +134,6 @@ mrb_ary_len(mrb_state *mrb, mrb_value ary)
return RARRAY_LEN(ary);
}
/** @} */
MRB_END_DECL
#endif /* MRUBY_ARRAY_H */
......@@ -10,10 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/class.h
* @defgroup mruby_class Class class
* @ingroup mruby
* @{
* Class class
*/
MRB_BEGIN_DECL
......@@ -87,7 +84,6 @@ void mrb_gc_mark_mt(mrb_state*, struct RClass*);
size_t mrb_gc_mark_mt_size(mrb_state*, struct RClass*);
void mrb_gc_free_mt(mrb_state*, struct RClass*);
/** @} */
MRB_END_DECL
#endif /* MRUBY_CLASS_H */
......@@ -7,12 +7,6 @@
#ifndef MRUBY_COMMON_H
#define MRUBY_COMMON_H
/**
* @file mruby/common.h
* @defgroup mruby_common Shared compiler macros
* @ingroup mruby
* @{
*/
#ifdef __cplusplus
# define MRB_BEGIN_DECL extern "C" {
......@@ -24,6 +18,11 @@
# define MRB_END_DECL
#endif
/**
* Shared compiler macros
*/
MRB_BEGIN_DECL
/** Declare a function that never returns. */
#if __STDC_VERSION__ >= 201112L
# define mrb_noreturn _Noreturn
......@@ -63,6 +62,6 @@
# define MRB_API extern
#endif
/** @} */
MRB_END_DECL
#endif /* MRUBY_COMMON_H */
......@@ -10,10 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/compile.h
* @defgroup mruby_compile Compiler
* @ingroup mruby
* @{
* MRuby Compiler
*/
MRB_BEGIN_DECL
......
......@@ -10,13 +10,9 @@
#include "mruby/common.h"
/**
* @file mruby/data.h
* @defgroup mruby_data Custom C wrapped data.
* Custom C wrapped data.
*
* Defining Ruby wrappers around native objects.
*
* @ingroup mruby
* @{
*/
MRB_BEGIN_DECL
......@@ -74,7 +70,6 @@ mrb_data_init(mrb_value v, void *ptr, const mrb_data_type *type)
DATA_TYPE(v) = type;
}
/** @} */
MRB_END_DECL
#endif /* MRUBY_DATA_H */
......@@ -10,10 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/debug.h
* @defgroup mruby_debug Debugging.
* @ingroup mruby
* @{
* MRuby Debugging.
*/
MRB_BEGIN_DECL
......@@ -64,7 +61,6 @@ MRB_API mrb_irep_debug_info_file *mrb_debug_info_append_file(
MRB_API mrb_irep_debug_info *mrb_debug_info_alloc(mrb_state *mrb, mrb_irep *irep);
MRB_API void mrb_debug_info_free(mrb_state *mrb, mrb_irep_debug_info *d);
/** @} */
MRB_END_DECL
#endif /* MRUBY_DEBUG_H */
......@@ -12,10 +12,7 @@
#include "mruby/common.h"
/**
* @file mruby/dump.h
* @defgroup mruby_dump Dumping compiled mruby script.
* @ingroup mruby
* @{
* Dumping compiled mruby script.
*/
MRB_BEGIN_DECL
......@@ -190,7 +187,6 @@ bin_to_uint8(const uint8_t *bin)
return (uint8_t)bin[0];
}
/** @} */
MRB_END_DECL
/** @internal crc.c */
......
......@@ -10,10 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/error.h
* @defgroup mruby_error Error handling.
* @ingroup mruby
* @{
* MRuby error handling.
*/
MRB_BEGIN_DECL
......@@ -45,7 +42,6 @@ MRB_API mrb_value mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_val
mrb_func_t rescue, mrb_value r_data,
mrb_int len, struct RClass **classes);
/** @} */
MRB_END_DECL
#endif /* MRUBY_ERROR_H */
......@@ -10,10 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/gc.h
* @defgroup mruby_gc Uncommon memory management stuffs.
* @ingroup mruby
* @{
* Uncommon memory management stuffs.
*/
MRB_BEGIN_DECL
......@@ -21,7 +18,6 @@ typedef void (mrb_each_object_callback)(mrb_state *mrb, struct RBasic *obj, void
void mrb_objspace_each_objects(mrb_state *mrb, mrb_each_object_callback *callback, void *data);
MRB_API void mrb_free_context(mrb_state *mrb, struct mrb_context *c);
/** @} */
MRB_END_DECL
#endif /* MRUBY_GC_H */
......@@ -10,10 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/hash.h
* @defgroup mruby_hash Hash class
* @ingroup mruby
* @{
* Hash class
*/
MRB_BEGIN_DECL
......@@ -27,15 +24,39 @@ struct RHash {
#define mrb_hash_value(p) mrb_obj_value((void*)(p))
MRB_API mrb_value mrb_hash_new_capa(mrb_state*, int);
/*
* Initializes a new hash.
*/
MRB_API mrb_value mrb_hash_new(mrb_state *mrb);
/*
* Sets a keys and values to hashes.
*/
MRB_API void mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val);
/*
* Gets a value from a key.
*/
MRB_API mrb_value mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key);
MRB_API mrb_value mrb_hash_fetch(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def);
/*
* Deletes hash key and value pair.
*/
MRB_API mrb_value mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key);
/*
* Gets an array of keys.
*/
MRB_API mrb_value mrb_hash_keys(mrb_state *mrb, mrb_value hash);
MRB_API mrb_value mrb_check_hash_type(mrb_state *mrb, mrb_value hash);
MRB_API mrb_value mrb_hash_empty_p(mrb_state *mrb, mrb_value self);
/*
* Clears the hash.
*/
MRB_API mrb_value mrb_hash_clear(mrb_state *mrb, mrb_value hash);
/* RHASH_TBL allocates st_table if not available. */
......@@ -53,7 +74,6 @@ void mrb_gc_mark_hash(mrb_state*, struct RHash*);
size_t mrb_gc_mark_hash_size(mrb_state*, struct RHash*);
void mrb_gc_free_hash(mrb_state*, struct RHash*);
/** @} */
MRB_END_DECL
#endif /* MRUBY_HASH_H */
......@@ -11,10 +11,7 @@
#include "mruby/compile.h"
/**
* @file mruby/irep.h
* @defgroup mruby_irep Compiled mruby scripts.
* @ingroup mruby
* @{
* Compiled mruby scripts.
*/
MRB_BEGIN_DECL
......@@ -58,7 +55,6 @@ void mrb_irep_free(mrb_state*, struct mrb_irep*);
void mrb_irep_incref(mrb_state*, struct mrb_irep*);
void mrb_irep_decref(mrb_state*, struct mrb_irep*);
/** @} */
MRB_END_DECL
#endif /* MRUBY_IREP_H */
......@@ -13,10 +13,7 @@
#include "mruby/common.h"
/**
* @file mruby/khash.h
* @defgroup mruby_khash khash definitions used in mruby's hash table.
* @ingroup mruby
* @{
* khash definitions used in mruby's hash table.
*/
MRB_BEGIN_DECL
......@@ -272,7 +269,6 @@ static inline khint_t __ac_X31_hash_string(const char *s)
typedef const char *kh_cstr_t;
/** @} */
MRB_END_DECL
#endif /* MRUBY_KHASH_H */
......@@ -10,13 +10,9 @@
#include "mruby/common.h"
/**
* @file mruby/numeric.h
* @defgroup mruby_numeric Numeric class and it's sub-classes.
* Numeric class and it's sub-classes.
*
* Numeric, Integer, Float, Fixnum classes
*
* @ingroup mruby
* @{
* Integer, Float and Fixnum
*/
MRB_BEGIN_DECL
......@@ -113,7 +109,6 @@ mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
#undef MRB_UINT_MAKE
#undef MRB_UINT_MAKE2
/** @} */
MRB_END_DECL
#endif /* MRUBY_NUMERIC_H */
......@@ -11,10 +11,7 @@
#include "mruby/irep.h"
/**
* @file mruby/proc.h
* @defgroup mruby_proc Proc class
* @ingroup mruby
* @{
* Proc class
*/
MRB_BEGIN_DECL
......@@ -74,7 +71,6 @@ MRB_API mrb_value mrb_proc_cfunc_env_get(mrb_state*, mrb_int);
#include "mruby/khash.h"
KHASH_DECLARE(mt, mrb_sym, struct RProc*, TRUE)
/** @} */
MRB_END_DECL
#endif /* MRUBY_PROC_H */
......@@ -10,10 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/range.h
* @defgroup mruby_range Range class
* @ingroup mruby
* @{
* Range class
*/
MRB_BEGIN_DECL
......@@ -31,11 +28,21 @@ struct RRange {
#define mrb_range_ptr(v) ((struct RRange*)(mrb_ptr(v)))
#define mrb_range_value(p) mrb_obj_value((void*)(p))
MRB_API mrb_value mrb_range_new(mrb_state*, mrb_value, mrb_value, mrb_bool);
/*
* Initializes a Range.
*
* If the third parameter is FALSE then it includes the last value in the range.
* If the third parameter is TRUE then it excludes the last value in the range.
*
* @param start the beginning value.
* @param end the ending value.
* @param exclude represents the inclusion or exclusion of the last value.
*/
MRB_API mrb_value mrb_range_new(mrb_state *mrb, mrb_value start, mrb_value end, mrb_bool exclude);
MRB_API mrb_bool mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len);
mrb_value mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int));
/** @} */
MRB_END_DECL
#endif /* MRUBY_RANGE_H */
......@@ -10,10 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/string.h
* @defgroup mrb_string String class
* @ingroup mruby
* @{
* String class
*/
MRB_BEGIN_DECL
......@@ -69,6 +66,9 @@ struct RString {
#define RSTR_SET_FROZEN_FLAG(s) ((s)->flags |= MRB_STR_FROZEN)
#define RSTR_UNSET_FROZEN_FLAG(s) ((s)->flags &= ~MRB_STR_FROZEN)
/*
* Returns a pointer from a Ruby string
*/
#define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s)))
#define RSTRING(s) mrb_str_ptr(s)
#define RSTRING_PTR(s) RSTR_PTR(RSTRING(s))
......@@ -88,34 +88,104 @@ mrb_int mrb_str_strlen(mrb_state*, struct RString*);
void mrb_gc_free_str(mrb_state*, struct RString*);
MRB_API void mrb_str_modify(mrb_state*, struct RString*);
MRB_API void mrb_str_concat(mrb_state*, mrb_value, mrb_value);
/*
* Adds two strings together.
*/
MRB_API mrb_value mrb_str_plus(mrb_state*, mrb_value, mrb_value);
/*
* Converts pointer into a Ruby string.
*/
MRB_API mrb_value mrb_ptr_to_str(mrb_state *, void*);
/*
* Returns an object as a Ruby string.
*/
MRB_API mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
/*
* Resizes the string's length.
*/
MRB_API mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len);
/*
* Returns a sub string.
*/
MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len);
/*
* Returns a Ruby string type.
*/
MRB_API mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
MRB_API mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa);
MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
MRB_API const char *mrb_string_value_ptr(mrb_state *mrb, mrb_value ptr);
/*
* Duplicates a string object.
*/
MRB_API mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
/*
* Returns a symbol from a passed in string.
*/
MRB_API mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
MRB_API mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck);
MRB_API double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck);
/*
* Returns a converted string type.
*/
MRB_API mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str);
/*
* Returns true if the strings match and false if the strings don't match.
*/
MRB_API mrb_bool mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
/*
* Returns a concated string comprised of a Ruby string and a C string.
*
* @see mrb_str_cat_cstr
*/
MRB_API 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.
*
* @see mrb_str_cat
*/
MRB_API mrb_value mrb_str_cat_cstr(mrb_state *mrb, mrb_value str, const char *ptr);
MRB_API mrb_value mrb_str_cat_str(mrb_state *mrb, mrb_value str, mrb_value str2);
#define mrb_str_cat_lit(mrb, str, lit) mrb_str_cat(mrb, str, lit, mrb_strlen_lit(lit))
/*
* Adds str2 to the end of str1.
*/
MRB_API mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, 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_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
/*
* Returns a C string from a Ruby string.
*/
MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
mrb_value mrb_str_pool(mrb_state *mrb, mrb_value str);
mrb_int mrb_str_hash(mrb_state *mrb, mrb_value str);
mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
/*
* Returns a printable version of str, surrounded by quote marks, with special characters escaped.
*/
mrb_value mrb_str_inspect(mrb_state *mrb, mrb_value str);
void mrb_noregexp(mrb_state *mrb, mrb_value self);
......@@ -126,7 +196,6 @@ void mrb_regexp_check(mrb_state *mrb, mrb_value obj);
#define mrb_str_buf_cat(mrb, str, ptr, len) mrb_str_cat(mrb, str, ptr, len)
#define mrb_str_buf_append(mrb, str, str2) mrb_str_cat_str(mrb, str, str2)
/** @} */
MRB_END_DECL
#endif /* MRUBY_STRING_H */
......@@ -10,13 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/value.h
* @defgroup mruby_value Value definitions
*
* @ref mrb_value functions and macros.
*
* @ingroup mruby
* @{
* MRuby Value definition functions and macros.
*/
MRB_BEGIN_DECL
......@@ -134,8 +128,10 @@ enum mrb_vtype {
#define mrb_test(o) mrb_bool(o)
MRB_API mrb_bool mrb_regexp_p(struct mrb_state*, mrb_value);
static inline mrb_value
mrb_float_value(struct mrb_state *mrb, mrb_float f)
/*
* Returns a float in Ruby.
*/
MRB_INLINE mrb_value mrb_float_value(struct mrb_state *mrb, mrb_float f)
{
mrb_value v;
(void) mrb;
......@@ -152,8 +148,10 @@ mrb_cptr_value(struct mrb_state *mrb, void *p)
return v;
}
static inline mrb_value
mrb_fixnum_value(mrb_int i)
/*
* Returns a fixnum in Ruby.
*/
MRB_INLINE mrb_value mrb_fixnum_value(mrb_int i)
{
mrb_value v;
SET_INT_VALUE(v, i);
......@@ -177,7 +175,7 @@ mrb_obj_value(void *p)
}
/**
/*
* Get a nil mrb_value object.
*
* @return
......@@ -190,16 +188,20 @@ MRB_INLINE mrb_value mrb_nil_value(void)
return v;
}
static inline mrb_value
mrb_false_value(void)
/*
* Returns false in Ruby.
*/
MRB_INLINE mrb_value mrb_false_value(void)
{
mrb_value v;
SET_FALSE_VALUE(v);
return v;
}
static inline mrb_value
mrb_true_value(void)
/*
* Returns true in Ruby.
*/
MRB_INLINE mrb_value mrb_true_value(void)
{
mrb_value v;
SET_TRUE_VALUE(v);
......@@ -245,7 +247,6 @@ mrb_ro_data_p(const char *p)
# define mrb_ro_data_p(p) FALSE
#endif
/** @} */
MRB_END_DECL
#endif /* MRUBY_VALUE_H */
......@@ -10,10 +10,7 @@
#include "mruby/common.h"
/**
* @file mruby/variable.h
* @defgroup mruby_variable Functions to access to mruby variables.
* @ingroup mruby
* @{
* Functions to access mruby variables.
*/
MRB_BEGIN_DECL
......@@ -80,7 +77,6 @@ void mrb_gc_mark_iv(mrb_state*, struct RObject*);
size_t mrb_gc_mark_iv_size(mrb_state*, struct RObject*);
void mrb_gc_free_iv(mrb_state*, struct RObject*);
/** @} */
MRB_END_DECL
#endif /* MRUBY_VARIABLE_H */
......@@ -10,47 +10,101 @@
#include "mruby/common.h"
/**
* @file mruby/version.h
* @brief MRuby version macros
* @defgroup mrb_string MRuby version macros
* @ingroup MRuby
* @{
* mruby version definition macros
*/
MRB_BEGIN_DECL
/*
* A passed in expression.
*/
#define MRB_STRINGIZE0(expr) #expr
/*
* Passes in an expression to MRB_STRINGIZE0.
*/
#define MRB_STRINGIZE(expr) MRB_STRINGIZE0(expr)
/*
* The version of Ruby used by mruby.
*/
#define MRUBY_RUBY_VERSION "1.9"
/*
* Ruby engine.
*/
#define MRUBY_RUBY_ENGINE "mruby"
/*
* Major release version number.
*/
#define MRUBY_RELEASE_MAJOR 1
/*
* Minor release version number.
*/
#define MRUBY_RELEASE_MINOR 1
/*
* Tiny release version number.
*/
#define MRUBY_RELEASE_TEENY 1
/*
* The mruby version.
*/
#define MRUBY_VERSION MRB_STRINGIZE(MRUBY_RELEASE_MAJOR) "." MRB_STRINGIZE(MRUBY_RELEASE_MINOR) "." MRB_STRINGIZE(MRUBY_RELEASE_TEENY)
/*
* Release number.
*/
#define MRUBY_RELEASE_NO (MRUBY_RELEASE_MAJOR * 100 * 100 + MRUBY_RELEASE_MINOR * 100 + MRUBY_RELEASE_TEENY)
/*
* Release year.
*/
#define MRUBY_RELEASE_YEAR 2014
/*
* Release month.
*/
#define MRUBY_RELEASE_MONTH 11
/*
* Release day.
*/
#define MRUBY_RELEASE_DAY 19
/*
* Release date as a string.
*/
#define MRUBY_RELEASE_DATE MRB_STRINGIZE(MRUBY_RELEASE_YEAR) "-" MRB_STRINGIZE(MRUBY_RELEASE_MONTH) "-" MRB_STRINGIZE(MRUBY_RELEASE_DAY)
/*
* The year mruby was first created.
*/
#define MRUBY_BIRTH_YEAR 2010
/*
* MRuby's authors.
*/
#define MRUBY_AUTHOR "mruby developers"
/*
* mruby's version, and release date.
*/
#define MRUBY_DESCRIPTION \
"mruby " MRUBY_VERSION \
" (" MRUBY_RELEASE_DATE ") " \
/*
* mruby's copyright information.
*/
#define MRUBY_COPYRIGHT \
"mruby - Copyright (c) " \
MRB_STRINGIZE(MRUBY_BIRTH_YEAR)"-" \
MRB_STRINGIZE(MRUBY_RELEASE_YEAR)" " \
MRUBY_AUTHOR \
/** @} */
MRB_END_DECL
#endif /* MRUBY_VERSION_H */
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