0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

mrbgemsのメソッドの引数

Last updated at Posted at 2023-10-24

mrubyの拡張のmebgemsをCで書いた場合のメソッドの引数は以下のようなパターンがあります。

無し

mrb_define_method(mrb, bsdspi, "getclk", mrb_bsdspi_getclk, MRB_ARGS_NONE());

固定引数

mrb_define_method(mrb, bsdspi, "setclk", mrb_bsdspi_setclk, MRB_ARGS_REQ(1));

関数では以下のようにして引数を拾います。

mrb_get_args(mrb, "i", &val);

複数の型が引数になる場合は、型をチェックして処理します。以下はintかarrayが引数になる処理です。

  mrb_get_args(mrb, "o", &arg);
  if (mrb_type(arg) == MRB_TT_INTEGER) {
    arr = mrb_ary_new(mrb);
    mrb_ary_push(mrb, arr, mrb_fixnum_value(mrb_integer(arg)));
  } else {
    arr = arg;
  }

可変引数

必須な引数と、オプションの引数が有る場合

mrb_define_method(mrb, bsdiic, "read", mrb_bsdiic_read, MRB_ARGS_ARG(2, 1));

これは以下のように拾います。

mrb_get_args(mrb, "ii|i", &addr, &len, &opt);

引数の数はmrb_get_argc(mrb)で確認できます。

オプションの引数のみの場合はMRB_ARGS_OPT(n)で指定できるようです。

完全可変引数

MRB_ARGS_ANY()で指定して、配列として受け取れます。

キーワード引数

MRB_ARGS_KEY()で指定して使えるようですが、未確認です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?