예제 #1
0
파일: dump.c 프로젝트: 14923523/mruby
int
mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, int debug_info, FILE *fp, const char *initname)
{
  uint8_t *bin = NULL;
  size_t bin_size = 0, bin_idx = 0;
  int result;

  if (fp == NULL || initname == NULL || !is_valid_c_symbol_name(initname)) {
    return MRB_DUMP_INVALID_ARGUMENT;
  }

  result = mrb_dump_irep(mrb, irep, debug_info, &bin, &bin_size);
  if (result == MRB_DUMP_OK) {
    fprintf(fp, "#include <stdint.h>\n"); /* for uint8_t under at least Darwin */
    fprintf(fp, "const uint8_t %s[] = {", initname);
    while (bin_idx < bin_size) {
      if (bin_idx % 16 == 0) fputs("\n", fp);
      fprintf(fp, "0x%02x,", bin[bin_idx++]);
    }
    fputs("\n};\n", fp);
  }

  mrb_free(mrb, bin);
  return result;
}
예제 #2
0
파일: mrbc.c 프로젝트: nurse/mruby
int
main(int argc, char **argv)
{
  mrb_state *mrb = mrb_open();
  int n = -1;
  struct _args args;
  struct mrb_parser_state *p;

  n = parse_args(mrb, argc, argv, &args);

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

  p = mrb_parse_file(mrb, args.rfp);
  if (!p || !p->tree || p->nerr) {
    cleanup(&args);
    mrb_close(mrb);
    return -1;
  }

  if (args.verbose)
    parser_dump(mrb, p->tree, 0);

  n = mrb_generate_code(mrb, p->tree);
  mrb_pool_close(p->pool);

  if (args.verbose)
    codedump_all(mrb, n);

  if (n < 0 || args.check_syntax) {
    cleanup(&args);
    mrb_close(mrb);
    return n;
  }
  if (args.initname) {
    if (args.dump_type == DUMP_TYPE_BIN)
      n = mrb_bdump_irep(mrb, n, args.wfp, args.initname);
    else
      n = mrb_cdump_irep(mrb, n, args.wfp, args.initname);
  }
  else {
    n = mrb_dump_irep(mrb, n, args.wfp);
  }

  cleanup(&args);
  mrb_close(mrb);

  return n;
}
예제 #3
0
파일: mrbc.c 프로젝트: jjue/mruby
int
main(int argc, char **argv)
{
  mrb_state *mrb = mrb_open();
  int n = -1;
  struct _args args;
  mrbc_context *c;
  mrb_value result;

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

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

  c = mrbc_context_new(mrb);
  if (args.verbose)
    c->dump_result = 1;
  c->no_exec = 1;
  c->filename = args.filename;
  result = mrb_load_file_cxt(mrb, args.rfp, c);
  if (mrb_undef_p(result) || mrb_fixnum(result) < 0) {
    cleanup(mrb, &args);
    return EXIT_FAILURE;
  }
  if (args.check_syntax) {
    printf("Syntax OK\n");
    cleanup(mrb, &args);
    return EXIT_SUCCESS;
  }
  if (args.initname) {
    if (args.dump_type == DUMP_TYPE_BIN)
      n = mrb_bdump_irep(mrb, n, args.wfp, args.initname);
    else
      n = mrb_cdump_irep(mrb, n, args.wfp, args.initname);
  }
  else {
    n = mrb_dump_irep(mrb, n, args.wfp);
  }

  cleanup(mrb, &args);
  return EXIT_SUCCESS;
}
예제 #4
0
파일: dump.c 프로젝트: Hozum/mruby
int
mrb_dump_irep_binary(mrb_state *mrb, size_t start_index, int debug_info, FILE* fp)
{
  uint8_t *bin = NULL;
  size_t bin_size = 0;
  int result;

  if (fp == NULL) {
    return MRB_DUMP_INVALID_ARGUMENT;
  }

  result = mrb_dump_irep(mrb, start_index, debug_info, &bin, &bin_size);
  if (result == MRB_DUMP_OK) {
    fwrite(bin, bin_size, 1, fp);
  }

  mrb_free(mrb, bin);
  return result;
}
예제 #5
0
파일: dump.c 프로젝트: 14923523/mruby
int
mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, int debug_info, FILE* fp)
{
  uint8_t *bin = NULL;
  size_t bin_size = 0;
  int result;

  if (fp == NULL) {
    return MRB_DUMP_INVALID_ARGUMENT;
  }

  result = mrb_dump_irep(mrb, irep, debug_info, &bin, &bin_size);
  if (result == MRB_DUMP_OK) {
    if (fwrite(bin, sizeof(bin[0]), bin_size, fp) != bin_size) {
      result = MRB_DUMP_WRITE_FAULT;
    }
  }

  mrb_free(mrb, bin);
  return result;
}
예제 #6
0
파일: dump.c 프로젝트: Hozum/mruby
int
mrb_dump_irep_cfunc(mrb_state *mrb, size_t start_index, int debug_info, FILE *fp, const char *initname)
{
  uint8_t *bin = NULL;
  size_t bin_size = 0, bin_idx = 0;
  int result;

  if (fp == NULL || initname == NULL) {
    return MRB_DUMP_INVALID_ARGUMENT;
  }

  result = mrb_dump_irep(mrb, start_index, debug_info, &bin, &bin_size);
  if (result == MRB_DUMP_OK) {
    fprintf(fp, "const uint8_t %s[] = {", initname);
    while (bin_idx < bin_size) {
      if (bin_idx % 16 == 0 ) fputs("\n", fp);
      fprintf(fp, "0x%02x,", bin[bin_idx++]);
    }
    fputs("\n};\n", fp);
  }

  mrb_free(mrb, bin);
  return result;
}