Esempio n. 1
0
int main ( int argc, char const *argv[] )
{
  mrb_state *mrb = mrb_open();
  
  // Load Ruby code defined in C (SimpleC)
  init_simple_c( mrb );
  
  // Load Ruby code compiled into C (SimpleRuby)
  mrb_load_irep( mrb, &simple_ruby );
  
  // Print with C
  printf( "Hello from C!\n" );
  
  // Print by evaluating Ruby from C
  mrb_load_string( mrb, "puts 'Hello from MRuby!'" );
  
  // Print by evaluating Ruby from C, using Class defined in compiled Ruby
  mrb_load_string( mrb, "SimpleRuby.say_hi" );
  
  // Print by evaluating Ruby from C, using Class defined in C
  mrb_load_string( mrb, "SimpleC.say_hi" );
  
  // Print by calling function directly from Class defined in compiled Ruby
  struct RClass *simple_ruby_class = mrb_class_get(mrb, "SimpleRuby");
  mrb_funcall(mrb, mrb_obj_value(simple_ruby_class), "say_hi", 0 );
  
  // Print by calling function directly from Class defined in C
  struct RClass *simple_c_class = mrb_class_get(mrb, "SimpleC");
  mrb_funcall(mrb, mrb_obj_value(simple_c_class), "say_hi", 0 );
  
  mrb_close( mrb ); // Not sure if this is really needed as we are exiting anyways but I'm afraid not to.
  
  return 0;
}
Esempio n. 2
0
int main(void)  
{
  mrb_state *mrb = mrb_open();
  char code[] = "5.times { puts 'Ruby is awesome!' }";
  mrb_load_string(mrb, code);
 return 0;
}
Esempio n. 3
0
mrb_value testBinding(mrb_state * mrb)
{
   // Export a C++ class
   auto rubyclass = mruby::bindclass<ACppClass>::it(mrb, "ACppClass");
   mruby::bind<ACppClass, decltype(&ACppClass::getNumber), int>::method(mrb, rubyclass, "get", &ACppClass::getNumber);
   auto ret1 = mrb_load_string(mrb, "app = ACppClass.new\n p app.get()");
   return ret1;
}
Esempio n. 4
0
int main()
{
  mrb_state *mrb;
  int n;

  mrb = mrb_open();
  n = mrb_load_string(mrb, "puts 'hello'; puts 1+2; p({:a=>[1,2,3], :b=>{'c'=>{'d'=>['e', {'f'=>nil}]}}})");
  mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value());
  mrb_close(mrb);
}
Esempio n. 5
0
int main(void)
{
  /* make a mruby instance */
  mrb_state *mrb = mrb_open();

  /* write some code */
  char code[] = "p 'Hello world!'";

  /* use it to execute code from string */
  mrb_load_string(mrb, code);
  return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
	mrb_state *mrb = mrb_open();
	char code[] = "p 'hello world!'";
	printf("Executing Ruby code from C!\n");

	mrb_load_string(mrb, code);

	getchar();

	return 0;
}
Esempio n. 7
0
static mrb_value  mrb_code_exec(mrb_state *mrb, const char *code)
{
    mrb_value ret;
    if(!mrb)
    {
        return mrb_nil_value();
    }
    ret =mrb_load_string(mrb,code);
    mrb_close(mrb);

    return ret;

}
int main(void)
{
  mrb_state *mrb = mrb_open();
  
  struct RClass* cv = mrb_class_get(mrb, "String");
  mrb_value code = mrb_str_new(mrb, "p 'hello world!'", 16);
  mrb_value str = mrb_obj_new(mrb, cv, 1, &code);

  printf("str#size is %d \n", mrb_funcall(mrb, str, "size", 0, 0));

  printf("str is '%s' \n", RSTRING_PTR(str));

  mrb_load_string(mrb, RSTRING_PTR(str));
  return 0;
}
Esempio n. 9
0
int main() {
  mrb_state* mrb = mrb_open();
  mrb_define_method(mrb, mrb->kernel_module, "plus", plus, MRB_ARGS_REQ(2));
  mrb_define_method(mrb, mrb->kernel_module, "myabs", myabs, MRB_ARGS_REQ(1));

  mrb_load_string(mrb,
                  "puts plus(1, 2);"
                  "puts plus('foo', 'bar');"
                  "puts myabs(-11);"
                  "puts myabs(332311);"
                  );
  if (mrb->exc) { // エラー処理
    mrb_p(mrb, mrb_obj_value(mrb->exc));
  }
  mrb_close(mrb);
  return 0;
}
Esempio n. 10
0
static int
eval_test(mrb_state *mrb)
{
  mrb_value return_value;
  const char *prog = "report()";

  /* evaluate the test */
  return_value = mrb_load_string(mrb, prog);
  /* did an exception occur? */
  if (mrb->exc) {
    mrb_p(mrb, return_value);
    mrb->exc = 0;
    return EXIT_FAILURE;
  }
  else if (!check_error(mrb)) {
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}
int main(void)
{
  mrb_state *mrb = mrb_open();
  
  struct RClass* cv = mrb_class_get(mrb, "String");
  mrb_value str = mrb_obj_new(mrb, cv, 0, 0);
  
  mrb_value head = mrb_str_new(mrb,"p 'hello",8);
  mrb_value tail = mrb_str_new(mrb," world'",7);

  str = mrb_funcall_argv(mrb, str, mrb_intern2(mrb, "+", 1), 1, &head);
  str = mrb_funcall_argv(mrb, str, mrb_intern2(mrb, "+", 1), 1, &tail);

  printf("str#size is %d \n", mrb_funcall(mrb, str, "size", 0, 0));
  printf("str is '%s' \n", RSTRING_PTR(str));

  mrb_load_string(mrb, RSTRING_PTR(str));
  return 0;
}
Esempio n. 12
0
int
main(int argc, char *argv[])
{
  mrb_state *mrb;
  int i;
  mrb_value ARGV;

  mrb = mrb_open();
  ARGV = mrb_ary_new_capa(mrb, argc - 1);
  for (i = 1; i < argc; i++) {
    mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, argv[i]));
  }
  mrb_define_global_const(mrb, "ARGV", ARGV);

  mrb_load_string(mrb, "Fastyet.start");

  if(mrb->exc) {
    mrb_p(mrb, mrb_obj_value(mrb->exc));
  }

  mrb_close(mrb);

  return EXIT_SUCCESS;
}
 mrb_value ScriptEngine::runString(std::string line) {
     return mrb_load_string(mrb.get(), line.c_str());
 }