Skip to content

Instantly share code, notes, and snippets.

@cubicdaiya cubicdaiya/hoge.c
Last active Dec 31, 2015

Embed
What would you like to do?
mruby example
#include <stdio.h>
#include <mruby.h>
#include <mruby/proc.h>
#include <mruby/data.h>
#include <mruby/compile.h>
#include <mruby/string.h>
#include <mruby/hash.h>
#include <mruby/variable.h>
static struct RProc *compile(mrb_state *mrb, const char *path)
{
FILE *mrb_file;
struct mrb_parser_state *p;
struct RProc* proc;
if ((mrb_file = fopen(path, "r")) == NULL) {
return NULL;;
}
p = mrb_parse_file(mrb, mrb_file, NULL);
fclose(mrb_file);
proc = mrb_generate_code(mrb, p);
if (proc == NULL) {
mrb_pool_close(p->pool);
return NULL;
}
mrb_pool_close(p->pool);
return proc;
}
static mrb_value hello(mrb_state *mrb, mrb_value self)
{
printf("Hello, mruby!\n");
return self;
}
static mrb_value add(mrb_state *mrb, mrb_value self)
{
mrb_int a, b;
mrb_get_args(mrb, "ii", &a, &b);
return mrb_fixnum_value(a + b);
}
static mrb_value table_get(mrb_state *mrb, mrb_value self)
{
mrb_value key;
mrb_value table;
table = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "table"));
if (mrb_nil_p(table)) {
table = mrb_hash_new(mrb);
mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "table"), table);
}
mrb_get_args(mrb, "o", &key);
return mrb_hash_get(mrb, table, key);
}
static mrb_value table_set(mrb_state *mrb, mrb_value self)
{
mrb_value key, val;
mrb_value table;
mrb_get_args(mrb, "oo", &key, &val);
table = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "table"));
if (mrb_nil_p(table)) {
table = mrb_hash_new(mrb);
}
mrb_hash_set(mrb, table, key, val);
mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "table"), table);
return self;
}
int main(int argc, char *argv[])
{
mrb_state *mrb;
struct RProc *proc;
struct RClass *hoge_module, *table_class;
const char *path = "hoge.mrb";
struct RString *err_str;
mrb = mrb_open();
// モジュールを定義する
hoge_module = mrb_define_module(mrb, "Hoge");
// クラスを定義する
//table_class = mrb_define_class_under(mrb, hoge_module, "Table", mrb->object_class);
table_class = mrb_define_class_under(mrb, mrb->kernel_module, "Table", mrb->object_class);
// 定数を定義する
mrb_define_const(mrb, hoge_module, "TRUE", mrb_fixnum_value(1));
mrb_define_const(mrb, hoge_module, "FALSE", mrb_fixnum_value(0));
// 関数を定義する
mrb_define_class_method(mrb, hoge_module, "hello", hello, MRB_ARGS_NONE());
mrb_define_class_method(mrb, hoge_module, "add", add, MRB_ARGS_ANY());
// ハッシュテーブル
mrb_define_class_method(mrb, table_class, "get", table_get, MRB_ARGS_ANY());
mrb_define_class_method(mrb, table_class, "set", table_set, MRB_ARGS_ANY());
// mrubyスクリプトをコンパイル
if((proc = compile(mrb, path)) == NULL) {
mrb_close(mrb);
printf("failed to compile:%s\n", path);
return 1;
}
// mrubyスクリプトを実行
mrb_run(mrb, proc, mrb_top_self(mrb));
if (mrb->exc) {
err_str = mrb_str_ptr(mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0));
printf("mrb_run failed:%s\n", err_str->ptr);
}
mrb_close(mrb);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.