Skip to content

Commit

Permalink
Don't compile rb files to mrb.
Browse files Browse the repository at this point in the history
This enables us to have proper backtraces, so when we debug it
actually makes sense.
  • Loading branch information
Blaž Hrastnik committed Jul 27, 2013
1 parent 6984b5d commit e382d50
Showing 1 changed file with 14 additions and 64 deletions.
78 changes: 14 additions & 64 deletions src/mrb_require.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ replace_stop_with_return(mrb_state *mrb, mrb_irep *irep)
}

static void
load_mrb_file_with_filepath(mrb_state *mrb, mrb_value filepath, mrb_value origfilepath)
load_mrb_file(mrb_state *mrb, mrb_value filepath)
{
char *fpath = RSTRING_PTR(filepath);
int sirep;
Expand Down Expand Up @@ -251,7 +251,7 @@ load_mrb_file_with_filepath(mrb_state *mrb, mrb_value filepath, mrb_value origfi
mrb_irep *irep = mrb->irep[n];
size_t i;
for (i = sirep; i < mrb->irep_len; i++) {
mrb->irep[i]->filename = mrb_string_value_ptr(mrb, origfilepath);
mrb->irep[i]->filename = mrb_string_value_ptr(mrb, filepath);
}

replace_stop_with_return(mrb, irep);
Expand All @@ -267,44 +267,6 @@ load_mrb_file_with_filepath(mrb_state *mrb, mrb_value filepath, mrb_value origfi
}
}

static void
load_mrb_file(mrb_state *mrb, mrb_value filepath)
{
load_mrb_file_with_filepath(mrb, filepath, filepath);
}

static mrb_value
mrb_compile(mrb_state *mrb0, char *tmpfilepath, char *filepath)
{
mrb_state *mrb = mrb_open();
mrbc_context *c;
mrb_value result;
FILE *fp;
int irep_len = mrb->irep_len;

debug("irep_len=%d\n", mrb->irep_len);
fp = fopen(filepath, "r");
c = mrbc_context_new(mrb);
c->no_exec = 1;
c->filename = filepath;
result = mrb_load_file_cxt(mrb, fp, c);
fclose(fp);
debug("result=%d, irep_len=%d\n", mrb_fixnum(result), mrb->irep_len);

fp = fopen(tmpfilepath, "w");
mrb->irep += mrb_fixnum(result);
debug("**dbg : %d\n", mrb->irep_len - irep_len);
mrb->irep_len -= irep_len;
mrb_dump_irep_binary(mrb, 0, 0, fp);
fclose(fp);

mrb->irep -= mrb_fixnum(result);
mrb->irep_len += irep_len;
mrb_close(mrb);

return mrb_nil_value();
}

void
mrb_load_irep_data(mrb_state* mrb, const uint8_t* data)
{
Expand Down Expand Up @@ -418,35 +380,23 @@ unload_so_file(mrb_state *mrb, mrb_value filepath)
static void
load_rb_file(mrb_state *mrb, mrb_value filepath)
{
mrb_value tmpfilepath;
FILE *file;
char *fpath = RSTRING_PTR(filepath);
mrb_value pid;

{
if (!exists(fpath)) {
mrb_raisef(mrb, E_LOAD_ERROR, "can't load %S", filepath);
return;
}
if (!exists(fpath)) {
mrb_raisef(mrb, E_LOAD_ERROR, "can't load %S", filepath);
return;
}

#ifdef _WIN32
{
char tmpfile[PATH_MAX];
GetTempFileName(NULL, "mruby.", 0, tmpfile);
tmpfilepath = mrb_str_new_cstr(mrb, tmpfile);
}
#else
tmpfilepath = mrb_str_new_cstr(mrb, "/tmp/mruby.");
#endif
pid = mrb_fixnum_value((int)getpid());
mrb_str_buf_append(mrb, tmpfilepath, mrb_fixnum_to_str(mrb, pid, 10));
debug("tmpfilepath: %s\n", RSTRING_PTR(tmpfilepath));
mrbc_context *mrbc_ctx = mrbc_context_new(mrb);

mrb_compile(mrb, mrb_string_value_ptr(mrb, tmpfilepath),
mrb_string_value_ptr(mrb, filepath));
load_mrb_file_with_filepath(mrb, tmpfilepath, filepath);
file = fopen((const char*)fpath, "r");
mrbc_filename(mrb, mrbc_ctx, fpath);
mrb_gv_set(mrb, mrb_intern2(mrb, "$0", 2), filepath);
mrb_load_file_cxt(mrb, file, mrbc_ctx);
fclose(file);

remove(RSTRING_PTR(tmpfilepath));
mrbc_context_free(mrb, mrbc_ctx);
}

static void
Expand All @@ -461,7 +411,7 @@ load_file(mrb_state *mrb, mrb_value filepath)
} else if (strcmp(ext, ".so") == 0) {
load_so_file(mrb, filepath);
} else {
mrb_raisef(mrb, E_LOAD_ERROR, "Filepath '%S' is invalid extension.", filepath);
mrb_raisef(mrb, E_LOAD_ERROR, "Filepath '%S' has invalid extension.", filepath);
return;
}
}
Expand Down

0 comments on commit e382d50

Please sign in to comment.