Skip to content

Commit

Permalink
Handles exceptions from code generation phase; fix #3695
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Jun 7, 2017
1 parent fc7096f commit b9f771d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion mrbgems/mruby-bin-mruby/tools/mruby/mruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ main(int argc, char **argv)
mrb_gc_arena_restore(mrb, ai);
mrbc_context_free(mrb, c);
if (mrb->exc) {
if (!mrb_undef_p(v)) {
if (mrb_undef_p(v)) {
mrb_p(mrb, mrb_obj_value(mrb->exc));
}
else {
mrb_print_error(mrb);
}
n = -1;
Expand Down
4 changes: 4 additions & 0 deletions mrbgems/mruby-compiler/core/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2993,6 +2993,7 @@ mrb_generate_code(mrb_state *mrb, parser_state *p)
{
codegen_scope *scope = scope_new(mrb, 0, 0);
struct RProc *proc;
struct mrb_jmpbuf *prev_jmp = mrb->jmp;

if (!scope) {
return NULL;
Expand All @@ -3003,17 +3004,20 @@ mrb_generate_code(mrb_state *mrb, parser_state *p)
scope->filename_index = p->current_filename_index;

MRB_TRY(&scope->jmp) {
mrb->jmp = &scope->jmp;
/* prepare irep */
codegen(scope, p->tree, NOVAL);
proc = mrb_proc_new(mrb, scope->irep);
mrb_irep_decref(mrb, scope->irep);
mrb_pool_close(scope->mpool);
proc->c = NULL;
mrb->jmp = prev_jmp;
return proc;
}
MRB_CATCH(&scope->jmp) {
mrb_irep_decref(mrb, scope->irep);
mrb_pool_close(scope->mpool);
mrb->jmp = prev_jmp;
return NULL;
}
MRB_END_EXC(&scope->jmp);
Expand Down
9 changes: 6 additions & 3 deletions mrbgems/mruby-compiler/core/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -5579,7 +5579,6 @@ mrb_parser_parse(parser_state *p, mrbc_context *c)
}
MRB_CATCH(p->mrb->jmp) {
p->nerr++;
mrb_p(p->mrb, mrb_obj_value(p->mrb->exc));
}
MRB_END_EXC(p->mrb->jmp);
p->mrb->jmp = 0;
Expand Down Expand Up @@ -5789,15 +5788,19 @@ mrb_load_exec(mrb_state *mrb, struct mrb_parser_state *p, mrbc_context *c)
return mrb_undef_value();
}
else {
mrb->exc = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, E_SYNTAX_ERROR, "syntax error"));
if (mrb->exc == NULL) {
mrb->exc = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, E_SYNTAX_ERROR, "syntax error"));
}
mrb_parser_free(p);
return mrb_undef_value();
}
}
proc = mrb_generate_code(mrb, p);
mrb_parser_free(p);
if (proc == NULL) {
mrb->exc = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, E_SCRIPT_ERROR, "codegen error"));
if (mrb->exc == NULL) {
mrb->exc = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, E_SCRIPT_ERROR, "codegen error"));
}
return mrb_undef_value();
}
if (c) {
Expand Down

0 comments on commit b9f771d

Please sign in to comment.