add new function mrb_load_irep_file_cxt() and simplifies mruby.c

parent d8f9a30e
......@@ -19,6 +19,7 @@ int mrb_dump_irep_binary(mrb_state*, mrb_irep*, int, FILE*);
int mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep*, int, FILE *f, const char *initname);
mrb_irep *mrb_read_irep_file(mrb_state*, FILE*);
mrb_value mrb_load_irep_file(mrb_state*,FILE*);
mrb_value mrb_load_irep_file_cxt(mrb_state*, FILE*, mrbc_context*);
#endif
mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*);
......
......@@ -11,6 +11,8 @@
extern "C" {
#endif
#include "mruby/compile.h"
enum irep_pool_type {
IREP_TT_STRING,
IREP_TT_FIXNUM,
......@@ -50,6 +52,7 @@ typedef struct mrb_irep {
mrb_irep *mrb_add_irep(mrb_state *mrb);
mrb_value mrb_load_irep(mrb_state*, const uint8_t*);
mrb_value mrb_load_irep_cxt(mrb_state*, const uint8_t*, mrbc_context*);
void mrb_irep_free(mrb_state*, struct mrb_irep*);
void mrb_irep_incref(mrb_state*, struct mrb_irep*);
void mrb_irep_decref(mrb_state*, struct mrb_irep*);
......
......@@ -178,6 +178,8 @@ main(int argc, char **argv)
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);
......@@ -197,30 +199,16 @@ main(int argc, char **argv)
}
mrb_define_global_const(mrb, "ARGV", ARGV);
c = mrbc_context_new(mrb);
if (args.verbose)
c->dump_result = 1;
if (args.check_syntax)
c->no_exec = 1;
if (args.mrbfile) {
mrb_irep *irep = mrb_read_irep_file(mrb, args.rfp);
if (!irep) {
fprintf(stderr, "failed to load mrb file: %s\n", args.cmdline);
}
else if (!args.check_syntax) {
mrb_context_run(mrb, mrb_proc_new(mrb, irep), mrb_top_self(mrb), 0);
mrb_irep_decref(mrb, irep);
n = 0;
if (mrb->exc) {
mrb_print_error(mrb);
n = -1;
}
}
v = mrb_load_irep_file_cxt(mrb, args.rfp, c);
}
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;
......@@ -234,17 +222,16 @@ main(int argc, char **argv)
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)) {
mrb_print_error(mrb);
}
n = -1;
}
else if (args.check_syntax) {
printf("Syntax OK\n");
}
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);
......
......@@ -477,7 +477,7 @@ irep_error(mrb_state *mrb)
}
mrb_value
mrb_load_irep(mrb_state *mrb, const uint8_t *bin)
mrb_load_irep_cxt(mrb_state *mrb, const uint8_t *bin, mrbc_context *c)
{
mrb_irep *irep = mrb_read_irep(mrb, bin);
mrb_value val;
......@@ -488,11 +488,18 @@ mrb_load_irep(mrb_state *mrb, const uint8_t *bin)
return mrb_nil_value();
}
proc = mrb_proc_new(mrb, irep);
val = mrb_context_run(mrb, proc, mrb_top_self(mrb), 0);
mrb_irep_decref(mrb, irep);
if (c && c->no_exec) return mrb_obj_value(proc);
val = mrb_context_run(mrb, proc, mrb_top_self(mrb), 0);
return val;
}
mrb_value
mrb_load_irep(mrb_state *mrb, const uint8_t *bin)
{
return mrb_load_irep_cxt(mrb, bin, NULL);
}
#ifdef ENABLE_STDIO
static int
......@@ -691,7 +698,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp)
}
mrb_value
mrb_load_irep_file(mrb_state *mrb, FILE* fp)
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;
......@@ -702,8 +709,15 @@ mrb_load_irep_file(mrb_state *mrb, FILE* fp)
return mrb_nil_value();
}
proc = mrb_proc_new(mrb, irep);
val = mrb_context_run(mrb, proc, mrb_top_self(mrb), 0);
mrb_irep_decref(mrb, irep);
if (c && c->no_exec) return mrb_obj_value(proc);
val = mrb_context_run(mrb, proc, mrb_top_self(mrb), 0);
return val;
}
mrb_value
mrb_load_irep_file(mrb_state *mrb, FILE* fp)
{
return mrb_load_irep_file_cxt(mrb, fp, NULL);
}
#endif /* ENABLE_STDIO */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment