16
13

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 5 years have passed since last update.

[mruby] mrb_value を C言語のデータ型に変換する方法

Last updated at Posted at 2015-01-21

共通コード

# include <stdio.h>
# include <mruby.h>
# include <mruby/compile.h>
# include <mruby/string.h>
# include <mruby/array.h>
# include <mruby/hash.h>

int main( void ){
    mrb_state* mrb = mrb_open();
    if ( NULL != mrb ){
        mrbc_context* cxt = mrbc_context_new( mrb );
        mrb_value value;
        
        mrb_load_string_cxt( mrb, "def debug( var ); p var; end", cxt );
        
        // TODO
        
        mrbc_context_free( mrb, cxt );
        cxt = NULL;
        mrb_close( mrb );
        mrb = NULL;
    }
    return 0;
}

mruby -> C言語

mrb_value -> bool

value = mrb_load_string_cxt( mrb, "false", cxt );
printf( "value=%d\n", mrb_bool( value ) ); // value=0

value = mrb_load_string_cxt( mrb, "true", cxt );
printf( "value=%d\n", mrb_bool( value ) ); // value=1

mrb_value -> int

value = mrb_load_string_cxt( mrb, "111", cxt );
if ( mrb_fixnum_p( value ) ){
    printf( "value=%d\n", mrb_fixnum( value ) ); // value=111
}

mrb_value -> float

value = mrb_load_string_cxt( mrb, "1.5", cxt );
if ( mrb_float_p( value ) ){
    printf( "value=%f\n", mrb_float( value ) ); // value=1.500000
}

mrb_value -> string

value = mrb_load_string_cxt( mrb, "string", cxt );
if ( mrb_string_p( value ) ){
    printf( "value=%s\n", RSTRING_PTR( value ) ); // value=string
}

mrb_value -> array

value = mrb_load_string_cxt( mrb, "[1,2]", cxt );
if ( mrb_array_p( value ) ){
    int len = RARRAY_LEN( value );
    printf( "array_len=%d\n", len ); // array_len=2
    for ( int i = 0; i < len; ++i ){
        printf( "array[%d]=%d\n", i, mrb_fixnum( mrb_ary_ref( mrb, value, i ) ) );
    }
    // array[0]=1
    // array[1]=2
}

mrb_value -> hash

value = mrb_load_string_cxt( mrb, "{ 'a' => 'A', 'b' => 'B', 'c' => 'C' }", cxt );
if ( mrb_hash_p( value ) ){
    mrb_value keys = mrb_hash_keys( mrb, value );
    int len = RARRAY_LEN( keys );
    printf( "hash_keys_len=%d\n", len ); // hash_keys_len=3
    for ( int i = 0; i < len; ++i ){
        mrb_value key = mrb_ary_ref( mrb, keys, i );
        printf( "hash[%s]=%s\n", RSTRING_PTR( key ), RSTRING_PTR( mrb_hash_get( mrb, value, key ) ) );
    }
    // hash[a]=A
    // hash[b]=B
    // hash[c]=C
}

C言語 -> mruby

bool -> mrb_value

mrb_funcall( mrb, mrb_top_self( mrb ), "debug", 1, mrb_false_value() ); // false
mrb_funcall( mrb, mrb_top_self( mrb ), "debug", 1, mrb_bool_value( false ) ); // false

mrb_funcall( mrb, mrb_top_self( mrb ), "debug", 1, mrb_true_value() ); // true
mrb_funcall( mrb, mrb_top_self( mrb ), "debug", 1, mrb_bool_value( true ) ); // true

int -> mrb_value

mrb_funcall( mrb, mrb_top_self( mrb ), "debug", 1, mrb_fixnum_value( 111 ) ); // 111

float -> mrb_value

mrb_funcall( mrb, mrb_top_self( mrb ), "debug", 1, mrb_float_value( mrb, 1.5 ) ); // 1.5

string -> mrb_value

mrb_funcall( mrb, mrb_top_self( mrb ), "debug", 1, mrb_str_new_cstr( mrb, "string" ) ); // "string"

参考サイト

mrb_valueについて調べてみた
c から mrubyのメソッドをコールする

16
13
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
16
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?