From aa63992d1a2df7e3583fae6059d46cb287ab407b Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 17 Apr 2023 20:08:18 -0500 Subject: [PATCH] bugfix: nested do-blocks with indirect return statments --- compiler/src/wasm_emit.c | 12 +++++++++++- core/container/iter.onyx | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/compiler/src/wasm_emit.c b/compiler/src/wasm_emit.c index 7d2e8bce..445b9f2f 100644 --- a/compiler/src/wasm_emit.c +++ b/compiler/src/wasm_emit.c @@ -3769,8 +3769,18 @@ EMIT_FUNC(return, AstReturn* ret) { AstLocal* result_destination = NULL; i64 jump_label = get_structured_jump_label(mod, Jump_Type_Return, ret->count + 1); + // + // If this is return statement if an inner return of a `do` block, + // we have to get the result destination out of the return location stack. + // This can be computed as the -ret->count element. + // + // 2 | return from second do + // 1 | return from first do + // 0 | return from function + // if (bh_arr_length(mod->return_location_stack) > 0 && jump_label >= 0) { - result_destination = bh_arr_last(mod->return_location_stack); + i32 len = bh_arr_length(mod->return_location_stack); + result_destination = mod->return_location_stack[len - ret->count - 1]; } // If we have an expression to return, we see if it should be placed on the linear memory stack, or the WASM stack. diff --git a/core/container/iter.onyx b/core/container/iter.onyx index d76dcd9d..4c8b0c84 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -31,6 +31,7 @@ use core.intrinsics.types {type_is_struct} count :: count; some :: some; every :: every; + collect :: to_array; } -- 2.25.1