modules/mruby/tools/mrbc/mrbc.c in webruby-0.2.2 vs modules/mruby/tools/mrbc/mrbc.c in webruby-0.2.4

- old
+ new

@@ -9,12 +9,10 @@ #define RITEBIN_EXT ".mrb" #define C_EXT ".c" void mrb_show_version(mrb_state *); void mrb_show_copyright(mrb_state *); -void parser_dump(mrb_state*, struct mrb_ast_node*, int); -void codedump_all(mrb_state*, int); struct mrbc_args { int argc; char **argv; int idx; @@ -179,57 +177,63 @@ } mrb_parser_set_filename(p, fn); return 0; } -static int +static mrb_value load_file(mrb_state *mrb, struct mrbc_args *args) { mrbc_context *c; mrb_value result; char *input = args->argv[args->idx]; FILE *infile; + int need_close = FALSE; c = mrbc_context_new(mrb); if (args->verbose) c->dump_result = 1; c->no_exec = 1; if (input[0] == '-' && input[1] == '\0') { infile = stdin; } - else if ((infile = fopen(input, "r")) == NULL) { - fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input); - return EXIT_FAILURE; + else { + need_close = TRUE; + if ((infile = fopen(input, "r")) == NULL) { + fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input); + return mrb_nil_value(); + } } mrbc_filename(mrb, c, input); args->idx++; if (args->idx < args->argc) { + need_close = FALSE; mrbc_partial_hook(mrb, c, partial_hook, (void*)args); } result = mrb_load_file_cxt(mrb, infile, c); - if (mrb_undef_p(result) || mrb_fixnum(result) < 0) { - mrbc_context_free(mrb, c); - return EXIT_FAILURE; - } + if (need_close) fclose(infile); mrbc_context_free(mrb, c); - return EXIT_SUCCESS; + if (mrb_undef_p(result)) { + return mrb_nil_value(); + } + return result; } static int -dump_file(mrb_state *mrb, FILE *wfp, const char *outfile, struct mrbc_args *args) +dump_file(mrb_state *mrb, FILE *wfp, const char *outfile, struct RProc *proc, struct mrbc_args *args) { int n = MRB_DUMP_OK; + mrb_irep *irep = proc->body.irep; if (args->initname) { - n = mrb_dump_irep_cfunc(mrb, 0, args->debug_info, wfp, args->initname); + n = mrb_dump_irep_cfunc(mrb, irep, args->debug_info, wfp, args->initname); if (n == MRB_DUMP_INVALID_ARGUMENT) { fprintf(stderr, "%s: invalid C language symbol name\n", args->initname); } } else { - n = mrb_dump_irep_binary(mrb, 0, args->debug_info, wfp); + n = mrb_dump_irep_binary(mrb, irep, args->debug_info, wfp); } if (n != MRB_DUMP_OK) { fprintf(stderr, "%s: error in mrb dump (%s) %d\n", args->prog, outfile, n); } return n; @@ -240,10 +244,11 @@ { mrb_state *mrb = mrb_open(); int n, result; struct mrbc_args args; FILE *wfp; + mrb_value load; if (mrb == NULL) { fputs("Invalid mrb_state, exiting mrbc\n", stderr); return EXIT_FAILURE; } @@ -267,16 +272,17 @@ return EXIT_FAILURE; } } args.idx = n; - if (load_file(mrb, &args) == EXIT_FAILURE) { + load = load_file(mrb, &args); + if (mrb_nil_p(load)) { cleanup(mrb, &args); return EXIT_FAILURE; } if (args.check_syntax) { - printf("%s:%s:Syntax OK", args.prog, argv[n]); + printf("%s:%s:Syntax OK\n", args.prog, argv[n]); } if (args.check_syntax) { cleanup(mrb, &args); return EXIT_SUCCESS; @@ -293,10 +299,10 @@ } else { fprintf(stderr, "Output file is required\n"); return EXIT_FAILURE; } - result = dump_file(mrb, wfp, args.outfile, &args); + result = dump_file(mrb, wfp, args.outfile, mrb_proc_ptr(load), &args); fclose(wfp); cleanup(mrb, &args); if (result != MRB_DUMP_OK) { return EXIT_FAILURE; }