Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short circuit evaluation #28

Merged
merged 5 commits into from Mar 7, 2020
Merged

Short circuit evaluation #28

merged 5 commits into from Mar 7, 2020

Conversation

RoboPhred
Copy link
Contributor

@RoboPhred RoboPhred commented Feb 20, 2020

Allow BinaryExpression to handle short-circuit evaluation. This is needed for many use cases involving potentially-null values.

Example:

    var variables = {
        value: null
    };
    var src = 'value && value.length';
    var ast = parse(src).body[0].expression;
    var res = evaluate(ast, variables);

In javascript, the value will be a falsey, so js does not try to evaluate value.length. However, currently BinaryExpression tries to evaluate both sides of the expression before performing the operation. This leads to a crash, as length cannot be retrieved from a null value.
The result is the common javascript idiom foo && access(foo) will not work in static-eval

The fix is to evaluate the left side first, and only evaluate the right side if the left side is not falsey.

@goto-bus-stop
Copy link
Member

Thanks! we should do the same for ||. would you mind adding that to this PR too?

@RoboPhred
Copy link
Contributor Author

Implemented short circuit evaluation for OR.

@RoboPhred
Copy link
Contributor Author

Reworked commits to fix github username / email.

Copy link
Member

@goto-bus-stop goto-bus-stop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants