GitHub

@@ -2603,14 +2603,25 @@ scope_body(codegen_scope *s, node *locals, node *body, int val)

26032603

codegen_scope *scope = scope_new(s->mrb, s, locals);

2604260426052605

/* Generate code for the body of the scope. */

2606-

codegen(scope, body, VAL);

2607-

/* Ensure the scope returns the value of its last expression. */

2608-

gen_return(scope, OP_RETURN, scope->sp-1);

2606+

codegen(scope, body, val);

2609260726102608

/* If this is the outermost scope (e.g., top-level script), add OP_STOP. */

26112609

if (!s->iseq) { /* s->iseq would be NULL for the initial dummy scope. */

2610+

if (val) {

2611+

gen_return(scope, OP_RETURN, scope->sp-1);

2612+

}

2613+

/* skip RETURN when no_return_value; STOP will terminate VM */

26122614

genop_0(scope, OP_STOP);

26132615

}

2616+

else {

2617+

/* Ensure the scope returns the value of its last expression. */

2618+

if (val) {

2619+

gen_return(scope, OP_RETURN, scope->sp-1);

2620+

}

2621+

else {

2622+

gen_return(scope, OP_RETURN, 0); /* return nil */

2623+

}

2624+

}

2614262526152626

/* Finalize the IREP for this scope. */

26162627

scope_finish(scope);

@@ -6120,7 +6131,7 @@ codegen_scope_node(codegen_scope *s, const node *varnode, int val)

61206131

struct mrb_ast_scope_node *scope = scope_node(varnode);

6121613261226133

/* Pass locals and body directly to scope_body() */

6123-

scope_body(s, scope->locals, scope->body, NOVAL);

6134+

scope_body(s, scope->locals, scope->body, val);

61246135

}

6125613661266137

static void

@@ -6937,5 +6948,5 @@ generate_code(mrb_state *mrb, parser_state *p, int val)

69376948

MRB_API struct RProc*

69386949

mrb_generate_code(mrb_state *mrb, parser_state *p)

69396950

{

6940-

return generate_code(mrb, p, VAL);

6951+

return generate_code(mrb, p, p->no_return_value ? NOVAL : VAL);

69416952

}

Read the original on github.com ↗