Skip to content

Commit

Permalink
create function Parrot_ex_get_current_handler to avoid duplicated cod…
Browse files Browse the repository at this point in the history
…e in core.ops
  • Loading branch information
NotFound committed Apr 28, 2012
1 parent 4dcb543 commit 0c24220
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 100 deletions.
1 change: 1 addition & 0 deletions config/gen/makefiles/root.in
Expand Up @@ -1544,6 +1544,7 @@ src/dynext$(O) : $(PARROT_H_HEADERS) $(INC_DIR)/dynext.h \
src/exceptions$(O) : $(PARROT_H_HEADERS) \
src/exceptions.str src/exceptions.c \
$(INC_DIR)/events.h \
$(INC_PMC_DIR)/pmc_exception.h \
$(INC_PMC_DIR)/pmc_continuation.h

src/threads$(O) : $(PARROT_H_HEADERS) $(INC_DIR)/atomic.h src/threads.c
Expand Down
8 changes: 8 additions & 0 deletions include/parrot/exceptions.h
Expand Up @@ -166,6 +166,12 @@ PMC * Parrot_ex_build_exception(PARROT_INTERP,
ARGIN_NULLOK(STRING *msg))
__attribute__nonnull__(1);

PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
PMC * Parrot_ex_get_current_handler(PARROT_INTERP, ARGIN_NULLOK(PMC *expmc))
__attribute__nonnull__(1);

PARROT_EXPORT
void Parrot_ex_mark_unhandled(PARROT_INTERP, ARGIN(PMC *exception))
__attribute__nonnull__(1)
Expand Down Expand Up @@ -248,6 +254,8 @@ void Parrot_print_backtrace(void);
, PARROT_ASSERT_ARG(jp))
#define ASSERT_ARGS_Parrot_ex_build_exception __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_Parrot_ex_get_current_handler __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_Parrot_ex_mark_unhandled __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(exception))
Expand Down
33 changes: 33 additions & 0 deletions src/exceptions.c
Expand Up @@ -20,6 +20,7 @@ Define the core subsystem for exceptions.
#include "parrot/parrot.h"
#include "exceptions.str"
#include "pmc/pmc_continuation.h"
#include "pmc/pmc_exception.h"
#include "parrot/exceptions.h"
#include "parrot/events.h"

Expand Down Expand Up @@ -507,6 +508,38 @@ Parrot_ex_mark_unhandled(PARROT_INTERP, ARGIN(PMC *exception))

/*
=item C<PMC * Parrot_ex_get_current_handler(PARROT_INTERP, PMC *expmc)>
Get the current exception handler from expmc.
If expmc is an exception handler, return itself.
If it's an exception, return its active handler.
=cut
*/

PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
PMC *
Parrot_ex_get_current_handler(PARROT_INTERP, ARGIN_NULLOK(PMC *expmc))
{
ASSERT_ARGS(Parrot_ex_get_current_handler)
PMC *eh = PMCNULL;
if (!PMC_IS_NULL(expmc)) {
/* If isa ExceptionHandler, use it. If isa Exception, get its active handler */
if (expmc->vtable->base_type == enum_class_Exception)
GETATTR_Exception_handler(interp, expmc, eh);
else if (VTABLE_isa(interp, expmc, CONST_STRING(interp, "ExceptionHandler")))
eh = expmc;
else if (VTABLE_isa(interp, expmc, CONST_STRING(interp, "Exception")))
eh = VTABLE_get_attr_str(interp, expmc, CONST_STRING(interp, "handler"));
}
return eh;
}

/*
=head2 Error Functions
=over 4
Expand Down
22 changes: 2 additions & 20 deletions src/ops/core.ops
Expand Up @@ -872,16 +872,7 @@ inline op exit(in INT) :flow {
op finalize(in PMC) {
/* Go to the next op after loop unrolling */
opcode_t * const dest = expr NEXT();
PMC *eh = PMCNULL;
if (!PMC_IS_NULL($1)) {
/* If isa ExceptionHandler, use it. If isa Exception, get its active handler */
if ($1->vtable->base_type == enum_class_Exception)
GETATTR_Exception_handler(interp, $1, eh);
else if (VTABLE_isa(interp, $1, Parrot_str_new_constant(interp, "ExceptionHandler")))
eh = $1;
else if (VTABLE_isa(interp, $1, Parrot_str_new_constant(interp, "Exception")))
eh = VTABLE_get_attr_str(interp, $1, Parrot_str_new_constant(interp, "handler"));
}
PMC *eh = Parrot_ex_get_current_handler(interp, $1);
if (!PMC_IS_NULL(eh)) {
/* Get the runloop_id from the continuation and jump to it. */
Parrot_runloop *rl = interp->current_runloop;
Expand All @@ -906,16 +897,7 @@ op finalize(in PMC) {
}

op pop_upto_eh(in PMC) {
PMC *eh = PMCNULL;
if (!PMC_IS_NULL($1)) {
/* If isa ExceptionHandler, use it. If isa Exception, get its active handler */
if ($1->vtable->base_type == enum_class_Exception)
GETATTR_Exception_handler(interp, $1, eh);
else if (VTABLE_isa(interp, $1, Parrot_str_new_constant(interp, "ExceptionHandler")))
eh = $1;
else if (VTABLE_isa(interp, $1, Parrot_str_new_constant(interp, "Exception")))
eh = VTABLE_get_attr_str(interp, $1, Parrot_str_new_constant(interp, "handler"));
}
PMC *eh = Parrot_ex_get_current_handler(interp, $1);
if (!PMC_IS_NULL(eh))
Parrot_cx_delete_upto_handler_local(interp, eh);
}
Expand Down
84 changes: 4 additions & 80 deletions src/ops/core_ops.c
Expand Up @@ -14132,26 +14132,7 @@ Parrot_exit_ic(opcode_t *cur_opcode, PARROT_INTERP) {
opcode_t *
Parrot_finalize_p(opcode_t *cur_opcode, PARROT_INTERP) {
opcode_t * const dest = cur_opcode + 2;
PMC * eh = PMCNULL;

if ((!PMC_IS_NULL(PREG(1)))) {
if ((PREG(1)->vtable->base_type == enum_class_Exception)) {
GETATTR_Exception_handler(interp, PREG(1), eh);
}
else {
if (VTABLE_isa(interp, PREG(1), Parrot_str_new_constant(interp, "ExceptionHandler"))) {
eh = PREG(1);
}
else {
if (VTABLE_isa(interp, PREG(1), Parrot_str_new_constant(interp, "Exception"))) {
eh = VTABLE_get_attr_str(interp, PREG(1), Parrot_str_new_constant(interp, "handler"));
}

}

}

}
PMC * eh = Parrot_ex_get_current_handler(interp, PREG(1));

if ((!PMC_IS_NULL(eh))) {
Parrot_runloop * rl = interp->current_runloop;
Expand Down Expand Up @@ -14187,26 +14168,7 @@ Parrot_finalize_p(opcode_t *cur_opcode, PARROT_INTERP) {
opcode_t *
Parrot_finalize_pc(opcode_t *cur_opcode, PARROT_INTERP) {
opcode_t * const dest = cur_opcode + 2;
PMC * eh = PMCNULL;

if ((!PMC_IS_NULL(PCONST(1)))) {
if ((PCONST(1)->vtable->base_type == enum_class_Exception)) {
GETATTR_Exception_handler(interp, PCONST(1), eh);
}
else {
if (VTABLE_isa(interp, PCONST(1), Parrot_str_new_constant(interp, "ExceptionHandler"))) {
eh = PCONST(1);
}
else {
if (VTABLE_isa(interp, PCONST(1), Parrot_str_new_constant(interp, "Exception"))) {
eh = VTABLE_get_attr_str(interp, PCONST(1), Parrot_str_new_constant(interp, "handler"));
}

}

}

}
PMC * eh = Parrot_ex_get_current_handler(interp, PCONST(1));

if ((!PMC_IS_NULL(eh))) {
Parrot_runloop * rl = interp->current_runloop;
Expand Down Expand Up @@ -14241,26 +14203,7 @@ Parrot_finalize_pc(opcode_t *cur_opcode, PARROT_INTERP) {

opcode_t *
Parrot_pop_upto_eh_p(opcode_t *cur_opcode, PARROT_INTERP) {
PMC * eh = PMCNULL;

if ((!PMC_IS_NULL(PREG(1)))) {
if ((PREG(1)->vtable->base_type == enum_class_Exception)) {
GETATTR_Exception_handler(interp, PREG(1), eh);
}
else {
if (VTABLE_isa(interp, PREG(1), Parrot_str_new_constant(interp, "ExceptionHandler"))) {
eh = PREG(1);
}
else {
if (VTABLE_isa(interp, PREG(1), Parrot_str_new_constant(interp, "Exception"))) {
eh = VTABLE_get_attr_str(interp, PREG(1), Parrot_str_new_constant(interp, "handler"));
}

}

}

}
PMC * eh = Parrot_ex_get_current_handler(interp, PREG(1));

if ((!PMC_IS_NULL(eh))) {
Parrot_cx_delete_upto_handler_local(interp, eh);
Expand All @@ -14271,26 +14214,7 @@ Parrot_pop_upto_eh_p(opcode_t *cur_opcode, PARROT_INTERP) {

opcode_t *
Parrot_pop_upto_eh_pc(opcode_t *cur_opcode, PARROT_INTERP) {
PMC * eh = PMCNULL;

if ((!PMC_IS_NULL(PCONST(1)))) {
if ((PCONST(1)->vtable->base_type == enum_class_Exception)) {
GETATTR_Exception_handler(interp, PCONST(1), eh);
}
else {
if (VTABLE_isa(interp, PCONST(1), Parrot_str_new_constant(interp, "ExceptionHandler"))) {
eh = PCONST(1);
}
else {
if (VTABLE_isa(interp, PCONST(1), Parrot_str_new_constant(interp, "Exception"))) {
eh = VTABLE_get_attr_str(interp, PCONST(1), Parrot_str_new_constant(interp, "handler"));
}

}

}

}
PMC * eh = Parrot_ex_get_current_handler(interp, PCONST(1));

if ((!PMC_IS_NULL(eh))) {
Parrot_cx_delete_upto_handler_local(interp, eh);
Expand Down

0 comments on commit 0c24220

Please sign in to comment.