Skip to content

Commit

Permalink
rename mrb_load_irep etc. for naming consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Dec 4, 2012
1 parent c7a2af3 commit b8e5a57
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
5 changes: 3 additions & 2 deletions include/mruby/dump.h
Expand Up @@ -16,9 +16,10 @@ extern "C" {
#include <stdint.h>

int mrb_dump_irep(mrb_state*,int,FILE*);
int mrb_load_irep(mrb_state*,FILE*);
int mrb_load_irep_offset(mrb_state*,FILE*,long);
int mrb_read_irep(mrb_state*,const char*);
int mrb_read_irep_file(mrb_state*,FILE*);
mrb_value mrb_load_irep(mrb_state*,const char*);
mrb_value mrb_load_irep_file(mrb_state*,FILE*);

int mrb_bdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname);

Expand Down
36 changes: 34 additions & 2 deletions src/load.c
Expand Up @@ -11,6 +11,7 @@
#ifdef ENABLE_REGEXP
#include "re.h"
#endif
#include "mruby/proc.h"
#include "mruby/irep.h"

typedef struct _RiteFILE
Expand All @@ -28,7 +29,7 @@ const char hex2bin[256] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, //30-3f
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, //40-4f
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //50-5f
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0 //60-6f
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0 //60-6f
//70-ff
};

Expand Down Expand Up @@ -241,7 +242,7 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_
}

int
mrb_load_irep(mrb_state *mrb, FILE* fp)
mrb_read_irep_file(mrb_state *mrb, FILE* fp)
{
int ret, i;
uint32_t len, rlen = 0;
Expand Down Expand Up @@ -659,3 +660,34 @@ hex_to_str(char *hex, char *str, uint16_t *str_len)
}
return str;
}

static void
irep_error(mrb_state *mrb, int n)
{
static const char msg[] = "irep load error";
mrb->exc = (struct RObject*)mrb_object(mrb_exc_new(mrb, E_SCRIPT_ERROR, msg, sizeof(msg) - 1));
}

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));
}

mrb_value
mrb_load_irep(mrb_state *mrb, const char *bin)
{
int n = mrb_read_irep(mrb, bin);

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));
}
4 changes: 1 addition & 3 deletions test/init_mrbtest.c
Expand Up @@ -9,9 +9,7 @@ extern const char mrbtest_irep[];
void
mrb_init_mrbtest(mrb_state *mrb)
{
int n = mrb_read_irep(mrb, mrbtest_irep);

mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
mrb_load_irep(mrb, mrbtest_irep);
if (mrb->exc) {
mrb_p(mrb, mrb_obj_value(mrb->exc));
exit(0);
Expand Down
2 changes: 1 addition & 1 deletion tools/mruby/mruby.c
Expand Up @@ -238,7 +238,7 @@ main(int argc, char **argv)
mrb_define_global_const(mrb, "ARGV", ARGV);

if (args.mrbfile) {
n = mrb_load_irep(mrb, args.rfp);
n = mrb_read_irep_file(mrb, args.rfp);
if (n < 0) {
fprintf(stderr, "failed to load mrb file: %s\n", args.cmdline);
}
Expand Down

0 comments on commit b8e5a57

Please sign in to comment.