Skip to content

Commit

Permalink
improved throw and throw_ref implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Ermler committed Sep 16, 2024
1 parent 79dc612 commit 48d7b92
Showing 1 changed file with 56 additions and 6 deletions.
62 changes: 56 additions & 6 deletions core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -1691,23 +1691,52 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
unwind_and_find_exception_handler:
{
frame_sp -= 2;
void * exnref = (void*) GET_REF_FROM_ADDR(frame_sp);
WASMExceptionReference exnref = (void*) GET_REF_FROM_ADDR(frame_sp);

LOG_REE("THROW_REF is lookung for try-table frame and catch clauses for exnref %p", exnref);
LOG_REE("THROW_REF is looking for try-table frame and catch clauses for exnref %p", exnref);

do {
LOG_REE("frame_csp is a label of type %d", (frame_csp-1)->label_type);
switch ((frame_csp-1)->label_type) {
case LABEL_TYPE_IF:
case LABEL_TYPE_BLOCK:
case LABEL_TYPE_LOOP:
/* that frame type have no excn handlers */
/* that frame types do not have excn handlers */
break;
case LABEL_TYPE_TRY_TABLE:
/* look for clauses */
{
uint32 num_clauses = *(int32 *)((frame_csp-1)->frame_sp);
LOG_REE("there are %d clauses in the frame", num_clauses);

}

/* to be implemented */

uint32 handler_clause = 0;
uint32 handler_depth = 0;
switch (handler_clause) {
case EXCN_HANDLER_CLAUSE_CATCH:
/* put values */
/* release excn instance */
free_exnref(module, exnref);
break;

case EXCN_HANDLER_CLAUSE_CATCH_REF:
/* put values and ref */
break;

case EXCN_HANDLER_CLAUSE_CATCH_ALL:
/* put nothing */
/* release excn instance */
free_exnref(module, exnref);
break;

case EXCN_HANDLER_CLAUSE_CATCH_ALL_REF:
/* put ref */
break;
}

break;
case LABEL_TYPE_FUNCTION:
/* return exnref to caller */
Expand All @@ -1728,6 +1757,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
goto got_exception;

}
/* unwind */
POP_CSP();
} while (frame_csp > frame->csp_bottom);
/* ... */
Expand All @@ -1742,10 +1772,28 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
{
read_leb_int32(frame_ip, frame_ip_end, exception_tag_index);

/* get tag address */
WASMTagInstance * ti = & module->e->tags[exception_tag_index];
WASMFuncType * tag_type = ti->is_import_tag ?
ti->u.tag_import->tag_type :
ti->u.tag->tag_type;

/* create exception instance */
LOG_REE("throw tag index %d, ref %p", exception_tag_index, ti);
WASMExceptionReference exnref = allocate_exnref(module, ti);
if (!exnref) {
wasm_set_exception(
module, "WASM_OP_THROW allocating an exception instance failed.");
goto got_exception;
}

/* collect tag values */
LOG_REE("tag has %d cells, exn has %d cells allocated",
tag_type->param_cell_num,
exnref->cells);

/* create exception ref */
void * exnref = (void*) 0xdeadbeef;
frame_sp-=tag_type->param_cell_num;
word_copy(exnref->vals, frame_sp, tag_type->param_cell_num);

/* push excnref to stack */
PUT_REF_TO_ADDR(frame_sp, exnref);
Expand Down Expand Up @@ -2108,13 +2156,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
uint32 handler_tagindex;
uint32 handler_targetlabel;
read_leb_int32(frame_ip, frame_ip_end, handler_count);

for (i=0; i<handler_count; i++) {
read_leb_int32(frame_ip, frame_ip_end, handler_clause);
switch (handler_clause) {
case EXCN_HANDLER_CLAUSE_CATCH: // catch
case EXCN_HANDLER_CLAUSE_CATCH_REF: // catch_ref
read_leb_int32(frame_ip, frame_ip_end, handler_tagindex);
read_leb_int32(frame_ip, frame_ip_end, handler_targetlabel);
read_leb_int32(frame_ip, frame_ip_end, handler_targetlabel);
LOG_VERBOSE("In %s, found handler clause %d for tagindex %d targetlabel %d\n",
__FUNCTION__,
handler_clause,
Expand Down Expand Up @@ -2148,6 +2197,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,

/* fourth: push control block */
PUSH_CSP(LABEL_TYPE_TRY_TABLE, param_cell_num, cell_num, end_addr);
PUSH_I32(handler_count);

HANDLE_OP_END();
}
Expand Down

0 comments on commit 48d7b92

Please sign in to comment.