Exemple #1
0
SEXP dotRb(SEXP args)
{
  SEXP ans;
  char *cmd;
  mrb_value val; //non utilisé pour l'instant!!!

  if(!isValidString(CADR(args)))
    error("invalid argument");
    cmd = (char*)CHAR(STRING_ELT(CADR(args), 0));
//printf("instruction à executer %s\n",cmd);
  val=mrb_load_string_cxt(mrb,cmd,mrb_cxt);
  //printf("state\n");
  if(mrb->exc) {
    if (!mrb_undef_p(val)) {
      mrb_print_error(mrb);
    }
    //rb_p(state);
    printf("MRuby error !!!\n");
    printf("in executing : %s\n",cmd);
    return R_NilValue;
  } else {
    //ans=(SEXP)newRbObj(val);
    ans=mrbArray2RVector(val);
    return ans;
  }
}
Exemple #2
0
                       s.run                                  \n\
";

int main(void)
{
  mrb_state *mrb = mrb_open();
  mrbc_context *ctx = mrbc_context_new(mrb);

  // 8080 into instance variable "@port" of main on same ctx
  mrb_iv_set(mrb, mrb_top_self(mrb), mrb_intern_lit(mrb, "@port"),
      mrb_fixnum_value(8080));
  mrb_iv_set(mrb, mrb_top_self(mrb), mrb_intern_lit(mrb, "@echo_content"),
      mrb_str_new_lit(mrb, "world from cb."));
  mrb_load_string_cxt(mrb, config, ctx);
  mrb_load_string_cxt(mrb, response, ctx);
  mrb_load_string_cxt(mrb, run, ctx);

  mrbc_context_free(mrb, ctx);
  mrb_close(mrb);
  return 0;
}
int main(void)
{
  mrb_value response;
  mrb_state *mrb = mrb_open();
  mrbc_context *ctx = mrbc_context_new(mrb);

  // URL into instance variable "@url" of main on same ctx
  mrb_iv_set(mrb, mrb_top_self(mrb), mrb_intern_lit(mrb, "@url"),
      mrb_str_new_lit(mrb, "https://127.0.0.1:8080/index.html"));
  response = mrb_load_string_cxt(mrb, request, ctx);
  mrb_funcall(mrb, mrb_top_self(mrb), "pp", 1, response);

  mrbc_context_free(mrb, ctx);
  mrb_close(mrb);
  return 0;
}
Exemple #4
0
int webruby_internal_run_source(mrb_state* mrb, const char *s, int print_level)
{
    mrbc_context *c = NULL;
    int err;

    if (print_level > 0) {
        c = mrbc_context_new(mrb);
        c->dump_result = TRUE;
    }
    err = check_and_print_errors(mrb, mrb_load_string_cxt(mrb, s, c),
                                 print_level);
    if (c) {
        mrbc_context_free(mrb, c);
    }
    return err;
}
Exemple #5
0
void ss_init( ss_info* info, int argc, const char** argv ){
  info->exit_status = 0;
  info->is_verbose = false;
  info->script.type = 0;
  info->script.value = null;
  
  for ( int i = 1; i < argc; ++i ){
    const char* arg = argv[ i ];
    
    if ( ss_string_match( "-e", arg ) ){
      info->script.type  = -1;
      info->script.value = &argv[ i + 1 ];
    }else if ( ss_string_match( "-v", arg ) ){
      info->is_verbose = true;
    }else if ( 0 == info->script.type ){
      info->script.type  = argc - i;
      info->script.value = &argv[ i ];
      break;
    }
  }
  
  ss_mruby_info* mruby = &(info->state.mruby);
  mruby->state = mrb_open();
  mruby->context = null;
  do{
    if ( null == mruby->state ){
      SS_PUTS_ERR( "mrb_open error" );
      info->exit_status = 1;
      break;
    }
    
    mruby->context = mrbc_context_new( mruby->state );
    if ( null == mruby->context ){
      SS_PUTS_ERR( "mrbc_context_new error" );
      info->exit_status = 1;
      break;
    }
    
    mrb_load_string_cxt( mruby->state, "class Node;  @@values = [ nil ];  ;  def self.add( value );    id = @@values.size;    @@values.push value;    id;  end;  ;  def self.get( id );    @@values[ id ];  end;end;;def node_get( id );  Node.get( id );end;;def node_add_string( string, length );  Node.add( string[ 0, length ] );end;;def node_add( value );  Node.add( value );end;", mruby->context );
  }while ( false );
}
Exemple #6
0
int
main(int argc, char **argv)
{
  mrb_state *mrb = mrb_open();
  int n = -1;
  int i;
  struct _args args;
  mrb_value ARGV;

  if (mrb == NULL) {
    fprintf(stderr, "Invalid mrb_state, exiting mruby");
    return EXIT_FAILURE;
  }

  n = parse_args(mrb, argc, argv, &args);
  if (n < 0 || (args.cmdline == NULL && args.rfp == NULL)) {
    cleanup(mrb, &args);
    usage(argv[0]);
    return n;
  }

  ARGV = mrb_ary_new_capa(mrb, args.argc);
  for (i = 0; i < args.argc; i++) {
    mrb_ary_push(mrb, ARGV, mrb_str_new(mrb, args.argv[i], strlen(args.argv[i])));
  }
  mrb_define_global_const(mrb, "ARGV", ARGV);

  if (args.mrbfile) {
    n = mrb_load_irep(mrb, args.rfp);
    if (n >= 0) {
      if (!args.check_syntax) {
	mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
	if (mrb->exc) {
	  p(mrb, mrb_obj_value(mrb->exc));
	}
      }
    }
  }
  else {
    mrbc_context *c = mrbc_context_new(mrb);
    mrb_value v;

    if (args.verbose)
      c->dump_result = 1;
    if (args.check_syntax)
      c->no_exec = 1;

    if (args.rfp) {
      mrbc_filename(mrb, c, args.cmdline ? args.cmdline : "-");
      v = mrb_load_file_cxt(mrb, args.rfp, c);
    }
    else {
      mrbc_filename(mrb, c, "-e");
      v = mrb_load_string_cxt(mrb, args.cmdline, c);
    }
    mrbc_context_free(mrb, c);
    if (mrb->exc) {
      if (!mrb_undef_p(v)) {
	p(mrb, mrb_obj_value(mrb->exc));
      }
      n = -1;
    }
    else if (args.check_syntax) {
      printf("Syntax OK\n");
    }
  }
  cleanup(mrb, &args);

  return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Exemple #7
0
int
main(int argc, char **argv)
{
  mrb_state *mrb = mrb_open();
  int n = -1;
  int i;
  struct _args args;
  mrb_value ARGV;
  mrbc_context *c;
  mrb_value v;

  if (mrb == NULL) {
    fputs("Invalid mrb_state, exiting mruby\n", stderr);
    return EXIT_FAILURE;
  }

  n = parse_args(mrb, argc, argv, &args);
  if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) {
    cleanup(mrb, &args);
    usage(argv[0]);
    return n;
  }

  ARGV = mrb_ary_new_capa(mrb, args.argc);
  for (i = 0; i < args.argc; i++) {
    mrb_ary_push(mrb, ARGV, mrb_str_new(mrb, args.argv[i], strlen(args.argv[i])));
  }
  mrb_define_global_const(mrb, "ARGV", ARGV);

  c = mrbc_context_new(mrb);
  if (args.verbose)
    c->dump_result = TRUE;
  if (args.check_syntax)
    c->no_exec = FALSE;
  if (args.mrbfile) {
    v = mrb_load_irep_file_cxt(mrb, args.rfp, c);
  }
  else {
    mrb_sym zero_sym = mrb_intern_lit(mrb, "$0");

    if (args.rfp) {
      char *cmdline;
      cmdline = args.cmdline ? args.cmdline : "-";
      mrbc_filename(mrb, c, cmdline);
      mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline));
      v = mrb_load_file_cxt(mrb, args.rfp, c);
    }
    else {
      mrbc_filename(mrb, c, "-e");
      mrb_gv_set(mrb, zero_sym, mrb_str_new_lit(mrb, "-e"));
      v = mrb_load_string_cxt(mrb, args.cmdline, c);
    }
  }
  mrbc_context_free(mrb, c);
  if (mrb->exc) {
    if (!mrb_undef_p(v)) {
      mrb_print_error(mrb);
    }
    n = -1;
  }
  else if (args.check_syntax) {
    printf("Syntax OK\n");
  }
  cleanup(mrb, &args);

  return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Exemple #8
0
int
main(int argc, char **argv)
{
  mrb_state *mrb = mrb_open();
  int n = -1;
  int i;
  struct _args args;
  mrb_value ARGV;
  mrbc_context *c;
  mrb_value v;
  mrb_sym zero_sym;
  struct mrb_jmpbuf c_jmp;
  int ai;

  if (mrb == NULL) {
    fputs("Invalid mrb_state, exiting mruby\n", stderr);
    return EXIT_FAILURE;
  }

  n = parse_args(mrb, argc, argv, &args);
  if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) {
    cleanup(mrb, &args);
    usage(argv[0]);
    return n;
  }

  ai = mrb_gc_arena_save(mrb);
  MRB_TRY(&c_jmp) {
    mrb->jmp = &c_jmp;
    ARGV = mrb_ary_new_capa(mrb, args.argc);
    for (i = 0; i < args.argc; i++) {
      char* utf8 = mrb_utf8_from_locale(args.argv[i], -1);
      if (utf8) {
        mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, utf8));
        mrb_utf8_free(utf8);
      }
    }
    mrb_define_global_const(mrb, "ARGV", ARGV);

    c = mrbc_context_new(mrb);
    if (args.verbose)
      c->dump_result = TRUE;
    if (args.check_syntax)
      c->no_exec = TRUE;

    /* Set $0 */
    zero_sym = mrb_intern_lit(mrb, "$0");
    if (args.rfp) {
      const char *cmdline;
      cmdline = args.cmdline ? args.cmdline : "-";
      mrbc_filename(mrb, c, cmdline);
      mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline));
    }
    else {
      mrbc_filename(mrb, c, "-e");
      mrb_gv_set(mrb, zero_sym, mrb_str_new_lit(mrb, "-e"));
    }

    /* Load program */
    if (args.mrbfile) {
      v = mrb_load_irep_file_cxt(mrb, args.rfp, c);
    }
    else if (args.rfp) {
      v = mrb_load_file_cxt(mrb, args.rfp, c);
    }
    else {
      char* utf8 = mrb_utf8_from_locale(args.cmdline, -1);
      if (!utf8) abort();
      v = mrb_load_string_cxt(mrb, utf8, c);
      mrb_utf8_free(utf8);
    }

    mrb_gc_arena_restore(mrb, ai);
    mrbc_context_free(mrb, c);
    if (mrb->exc) {
      if (!mrb_undef_p(v)) {
        mrb_print_error(mrb);
      }
      n = -1;
    }
    else if (args.check_syntax) {
      printf("Syntax OK\n");
    }
  }
  MRB_CATCH(&c_jmp) {           /* error */
  }
  MRB_END_EXC(&c_jmp);
  cleanup(mrb, &args);

  return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
mrb_value
mrb_load_string(mrb_state *mrb, const char *s)
{
    return mrb_load_string_cxt(mrb, s, NULL);
}