Skip to content

Commit

Permalink
improve mrbc_raise, mrbc_raisef functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
HirohitoHigashi committed May 31, 2022
1 parent c8fcfb2 commit 9d207e3
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,14 @@ void mrbc_exception_delete(mrbc_value *value)
*/
void mrbc_raise( struct VM *vm, struct RClass *exc_cls, const char *msg )
{
vm->exception = mrbc_exception_new( vm,
if( vm ) {
vm->exception = mrbc_exception_new( vm,
exc_cls ? exc_cls : MRBC_CLASS(RuntimeError), msg, 0 );
vm->flag_preemption = 2;
vm->flag_preemption = 2;

} else {
mrbc_printf("Exception : %s (%s)\n", msg ? msg : mrbc_symid_to_str(exc_cls->sym_id), mrbc_symid_to_str(exc_cls->sym_id));
}
}


Expand All @@ -158,16 +163,22 @@ void mrbc_raisef( struct VM *vm, struct RClass *exc_cls, const char *fstr, ... )
va_list ap;
va_start( ap, fstr );

char *buf = mrbc_alloc( vm, 32 );
if( !buf ) return; // ENOMEM
if( vm ) {
char *buf = mrbc_alloc( vm, 32 );
if( !buf ) return; // ENOMEM

mrbc_vsprintf( buf, 32, fstr, ap );
va_end( ap );
mrbc_vsprintf( buf, 32, fstr, ap );

vm->exception = mrbc_exception_new_alloc( vm,
vm->exception = mrbc_exception_new_alloc( vm,
exc_cls ? exc_cls : MRBC_CLASS(RuntimeError),
buf, strlen(buf) );
vm->flag_preemption = 2;
vm->flag_preemption = 2;
} else {

mrbc_vprintf( fstr, ap );
}

va_end( ap );
}


Expand Down

0 comments on commit 9d207e3

Please sign in to comment.