Skip to content

Instantly share code, notes, and snippets.

@matsumotory
Forked from tsuzakiyo/gist:2872532
Created June 5, 2012 07:39
Show Gist options
  • Save matsumotory/2873356 to your computer and use it in GitHub Desktop.
Save matsumotory/2873356 to your computer and use it in GitHub Desktop.
mrb_fixnum_value_sample.c
#include <mruby.h>
#include "mruby/string.h"
#include "mruby/proc.h"
#include "mruby/compile.h"
struct RClass *class;
struct RClass *class_int;
mrb_value mrb_num_init(mrb_state *mrb, mrb_value str);
mrb_value ap_mrb_get(mrb_state *mrb, mrb_value str);
mrb_value mrb_print(mrb_state *mrb, mrb_value str);
mrb_value mrb_num_init(mrb_state *mrb, mrb_value str)
{
str = mrb_class_new_instance(mrb, 0, NULL, class);
return str;
}
mrb_value mrb_num_get(mrb_state *mrb, mrb_value str)
{
int val = 100;
return mrb_fixnum_value(val);
}
mrb_value mrb_print(mrb_state *mrb, mrb_value str)
{
mrb_value msg;
mrb_get_args(mrb, "o", &msg);
printf("%d\n", RSTRING_PTR(msg));
return str;
}
int
main() {
int n;
mrb_state* mrb;
struct mrb_parser_state* p;
mrb = mrb_open();
class = mrb_define_module(mrb, "Variable");
mrb_define_class_method(mrb, class, "print", mrb_print, ARGS_ANY());
class_int = mrb_define_class_under(mrb, class, "Integer", mrb->object_class);
mrb_define_method(mrb, class, "Initialize", mrb_num_init, ARGS_NONE());
mrb_define_method(mrb, class, "int", mrb_num_get, ARGS_NONE());
{
char* code =
"require 'Variable' \n"
"t = Variable::Integer.new() \n"
"Variable.print(\"int= \" + r.int) \n";
p = mrb_parse_string(mrb, code);
n = mrb_generate_code(mrb, p->tree);
mrb_pool_close(p->pool);
mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment