Skip to content

Last expressions result gets returned from functions when there is no return #672

Closed

Description

@dvtkrlbs

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

Activity

  1. added
    A-Bug
    on Sep 1, 2020
  2. RageKnify commented on Sep 1, 2020

    @RageKnify
    Contributor

    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
  3. changed the title [-]Modified values gets returned from functions when they shouldn't[/-] [+]Last expressions result gets returned from functions when there is no return[/+] on Sep 1, 2020
  4. RageKnify commented on Sep 1, 2020

    @RageKnify
    Contributor

    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.

  5. jasonwilliams commented on Sep 1, 2020

    @jasonwilliams
    Member

    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’t get that far. You could return Ok(val) directly from the return state branch in that link @RageKnify posted.

    It’s something worth playing with, but it should work.

  6. RageKnify commented on Sep 2, 2020

    @RageKnify
    Contributor

    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?

  7. added
    A-Execution
    on Dec 25, 2020
  8. added this to the v0.12.0 milestone on Jan 11, 2021
  9. modified the milestones: v0.12.0, v0.13.0 on May 22, 2021
  10. JayHelton commented on Jun 9, 2021

    @JayHelton

    Hello! Is this available to work on?

  11. jasonwilliams commented on Jun 9, 2021

    @jasonwilliams
    Member

    @JayHelton i believe so!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-BugA-Execution

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions