Skip to content

Instantly share code, notes, and snippets.

@hiroeorz
Created May 19, 2014 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroeorz/9a18b89b15c9c336fe5d to your computer and use it in GitHub Desktop.
Save hiroeorz/9a18b89b15c9c336fe5d to your computer and use it in GitHub Desktop.
ツナでもわかるmruby[7回目:mrbgemsの作り方メモ(勉強中)] ref: http://qiita.com/hiroeorz@github/items/d6fa2978d9f53a41c777
conf.gem '../mruby-mqtt'
format specifiers:
string mruby type C type note
----------------------------------------------------------------------
o: Object [mrb_value]
C: class/module [mrb_value]
S: String [mrb_value]
A: Array [mrb_value]
H: Hash [mrb_value]
s: String [char*,mrb_int] Receive two arguments.
z: String [char*] NUL terminated string.
a: Array [mrb_value*,mrb_int] Receive two arguments.
f: Float [mrb_float]
i: Integer [mrb_int]
b: Boolean [mrb_bool]
n: Symbol [mrb_sym]
d: Data [void*,mrb_data_type const] 2nd argument will
be used to check
data type so
it won't be
modified
&: Block [mrb_value]
*: rest argument [mrb_value*,mrb_int] Receive the rest of
the arguments as
an array.
|: optional Next argument of '|' and
later are optional.
//
?: optional given [mrb_bool] true if preceding
argument (optional) is
given.
static mrb_value
fiber_yield(mrb_state *mrb, mrb_value self)
{
mrb_value *a; // 引数の配列へのポインタが入る変数
mrb_int len; // 引数の数が入る変数
mrb_get_args(mrb, "*", &a, &len);
return mrb_fiber_yield(mrb, len, a);
}
mruby-クラス名/ mrbgem.rake
src / クラス名.c
[その他のCソースファイル]
mrblib / クラス名.rb
[その他のmrubyソースファイル]
test / [テストファイル]
mrb_define_method(mrb, klass, "インスタンスメソッド名", 関連付けるC関数, 引数タイプ)
mrb_define_class_method(mrb, klass, "クラスメソッド名", 関連付けるC関数, 引数タイプ);
void
mrb_mruby_クラス名_gem_final(mrb_state* mrb)
{
}
mrb_value
mrb_str_new_cstr(mrb_state *mrb, const char *p)
MRuby::Gem::Specification.new('mruby-time') do |spec|
spec.license = 'MIT'
spec.author = 'mruby developers'
spec.summary = 'standard Time class'
end
struct RClass *klass;
struct RClass *string_klass = mrb->string_class;
mrb_define_class(mrb, "クラス名", 継承元クラス);
mrb_define_class_method(mrb, gc, "start", gc_start, MRB_ARGS_NONE());
static mrb_value
mrb_hash_set_default(mrb_state *mrb, mrb_value hash)
{
mrb_value ifnone;
mrb_get_args(mrb, "o", &ifnone);
mrb_hash_modify(mrb, hash);
mrb_iv_set(mrb, hash, mrb_intern_lit(mrb, "ifnone"), ifnone);
RHASH(hash)->flags &= ~(MRB_HASH_PROC_DEFAULT);
return ifnone;
}
mrb_funcall(mrb, obj, "メソッド名", 引数の数, 引数1, 引数2 ...)
mrb_raise(mrb, エラークラス, "エラーメッセージ");
void
mrb_mruby_string_utf8_gem_final(mrb_state* mrb)
{
}
static mrb_value
mrb_time_eq(mrb_state *mrb, mrb_value self)
{
mrb_value other;
struct mrb_time *tm1, *tm2;
mrb_bool eq_p;
mrb_get_args(mrb, "o", &other);
tm1 = DATA_CHECK_GET_PTR(mrb, self, &mrb_time_type, struct mrb_time);
tm2 = DATA_CHECK_GET_PTR(mrb, other, &mrb_time_type, struct mrb_time);
eq_p = tm1 && tm2 && tm1->sec == tm2->sec && tm1->usec == tm2->usec;
return mrb_bool_value(eq_p);
}
/* macros to get typical exception objects
note:
+ those E_* macros requires mrb_state* variable named mrb.
+ exception objects obtained from those macros are local to mrb
*/
#define E_RUNTIME_ERROR (mrb_class_get(mrb, "RuntimeError"))
#define E_TYPE_ERROR (mrb_class_get(mrb, "TypeError"))
#define E_ARGUMENT_ERROR (mrb_class_get(mrb, "ArgumentError"))
#define E_INDEX_ERROR (mrb_class_get(mrb, "IndexError"))
#define E_RANGE_ERROR (mrb_class_get(mrb, "RangeError"))
#define E_NAME_ERROR (mrb_class_get(mrb, "NameError"))
#define E_NOMETHOD_ERROR (mrb_class_get(mrb, "NoMethodError"))
#define E_SCRIPT_ERROR (mrb_class_get(mrb, "ScriptError"))
#define E_SYNTAX_ERROR (mrb_class_get(mrb, "SyntaxError"))
#define E_LOCALJUMP_ERROR (mrb_class_get(mrb, "LocalJumpError"))
#define E_REGEXP_ERROR (mrb_class_get(mrb, "RegexpError"))
#define E_NOTIMP_ERROR (mrb_class_get(mrb, "NotImplementedError"))
#define E_FLOATDOMAIN_ERROR (mrb_class_get(mrb, "FloatDomainError"))
#define E_KEY_ERROR (mrb_class_get(mrb, "KeyError"))
static const struct types {
unsigned char type;
const char *name;
} builtin_types[] = {
{MRB_TT_FALSE, "false"},
{MRB_TT_TRUE, "true"},
{MRB_TT_FIXNUM, "Fixnum"},
{MRB_TT_SYMBOL, "Symbol"}, /* :symbol */
{MRB_TT_MODULE, "Module"},
{MRB_TT_OBJECT, "Object"},
{MRB_TT_CLASS, "Class"},
{MRB_TT_ICLASS, "iClass"}, /* internal use: mixed-in module holder */
{MRB_TT_SCLASS, "SClass"},
{MRB_TT_PROC, "Proc"},
{MRB_TT_FLOAT, "Float"},
{MRB_TT_ARRAY, "Array"},
{MRB_TT_HASH, "Hash"},
{MRB_TT_STRING, "String"},
{MRB_TT_RANGE, "Range"},
{MRB_TT_FILE, "File"},
{MRB_TT_DATA, "Data"}, /* internal use: wrapped C pointers */
{-1, 0}
};
void
mrb_mruby_クラス名_gem_init(mrb_state* mrb) {
/*...*/
}
mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 1)", mrb_fixnum_value(argc));
mrb_value
mrb_iv_get(mrb_state *mrb, mrb_value obj, mrb_sym sym)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment