Esempio n. 1
0
static mrb_value
mrb_require_load_mrb_file(mrb_state *mrb, mrb_value self)
{
  char *path_ptr = NULL;
  FILE *fp = NULL;
  mrb_irep *irep;
  mrb_value path;

  mrb_get_args(mrb, "S", &path);
  path_ptr = mrb_str_to_cstr(mrb, path);

  fp = fopen(path_ptr, "rb");
  if (fp == NULL) {
    mrb_raisef(mrb, E_LOAD_ERROR, "can't open file -- %S", path);
  }

  irep = mrb_read_irep_file(mrb, fp);
  fclose(fp);

  if (irep) {
    eval_load_irep(mrb, irep);
  } else if (mrb->exc) {
    // fail to load
    longjmp(*(jmp_buf*)mrb->jmp, 1);
  } else {
    mrb_raisef(mrb, E_LOAD_ERROR, "can't load file -- %S", path);
    return mrb_nil_value();
  }

  return mrb_true_value();
}
Esempio n. 2
0
int main(int argc, char** argv)
{
  const char rite[] = "app.mrb";

  mrb_state* mrb = mrb_open();

  FILE* fp = fopen(rite, "r");
  if (fp == NULL) {
    fprintf(stderr, "%s not found.\n", rite);
    fprintf(stderr, "`mruby/bin/mrbc app.rb` to create app.mrb file.\n");
    exit(EXIT_FAILURE);
  }

  int n = mrb_read_irep_file(mrb, fp);
  fclose(fp);
  if (n < 0) {
    fprintf(stderr, "%s - irep load error.\n", rite);
    exit(EXIT_FAILURE);
  }
  mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));

  if (argc < 2) {
    printf("Usage %s <environment>\n", argv[0]);
    return 0;
  }

  // AppSetting
  struct RClass* clz = mrb_class_get(mrb, "AppSetting");
  mrb_value setting = mrb_obj_value(clz);
  // AppSetting.load(*targets) -> AppConfig
  mrb_value target = mrb_str_new(mrb, argv[1], strlen(argv[1]));
  mrb_value conf = mrb_funcall(mrb, setting, "load", 1, target);
  // AppConfig.#debug -> bool
  mrb_value debug = mrb_funcall(mrb, conf, "debug", 0);
  printf("debug: %d\n", mrb_type(debug) == MRB_TT_TRUE);
  // AppConfig.#timeout -> Integer
  mrb_value timeout = mrb_funcall(mrb, conf, "timeout", 0);
  printf("timeout: %d\n", timeout.value.i);
  // AppConfig.#title -> String
  mrb_value title = mrb_funcall(mrb, conf, "title", 0);
  printf("title: %s\n", mrb_string_value_ptr(mrb, title));
  // AppConfig.#messages -> Array
  mrb_value messages = mrb_funcall(mrb, conf, "messages", 0);
  {
    // Array.#size -> Integer
    mrb_value len = mrb_funcall(mrb, messages, "size", 0);
    int i;
    for (i = 0; i < len.value.i; i++) {
      // Array.#at(nth) -> object
      mrb_value message = mrb_funcall(mrb, messages, "at", 1, mrb_fixnum_value(i));
      printf("%s\n", mrb_string_value_ptr(mrb, message));
    }
  }

  mrb_close(mrb);

  return 0;
}
Esempio n. 3
0
static mrb_value
mrb_require_load_rb_str(mrb_state *mrb, mrb_value self)
{
  char *path_ptr = NULL;
  char tmpname[] = "tmp.XXXXXXXX";
  mode_t mask;
  FILE *tmpfp = NULL;
  int fd = -1, ret;
  mrb_irep *irep;
  mrb_value code, path = mrb_nil_value();

  mrb_get_args(mrb, "S|S", &code, &path);
  if (!mrb_string_p(path)) {
    path = mrb_str_new_cstr(mrb, "-");
  }
  path_ptr = mrb_str_to_cstr(mrb, path);

  mask = umask(077);
  fd = mkstemp(tmpname);
  if (fd == -1) {
    mrb_sys_fail(mrb, "can't create mkstemp() at mrb_require_load_rb_str");
  }
  umask(mask);

  tmpfp = fopen(tmpname, "w+");
  if (tmpfp == NULL) {
    mrb_sys_fail(mrb, "can't open temporay file at mrb_require_load_rb_str");
  }

  ret = compile_rb2mrb(mrb, RSTRING_PTR(code), RSTRING_LEN(code), path_ptr, tmpfp);
  if (ret != MRB_DUMP_OK) {
    fclose(tmpfp);
    remove(tmpname);
    mrb_raisef(mrb, E_LOAD_ERROR, "can't load file -- %S", path);
    return mrb_nil_value();
  }

  rewind(tmpfp);
  irep = mrb_read_irep_file(mrb, tmpfp);
  fclose(tmpfp);
  remove(tmpname);

  if (irep) {
    eval_load_irep(mrb, irep);
  } else if (mrb->exc) {
    // fail to load
    longjmp(*(jmp_buf*)mrb->jmp, 1);
  } else {
    mrb_raisef(mrb, E_LOAD_ERROR, "can't load file -- %S", path);
    return mrb_nil_value();
  }

  return mrb_true_value();
}
Esempio n. 4
0
File: load.c Progetto: kano4/mruby
mrb_value
mrb_load_irep_file(mrb_state *mrb, FILE* fp)
{
  int n = mrb_read_irep_file(mrb, fp);

  if (n < 0) {
    irep_error(mrb, n);
    return mrb_nil_value();
  }
  return mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
}
Esempio n. 5
0
static int
strip(mrb_state *mrb, struct strip_args *args)
{
  int i;

  for (i = args->argc_start; i < args->argc; ++i) {
    char *filename;
    FILE *rfile;
    mrb_irep *irep;
    FILE *wfile;
    int dump_result;

    filename = args->argv[i];
    rfile = fopen(filename, "rb");
    if (rfile == NULL) {
      fprintf(stderr, "can't open file for reading %s\n", filename);
      return EXIT_FAILURE;
    }

    irep = mrb_read_irep_file(mrb, rfile);
    fclose(rfile);
    if (irep == NULL) {
      fprintf(stderr, "can't read irep file %s\n", filename);
      return EXIT_FAILURE;
    }

    /* clear lv if --lvar is enabled */
    if (args->lvar) {
      irep_remove_lv(mrb, irep);
    }

    wfile = fopen(filename, "wb");
    if (wfile == NULL) {
      fprintf(stderr, "can't open file for writing %s\n", filename);
      mrb_irep_decref(mrb, irep);
      return EXIT_FAILURE;
    }

    /* debug flag must always be false */
    dump_result = mrb_dump_irep_binary(mrb, irep, FALSE, wfile);

    fclose(wfile);
    mrb_irep_decref(mrb, irep);

    if (dump_result != MRB_DUMP_OK) {
      fprintf(stderr, "error occurred during dumping %s\n", filename);
      return EXIT_FAILURE;
    }
  }
  return EXIT_SUCCESS;
}
Esempio n. 6
0
static void
load_mrb_file(mrb_state *mrb, mrb_value filepath)
{
  char *fpath = RSTRING_PTR(filepath);
  int arena_idx;
  FILE *fp;
  mrb_irep *irep;

  {
    FILE *fp = fopen(fpath, "rb");
    if (fp == NULL) {
      mrb_load_fail(
        mrb,
        mrb_str_new_cstr(mrb, fpath),
        "cannot load such file"
      );
      return;
    }
    fclose(fp);
  }

  arena_idx = mrb_gc_arena_save(mrb);

  fp = fopen(fpath, "rb");
  irep = mrb_read_irep_file(mrb, fp);
  fclose(fp);

  mrb_gc_arena_restore(mrb, arena_idx);

  if (irep) {
    struct RProc *proc;
    /*
    size_t i;
    for (i = sirep; i < mrb->irep_len; i++) {
      mrb->irep[i]->filename = mrb_string_value_ptr(mrb, filepath);
    }
    */

    replace_stop_with_return(mrb, irep);
    proc = mrb_proc_new(mrb, irep);
    proc->target_class = mrb->object_class;

    arena_idx = mrb_gc_arena_save(mrb);
    mrb_yield_with_class(mrb, mrb_obj_value(proc), 0, NULL, mrb_top_self(mrb), mrb->object_class);
    mrb_gc_arena_restore(mrb, arena_idx);
  } else if (mrb->exc) {
    // fail to load
    longjmp(*(jmp_buf*)mrb->jmp, 1);
  }
}
Esempio n. 7
0
static void
load_mrb_file(mrb_state *mrb, mrb_value filepath)
{
  char *fpath = RSTRING_PTR(filepath);
  int ai;
  FILE *fp;
  mrb_irep *irep;

  fp = fopen(fpath, "rb");
  if (fp == NULL) {
    mrb_load_fail(
      mrb,
      mrb_str_new_cstr(mrb, fpath),
      "cannot load such file"
    );
    return;
  }

  ai = mrb_gc_arena_save(mrb);

  irep = mrb_read_irep_file(mrb, fp);
  fclose(fp);

  mrb_gc_arena_restore(mrb, ai);

  if (irep) {
    struct RProc *proc;
    /*
    size_t i;
    for (i = sirep; i < mrb->irep_len; i++) {
      mrb->irep[i]->filename = mrb_string_value_ptr(mrb, filepath);
    }
    */

#ifdef USE_MRUBY_OLD_BYTE_CODE
    replace_stop_with_return(mrb, irep);
#endif
    proc = mrb_proc_new(mrb, irep);
    MRB_PROC_SET_TARGET_CLASS(proc, mrb->object_class);

    ai = mrb_gc_arena_save(mrb);
    mrb_yield_with_class(mrb, mrb_obj_value(proc), 0, NULL, mrb_top_self(mrb), mrb->object_class);
    mrb_gc_arena_restore(mrb, ai);
  } else if (mrb->exc) {
    // fail to load
    longjmp(*(jmp_buf*)mrb->jmp, 1);
  }
}
Esempio n. 8
0
mrb_value
mrb_load_irep_file_cxt(mrb_state *mrb, FILE* fp, mrbc_context *c)
{
  mrb_irep *irep = mrb_read_irep_file(mrb, fp);
  mrb_value val;
  struct RProc *proc;

  if (!irep) {
    irep_error(mrb);
    return mrb_nil_value();
  }
  proc = mrb_proc_new(mrb, irep);
  mrb_irep_decref(mrb, irep);
  if (c && c->no_exec) return mrb_obj_value(proc);
  val = mrb_toplevel_run(mrb, proc);
  return val;
}
Esempio n. 9
0
MRB_API mrb_value
mrb_load_irep_file_cxt(mrb_state *mrb, FILE* fp, mrbc_context *c)
{
  return load_irep(mrb, mrb_read_irep_file(mrb, fp), c);
}
Esempio n. 10
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) {
    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);

  if (args.mrbfile) {
    n = mrb_read_irep_file(mrb, args.rfp);
    if (n < 0) {
      fprintf(stderr, "failed to load mrb file: %s\n", args.cmdline);
    }
    else if (!args.check_syntax) {
      mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
      n = 0;
      if (mrb->exc) {
        showcallinfo(mrb);
        p(mrb, mrb_obj_value(mrb->exc));
        n = -1;
      }
    }
  }
  else {
    mrbc_context *c = mrbc_context_new(mrb);
    mrb_sym zero_sym = mrb_intern2(mrb, "$0", 2);
    mrb_value v;

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

    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(mrb, "-e", 2));
      v = mrb_load_string_cxt(mrb, args.cmdline, c);
    }

    mrbc_context_free(mrb, c);
    if (mrb->exc) {
      if (!mrb_undef_p(v)) {
        showcallinfo(mrb);
        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;
}