Boa: Last expressions result gets returned from functions when there is no return

Created on 1 Sep 2020  路  6Comments  路  Source: boa-dev/boa

When non returning functions mutates a property of another object that modified object gets returned even though we dont use the return statement

In the repl

>> function x() {}
undefined
>> function y() { x.y = 3 }
undefined
>> x.y
undefined
>> y()
3

y() should not return anything

bug execution

All 6 comments

This can be generalized, it seems that when the last statement in a function is an expression the expression's result is returned rather than undefined.

>> function z() { 0 }
undefined
>> z()
0

Problem is in: https://github.com/boa-dev/boa/blob/be20b65a9e1a2ac6d802f5c8f3f87edd149274eb/boa/src/exec/statement_list.rs

if i + 1 == self.statements().len() {
     obj = val;
}

Regardless of the type of node, the value obtained from running it is used as the return value.

I think StatementLists are used elsewhere besides function, if so it might not be as simple as removing these lines....

PS: It's not as simple as removing them, I think some mantainer orientation is required.

Seeing that all returns in JavaScript are explicit anyway, I wonder if StatementList can Ok(undefined), it should be safe as any return will change the interpreter state so it wouldn鈥檛 get that far. You could return Ok(val) directly from the return state branch in that link @RageKnify posted.

It鈥檚 something worth playing with, but it should work.

Should have been explicit on my PS, what happened when I tried removing the 3 lines was that the CLI started only returning undefined, because what the parser gives to be run with the Interpreter is a StatementList.

Changing the break to an early return wouldn't change the current flow, right?

Hello! Is this available to work on?

@JayHelton i believe so!

Was this page helpful?
0 / 5 - 0 ratings